22#if defined(__INTEL_COMPILER)
24#elif defined(_MSC_VER)
26#elif defined(__GNUC__)
30#if defined(_M_IA64) || defined(__ia64__)
32#elif defined(_WIN64) || defined(__amd64__) || defined(_M_X64) || defined(__x86_64__)
34#elif defined(_M_IX86) || defined(__i386__)
36#elif defined(_M_PPC) || defined(__powerpc__)
39#define AE_ARCH_UNKNOWN
44#define AE_UNUSED(x) ((void)x)
48#if defined(__SANITIZE_THREAD__)
49#define AE_TSAN_IS_ENABLED
52#if defined(__has_feature)
53#if __has_feature(thread_sanitizer) && !defined(AE_TSAN_IS_ENABLED)
54#define AE_TSAN_IS_ENABLED
58#ifdef AE_TSAN_IS_ENABLED
59#if __cplusplus >= 201703L
60namespace moodycamel {
inline int ae_tsan_global; }
61#define AE_TSAN_ANNOTATE_RELEASE() AnnotateHappensBefore(__FILE__, __LINE__, (void *)(&::moodycamel::ae_tsan_global))
62#define AE_TSAN_ANNOTATE_ACQUIRE() AnnotateHappensAfter(__FILE__, __LINE__, (void *)(&::moodycamel::ae_tsan_global))
63extern "C" void AnnotateHappensBefore(
const char*,
int,
void*);
64extern "C" void AnnotateHappensAfter(
const char*,
int,
void*);
66#define AE_NO_TSAN __attribute__((no_sanitize("thread")))
74#ifndef AE_TSAN_ANNOTATE_RELEASE
75#define AE_TSAN_ANNOTATE_RELEASE()
76#define AE_TSAN_ANNOTATE_ACQUIRE()
81#if defined(AE_VCPP) || defined(AE_ICC)
82#define AE_FORCEINLINE __forceinline
85#define AE_FORCEINLINE inline
87#define AE_FORCEINLINE inline
92#if defined(AE_VCPP) || defined(AE_ICC)
93#define AE_ALIGN(x) __declspec(align(x))
95#define AE_ALIGN(x) __attribute__((aligned(x)))
98#define AE_ALIGN(x) __attribute__((aligned(x)))
104namespace moodycamel {
107 memory_order_relaxed,
108 memory_order_acquire,
109 memory_order_release,
110 memory_order_acq_rel,
111 memory_order_seq_cst,
115 memory_order_sync = memory_order_seq_cst
120#if (defined(AE_VCPP) && (_MSC_VER < 1700 || defined(__cplusplus_cli))) || (defined(AE_ICC) && __INTEL_COMPILER < 1600)
125#if defined(AE_ARCH_X64) || defined(AE_ARCH_X86)
126#define AeFullSync _mm_mfence
127#define AeLiteSync _mm_mfence
128#elif defined(AE_ARCH_IA64)
129#define AeFullSync __mf
130#define AeLiteSync __mf
131#elif defined(AE_ARCH_PPC)
132#include <ppcintrinsics.h>
133#define AeFullSync __sync
134#define AeLiteSync __lwsync
140#pragma warning(disable: 4365)
141#ifdef __cplusplus_cli
142#pragma managed(push, off)
146namespace moodycamel {
148AE_FORCEINLINE
void compiler_fence(memory_order order) AE_NO_TSAN
151 case memory_order_relaxed:
break;
152 case memory_order_acquire: _ReadBarrier();
break;
153 case memory_order_release: _WriteBarrier();
break;
154 case memory_order_acq_rel: _ReadWriteBarrier();
break;
155 case memory_order_seq_cst: _ReadWriteBarrier();
break;
156 default: assert(
false);
163#if defined(AE_ARCH_X86) || defined(AE_ARCH_X64)
164AE_FORCEINLINE
void fence(memory_order order) AE_NO_TSAN
167 case memory_order_relaxed:
break;
168 case memory_order_acquire: _ReadBarrier();
break;
169 case memory_order_release: _WriteBarrier();
break;
170 case memory_order_acq_rel: _ReadWriteBarrier();
break;
171 case memory_order_seq_cst:
176 default: assert(
false);
180AE_FORCEINLINE
void fence(memory_order order) AE_NO_TSAN
184 case memory_order_relaxed:
186 case memory_order_acquire:
191 case memory_order_release:
196 case memory_order_acq_rel:
201 case memory_order_seq_cst:
206 default: assert(
false);
215namespace moodycamel {
217AE_FORCEINLINE
void compiler_fence(memory_order order) AE_NO_TSAN
220 case memory_order_relaxed:
break;
221 case memory_order_acquire: std::atomic_signal_fence(std::memory_order_acquire);
break;
222 case memory_order_release: std::atomic_signal_fence(std::memory_order_release);
break;
223 case memory_order_acq_rel: std::atomic_signal_fence(std::memory_order_acq_rel);
break;
224 case memory_order_seq_cst: std::atomic_signal_fence(std::memory_order_seq_cst);
break;
225 default: assert(
false);
229AE_FORCEINLINE
void fence(memory_order order) AE_NO_TSAN
232 case memory_order_relaxed:
break;
233 case memory_order_acquire: AE_TSAN_ANNOTATE_ACQUIRE(); std::atomic_thread_fence(std::memory_order_acquire);
break;
234 case memory_order_release: AE_TSAN_ANNOTATE_RELEASE(); std::atomic_thread_fence(std::memory_order_release);
break;
235 case memory_order_acq_rel: AE_TSAN_ANNOTATE_ACQUIRE(); AE_TSAN_ANNOTATE_RELEASE(); std::atomic_thread_fence(std::memory_order_acq_rel);
break;
236 case memory_order_seq_cst: AE_TSAN_ANNOTATE_ACQUIRE(); AE_TSAN_ANNOTATE_RELEASE(); std::atomic_thread_fence(std::memory_order_seq_cst);
break;
237 default: assert(
false);
246#if !defined(AE_VCPP) || (_MSC_VER >= 1700 && !defined(__cplusplus_cli))
247#define AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC
250#ifdef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC
259namespace moodycamel {
264 AE_NO_TSAN weak_atomic() : value() { }
267#pragma warning(disable: 4100)
269 template<
typename U> AE_NO_TSAN weak_atomic(U&& x) : value(std::forward<U>(x)) { }
270#ifdef __cplusplus_cli
272 AE_NO_TSAN weak_atomic(nullptr_t) : value(
nullptr) { }
274 AE_NO_TSAN weak_atomic(weak_atomic
const& other) : value(other.load()) { }
275 AE_NO_TSAN weak_atomic(weak_atomic&& other) : value(std::move(other.load())) { }
280 AE_FORCEINLINE
operator T()
const AE_NO_TSAN {
return load(); }
283#ifndef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC
284 template<
typename U> AE_FORCEINLINE weak_atomic
const& operator=(U&& x) AE_NO_TSAN { value = std::forward<U>(x);
return *
this; }
285 AE_FORCEINLINE weak_atomic
const& operator=(weak_atomic
const& other) AE_NO_TSAN { value = other.value;
return *
this; }
287 AE_FORCEINLINE T load()
const AE_NO_TSAN {
return value; }
289 AE_FORCEINLINE T fetch_add_acquire(T increment) AE_NO_TSAN
291#if defined(AE_ARCH_X64) || defined(AE_ARCH_X86)
292 if (
sizeof(T) == 4)
return _InterlockedExchangeAdd((
long volatile*)&value, (
long)increment);
294 else if (
sizeof(T) == 8)
return _InterlockedExchangeAdd64((
long long volatile*)&value, (
long long)increment);
297#error Unsupported platform
299 assert(
false &&
"T must be either a 32 or 64 bit type");
303 AE_FORCEINLINE T fetch_add_release(T increment) AE_NO_TSAN
305#if defined(AE_ARCH_X64) || defined(AE_ARCH_X86)
306 if (
sizeof(T) == 4)
return _InterlockedExchangeAdd((
long volatile*)&value, (
long)increment);
308 else if (
sizeof(T) == 8)
return _InterlockedExchangeAdd64((
long long volatile*)&value, (
long long)increment);
311#error Unsupported platform
313 assert(
false &&
"T must be either a 32 or 64 bit type");
318 AE_FORCEINLINE weak_atomic
const& operator=(U&& x) AE_NO_TSAN
320 value.store(std::forward<U>(x), std::memory_order_relaxed);
324 AE_FORCEINLINE weak_atomic
const& operator=(weak_atomic
const& other) AE_NO_TSAN
326 value.store(other.value.load(std::memory_order_relaxed), std::memory_order_relaxed);
330 AE_FORCEINLINE T load()
const AE_NO_TSAN {
return value.load(std::memory_order_relaxed); }
332 AE_FORCEINLINE T fetch_add_acquire(T increment) AE_NO_TSAN
334 return value.fetch_add(increment, std::memory_order_acquire);
337 AE_FORCEINLINE T fetch_add_release(T increment) AE_NO_TSAN
339 return value.fetch_add(increment, std::memory_order_release);
345#ifndef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC
350 std::atomic<T> value;
367 struct _SECURITY_ATTRIBUTES;
368 __declspec(dllimport)
void* __stdcall CreateSemaphoreW(_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
long lInitialCount,
long lMaximumCount,
const wchar_t* lpName);
369 __declspec(dllimport)
int __stdcall CloseHandle(
void* hObject);
370 __declspec(dllimport)
unsigned long __stdcall WaitForSingleObject(
void* hHandle,
unsigned long dwMilliseconds);
371 __declspec(dllimport)
int __stdcall ReleaseSemaphore(
void* hSemaphore,
long lReleaseCount,
long* lpPreviousCount);
373#elif defined(__MACH__)
374#include <mach/mach.h>
375#elif defined(__unix__)
376#include <semaphore.h>
377#elif defined(FREERTOS)
414 Semaphore(
const Semaphore& other);
415 Semaphore& operator=(
const Semaphore& other);
418 AE_NO_TSAN Semaphore(
int initialCount = 0) : m_hSema()
420 assert(initialCount >= 0);
421 const long maxLong = 0x7fffffff;
422 m_hSema = CreateSemaphoreW(
nullptr, initialCount, maxLong,
nullptr);
426 AE_NO_TSAN ~Semaphore()
428 CloseHandle(m_hSema);
431 bool wait() AE_NO_TSAN
433 const unsigned long infinite = 0xffffffff;
434 return WaitForSingleObject(m_hSema, infinite) == 0;
437 bool try_wait() AE_NO_TSAN
439 return WaitForSingleObject(m_hSema, 0) == 0;
442 bool timed_wait(std::uint64_t usecs) AE_NO_TSAN
444 return WaitForSingleObject(m_hSema, (
unsigned long)(usecs / 1000)) == 0;
447 void signal(
int count = 1) AE_NO_TSAN
449 while (!ReleaseSemaphore(m_hSema, count,
nullptr));
452#elif defined(__MACH__)
462 Semaphore(
const Semaphore& other);
463 Semaphore& operator=(
const Semaphore& other);
466 AE_NO_TSAN Semaphore(
int initialCount = 0) : m_sema()
468 assert(initialCount >= 0);
469 kern_return_t rc = semaphore_create(mach_task_self(), &m_sema, SYNC_POLICY_FIFO, initialCount);
470 assert(rc == KERN_SUCCESS);
474 AE_NO_TSAN ~Semaphore()
476 semaphore_destroy(mach_task_self(), m_sema);
479 bool wait() AE_NO_TSAN
481 return semaphore_wait(m_sema) == KERN_SUCCESS;
484 bool try_wait() AE_NO_TSAN
486 return timed_wait(0);
489 bool timed_wait(std::uint64_t timeout_usecs) AE_NO_TSAN
492 ts.tv_sec =
static_cast<unsigned int>(timeout_usecs / 1000000);
493 ts.tv_nsec =
static_cast<int>((timeout_usecs % 1000000) * 1000);
496 kern_return_t rc = semaphore_timedwait(m_sema, ts);
497 return rc == KERN_SUCCESS;
500 void signal() AE_NO_TSAN
502 while (semaphore_signal(m_sema) != KERN_SUCCESS);
505 void signal(
int count) AE_NO_TSAN
509 while (semaphore_signal(m_sema) != KERN_SUCCESS);
513#elif defined(__unix__)
522 Semaphore(
const Semaphore& other);
523 Semaphore& operator=(
const Semaphore& other);
526 AE_NO_TSAN Semaphore(
int initialCount = 0) : m_sema()
528 assert(initialCount >= 0);
529 int rc = sem_init(&m_sema, 0,
static_cast<unsigned int>(initialCount));
534 AE_NO_TSAN ~Semaphore()
536 sem_destroy(&m_sema);
539 bool wait() AE_NO_TSAN
545 rc = sem_wait(&m_sema);
547 while (rc == -1 && errno == EINTR);
551 bool try_wait() AE_NO_TSAN
555 rc = sem_trywait(&m_sema);
556 }
while (rc == -1 && errno == EINTR);
560 bool timed_wait(std::uint64_t usecs) AE_NO_TSAN
563 const int usecs_in_1_sec = 1000000;
564 const int nsecs_in_1_sec = 1000000000;
565 clock_gettime(CLOCK_REALTIME, &ts);
566 ts.tv_sec +=
static_cast<time_t
>(usecs / usecs_in_1_sec);
567 ts.tv_nsec +=
static_cast<long>(usecs % usecs_in_1_sec) * 1000;
570 if (ts.tv_nsec >= nsecs_in_1_sec) {
571 ts.tv_nsec -= nsecs_in_1_sec;
577 rc = sem_timedwait(&m_sema, &ts);
578 }
while (rc == -1 && errno == EINTR);
582 void signal() AE_NO_TSAN
584 while (sem_post(&m_sema) == -1);
587 void signal(
int count) AE_NO_TSAN
591 while (sem_post(&m_sema) == -1);
595#elif defined(FREERTOS)
602 SemaphoreHandle_t m_sema;
604 Semaphore(
const Semaphore& other);
605 Semaphore& operator=(
const Semaphore& other);
608 AE_NO_TSAN Semaphore(
int initialCount = 0) : m_sema()
610 assert(initialCount >= 0);
611 m_sema = xSemaphoreCreateCounting(
static_cast<UBaseType_t
>(~0ull),
static_cast<UBaseType_t
>(initialCount));
615 AE_NO_TSAN ~Semaphore()
617 vSemaphoreDelete(m_sema);
620 bool wait() AE_NO_TSAN
622 return xSemaphoreTake(m_sema, portMAX_DELAY) == pdTRUE;
625 bool try_wait() AE_NO_TSAN
629 if (xPortIsInsideInterrupt())
630 return xSemaphoreTakeFromISR(m_sema, NULL) == pdTRUE;
631 return xSemaphoreTake(m_sema, 0) == pdTRUE;
634 bool timed_wait(std::uint64_t usecs) AE_NO_TSAN
636 std::uint64_t msecs = usecs / 1000;
637 TickType_t ticks =
static_cast<TickType_t
>(msecs / portTICK_PERIOD_MS);
640 return xSemaphoreTake(m_sema, ticks) == pdTRUE;
643 void signal() AE_NO_TSAN
648 if (xPortIsInsideInterrupt())
649 rc = xSemaphoreGiveFromISR(m_sema, NULL);
651 rc = xSemaphoreGive(m_sema);
652 assert(rc == pdTRUE);
656 void signal(
int count) AE_NO_TSAN
663#error Unsupported platform! (No semaphore wrapper available)
669 class LightweightSemaphore
672 typedef std::make_signed<std::size_t>::type ssize_t;
678 bool waitWithPartialSpinning(std::int64_t timeout_usecs = -1) AE_NO_TSAN
687 if (m_count.load() > 0)
689 m_count.fetch_add_acquire(-1);
692 compiler_fence(memory_order_acquire);
694 oldCount = m_count.fetch_add_acquire(-1);
697 if (timeout_usecs < 0)
702 if (timeout_usecs > 0 && m_sema.timed_wait(
static_cast<uint64_t
>(timeout_usecs)))
711 oldCount = m_count.fetch_add_release(1);
715 oldCount = m_count.fetch_add_acquire(-1);
716 if (oldCount > 0 && m_sema.try_wait())
722 AE_NO_TSAN LightweightSemaphore(ssize_t initialCount = 0) : m_count(initialCount), m_sema()
724 assert(initialCount >= 0);
727 bool tryWait() AE_NO_TSAN
729 if (m_count.load() > 0)
731 m_count.fetch_add_acquire(-1);
737 bool wait() AE_NO_TSAN
739 return tryWait() || waitWithPartialSpinning();
742 bool wait(std::int64_t timeout_usecs) AE_NO_TSAN
744 return tryWait() || waitWithPartialSpinning(timeout_usecs);
747 void signal(ssize_t count = 1) AE_NO_TSAN
750 ssize_t oldCount = m_count.fetch_add_release(count);
751 assert(oldCount >= -1);
758 std::size_t availableApprox()
const AE_NO_TSAN
760 ssize_t count = m_count.load();
761 return count > 0 ?
static_cast<std::size_t
>(count) : 0;
767#if defined(AE_VCPP) && (_MSC_VER < 1700 || defined(__cplusplus_cli))
769#ifdef __cplusplus_cli
Definition atomicops.h:262