From 63d42073b86b7687b0e5755572a4aa776ea9ee7f Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Tue, 13 Nov 2018 17:05:06 +0100 Subject: [PATCH] [util] Add ticket lock implementation --- src/util/sync/sync_ticketlock.h | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/util/sync/sync_ticketlock.h diff --git a/src/util/sync/sync_ticketlock.h b/src/util/sync/sync_ticketlock.h new file mode 100644 index 00000000..ee55433f --- /dev/null +++ b/src/util/sync/sync_ticketlock.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +#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 m_counter = { 0 }; + std::atomic m_serving = { 0 }; + + }; + +}