mirror of
https://github.com/doitsujin/dxvk.git
synced 2025-03-15 07:29:17 +01:00
[util] Add ticket lock implementation
This commit is contained in:
parent
1724d51079
commit
63d42073b8
39
src/util/sync/sync_ticketlock.h
Normal file
39
src/util/sync/sync_ticketlock.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
#include "../thread.h"
|
||||||
|
|
||||||
|
namespace dxvk::sync {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Ticket spinlock
|
||||||
|
*
|
||||||
|
* A fair spinlock implementation that should
|
||||||
|
* be preferred over \ref Spinlock when one of
|
||||||
|
* the threads accessing the lock is likely to
|
||||||
|
* starve another.
|
||||||
|
*/
|
||||||
|
class TicketLock {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void lock() {
|
||||||
|
uint32_t ticket = m_counter.fetch_add(1);
|
||||||
|
|
||||||
|
while (m_serving.load(std::memory_order_acquire) != ticket)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock() {
|
||||||
|
m_serving.fetch_add(1, std::memory_order_release);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::atomic<uint32_t> m_counter = { 0 };
|
||||||
|
std::atomic<uint32_t> m_serving = { 0 };
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user