15 #include <type_traits> 21 #if defined(__INTEL_COMPILER) 23 #elif defined(_MSC_VER) 25 #elif defined(__GNUC__) 29 #if defined(_M_IA64) || defined(__ia64__) 31 #elif defined(_WIN64) || defined(__amd64__) || defined(_M_X64) || defined(__x86_64__) 33 #elif defined(_M_IX86) || defined(__i386__) 35 #elif defined(_M_PPC) || defined(__powerpc__) 38 #define AE_ARCH_UNKNOWN 43 #define AE_UNUSED(x) ((void)x) 46 #if defined(__has_feature) 47 #if __has_feature(thread_sanitizer) 48 #define AE_NO_TSAN __attribute__((no_sanitize("thread"))) 58 #if defined(AE_VCPP) || defined(AE_ICC) 59 #define AE_FORCEINLINE __forceinline 62 #define AE_FORCEINLINE inline 64 #define AE_FORCEINLINE inline 69 #if defined(AE_VCPP) || defined(AE_ICC) 70 #define AE_ALIGN(x) __declspec(align(x)) 72 #define AE_ALIGN(x) __attribute__((aligned(x))) 75 #define AE_ALIGN(x) __attribute__((aligned(x))) 97 #if (defined(AE_VCPP) && (_MSC_VER < 1700 || defined(__cplusplus_cli))) || (defined(AE_ICC) && __INTEL_COMPILER < 1600) 102 #if defined(AE_ARCH_X64) || defined(AE_ARCH_X86) 103 #define AeFullSync _mm_mfence 104 #define AeLiteSync _mm_mfence 105 #elif defined(AE_ARCH_IA64) 106 #define AeFullSync __mf 107 #define AeLiteSync __mf 108 #elif defined(AE_ARCH_PPC) 109 #include <ppcintrinsics.h> 110 #define AeFullSync __sync 111 #define AeLiteSync __lwsync 116 #pragma warning(push) 117 #pragma warning(disable: 4365) // Disable erroneous 'conversion from long to unsigned int, signed/unsigned mismatch' error when using `assert` 118 #ifdef __cplusplus_cli 119 #pragma managed(push, off) 133 default: assert(
false);
140 #if defined(AE_ARCH_X86) || defined(AE_ARCH_X64) 153 default: assert(
false);
183 default: assert(
false);
202 default: assert(
false);
214 default: assert(
false);
223 #if !defined(AE_VCPP) || (_MSC_VER >= 1700 && !defined(__cplusplus_cli)) 224 #define AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 227 #ifdef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 243 #pragma warning(push) 244 #pragma warning(disable: 4100) // Get rid of (erroneous) 'unreferenced formal parameter' warning 247 #ifdef __cplusplus_cli 260 #ifndef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 268 #if defined(AE_ARCH_X64) || defined(AE_ARCH_X86) 269 if (
sizeof(T) == 4)
return _InterlockedExchangeAdd((
long volatile*)&
value, (
long)increment);
270 #if defined(_M_AMD64) 271 else if (
sizeof(T) == 8)
return _InterlockedExchangeAdd64((
long long volatile*)&
value, (
long long)increment);
274 #error Unsupported platform 276 assert(
false &&
"T must be either a 32 or 64 bit type");
282 #if defined(AE_ARCH_X64) || defined(AE_ARCH_X86) 283 if (
sizeof(T) == 4)
return _InterlockedExchangeAdd((
long volatile*)&
value, (
long)increment);
284 #if defined(_M_AMD64) 285 else if (
sizeof(T) == 8)
return _InterlockedExchangeAdd64((
long long volatile*)&
value, (
long long)increment);
288 #error Unsupported platform 290 assert(
false &&
"T must be either a 32 or 64 bit type");
322 #ifndef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 344 struct _SECURITY_ATTRIBUTES;
345 __declspec(dllimport)
void* __stdcall CreateSemaphoreW(_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
long lInitialCount,
long lMaximumCount,
const wchar_t* lpName);
346 __declspec(dllimport)
int __stdcall CloseHandle(
void* hObject);
347 __declspec(dllimport)
unsigned long __stdcall WaitForSingleObject(
void* hHandle,
unsigned long dwMilliseconds);
348 __declspec(dllimport)
int __stdcall ReleaseSemaphore(
void* hSemaphore,
long lReleaseCount,
long* lpPreviousCount);
350 #elif defined(__MACH__) 351 #include <mach/mach.h> 352 #elif defined(__unix__) 353 #include <semaphore.h> 387 Semaphore(
const Semaphore& other);
388 Semaphore&
operator=(
const Semaphore& other);
393 assert(initialCount >= 0);
394 const long maxLong = 0x7fffffff;
395 m_hSema = CreateSemaphoreW(
nullptr, initialCount, maxLong,
nullptr);
400 CloseHandle(m_hSema);
405 const unsigned long infinite = 0xffffffff;
406 WaitForSingleObject(m_hSema, infinite);
411 const unsigned long RC_WAIT_TIMEOUT = 0x00000102;
412 return WaitForSingleObject(m_hSema, 0) != RC_WAIT_TIMEOUT;
415 bool timed_wait(std::uint64_t usecs)
AE_NO_TSAN 417 const unsigned long RC_WAIT_TIMEOUT = 0x00000102;
418 return WaitForSingleObject(m_hSema, (
unsigned long)(usecs / 1000)) != RC_WAIT_TIMEOUT;
423 ReleaseSemaphore(m_hSema, count,
nullptr);
426 #elif defined(__MACH__) 436 Semaphore(
const Semaphore& other);
437 Semaphore&
operator=(
const Semaphore& other);
442 assert(initialCount >= 0);
443 semaphore_create(mach_task_self(), &m_sema, SYNC_POLICY_FIFO, initialCount);
448 semaphore_destroy(mach_task_self(), m_sema);
453 semaphore_wait(m_sema);
458 return timed_wait(0);
461 bool timed_wait(std::int64_t timeout_usecs)
AE_NO_TSAN 464 ts.tv_sec =
static_cast<unsigned int>(timeout_usecs / 1000000);
465 ts.tv_nsec = (timeout_usecs % 1000000) * 1000;
468 kern_return_t rc = semaphore_timedwait(m_sema, ts);
470 return rc != KERN_OPERATION_TIMED_OUT && rc != KERN_ABORTED;
475 semaphore_signal(m_sema);
482 semaphore_signal(m_sema);
486 #elif defined(__unix__) 495 Semaphore(
const Semaphore& other);
496 Semaphore&
operator=(
const Semaphore& other);
501 assert(initialCount >= 0);
502 sem_init(&m_sema, 0, initialCount);
507 sem_destroy(&m_sema);
516 rc = sem_wait(&m_sema);
518 while (rc == -1 && errno == EINTR);
525 rc = sem_trywait(&m_sema);
526 }
while (rc == -1 && errno == EINTR);
527 return !(rc == -1 && errno == EAGAIN);
530 bool timed_wait(std::uint64_t usecs)
AE_NO_TSAN 533 const int usecs_in_1_sec = 1000000;
534 const int nsecs_in_1_sec = 1000000000;
535 clock_gettime(CLOCK_REALTIME, &ts);
536 ts.tv_sec += usecs / usecs_in_1_sec;
537 ts.tv_nsec += (usecs % usecs_in_1_sec) * 1000;
540 if (ts.tv_nsec >= nsecs_in_1_sec) {
541 ts.tv_nsec -= nsecs_in_1_sec;
547 rc = sem_timedwait(&m_sema, &ts);
548 }
while (rc == -1 && errno == EINTR);
549 return !(rc == -1 && errno == ETIMEDOUT);
566 #error Unsupported platform! (No semaphore wrapper available) 575 typedef std::make_signed<std::size_t>::type
ssize_t;
590 if (m_count.
load() > 0)
600 if (timeout_usecs < 0)
605 if (m_sema.timed_wait(timeout_usecs))
619 if (oldCount > 0 && m_sema.try_wait())
627 assert(initialCount >= 0);
632 if (m_count.
load() > 0)
643 waitWithPartialSpinning();
648 return tryWait() || waitWithPartialSpinning(timeout_usecs);
655 assert(oldCount >= -1);
664 ssize_t count = m_count.
load();
665 return count > 0 ? count : 0;
671 #if defined(AE_VCPP) && (_MSC_VER < 1700 || defined(__cplusplus_cli)) 673 #ifdef __cplusplus_cli
AE_NO_TSAN LightweightSemaphore(ssize_t initialCount=0)
AE_NO_TSAN weak_atomic(weak_atomic const &other)
AE_FORCEINLINE weak_atomic const & operator=(weak_atomic const &other) AE_NO_TSAN
bool wait(std::int64_t timeout_usecs) AE_NO_TSAN
AE_FORCEINLINE T fetch_add_acquire(T increment) AE_NO_TSAN
ssize_t availableApprox() const AE_NO_TSAN
AE_FORCEINLINE void fence(memory_order order) AE_NO_TSAN
AE_FORCEINLINE T load() const AE_NO_TSAN
AE_NO_TSAN weak_atomic(weak_atomic &&other)
weak_atomic< ssize_t > m_count
bool tryWait() AE_NO_TSAN
AE_NO_TSAN weak_atomic(U &&x)
void signal(ssize_t count=1) AE_NO_TSAN
TFSIMD_FORCE_INLINE const tfScalar & x() const
std::make_signed< std::size_t >::type ssize_t
AE_FORCEINLINE T fetch_add_release(T increment) AE_NO_TSAN
AE_FORCEINLINE void compiler_fence(memory_order order) AE_NO_TSAN
bool waitWithPartialSpinning(std::int64_t timeout_usecs=-1) AE_NO_TSAN
AE_FORCEINLINE weak_atomic const & operator=(U &&x) AE_NO_TSAN