18 #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 42 #define AE_UNUSED(x) ((void)x) 45 #if defined(AE_VCPP) || defined(AE_ICC) 46 #define AE_FORCEINLINE __forceinline 49 #define AE_FORCEINLINE inline 51 #define AE_FORCEINLINE inline 55 #if defined(AE_VCPP) || defined(AE_ICC) 56 #define AE_ALIGN(x) __declspec(align(x)) 58 #define AE_ALIGN(x) __attribute__((aligned(x))) 61 #define AE_ALIGN(x) __attribute__((aligned(x))) 83 #if (defined(AE_VCPP) && (_MSC_VER < 1700 || defined(__cplusplus_cli))) || defined(AE_ICC) 88 #if defined(AE_ARCH_X64) || defined(AE_ARCH_X86) 89 #define AeFullSync _mm_mfence 90 #define AeLiteSync _mm_mfence 91 #elif defined(AE_ARCH_IA64) 92 #define AeFullSync __mf 93 #define AeLiteSync __mf 94 #elif defined(AE_ARCH_PPC) 95 #include <ppcintrinsics.h> 96 #define AeFullSync __sync 97 #define AeLiteSync __lwsync 101 #pragma warning(push) 102 #pragma warning(disable : 4365) // Disable erroneous 'conversion from long to unsigned int, signed/unsigned mismatch' 104 #ifdef __cplusplus_cli 105 #pragma managed(push, off) 137 #if defined(AE_ARCH_X86) || defined(AE_ARCH_X64) 252 #if !defined(AE_VCPP) || (_MSC_VER >= 1700 && !defined(__cplusplus_cli)) 253 #define AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 256 #ifdef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 267 template <
typename T>
275 #pragma warning(disable : 4100) // Get rid of (erroneous) 'unreferenced formal parameter' warning 277 template <
typename U>
281 #ifdef __cplusplus_cli 294 #pragma warning(default : 4100) 302 #ifndef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 303 template <
typename U>
306 value = std::forward<U>(
x);
322 #if defined(AE_ARCH_X64) || defined(AE_ARCH_X86) 324 return _InterlockedExchangeAdd((
long volatile*)&
value, (
long)increment);
325 #if defined(_M_AMD64) 326 else if (
sizeof(T) == 8)
327 return _InterlockedExchangeAdd64((
long long volatile*)&
value, (
long long)increment);
330 #error Unsupported platform 332 assert(
false &&
"T must be either a 32 or 64 bit type");
338 #if defined(AE_ARCH_X64) || defined(AE_ARCH_X86) 340 return _InterlockedExchangeAdd((
long volatile*)&
value, (
long)increment);
341 #if defined(_M_AMD64) 342 else if (
sizeof(T) == 8)
343 return _InterlockedExchangeAdd64((
long long volatile*)&
value, (
long long)increment);
346 #error Unsupported platform 348 assert(
false &&
"T must be either a 32 or 64 bit type");
352 template <
typename U>
382 #ifndef AE_USE_STD_ATOMIC_FOR_WEAK_ATOMIC 402 struct _SECURITY_ATTRIBUTES;
403 __declspec(dllimport)
void* __stdcall CreateSemaphoreW(_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
long lInitialCount,
404 long lMaximumCount,
const wchar_t* lpName);
405 __declspec(dllimport)
int __stdcall CloseHandle(
void* hObject);
406 __declspec(dllimport)
unsigned long __stdcall WaitForSingleObject(
void* hHandle,
unsigned long dwMilliseconds);
407 __declspec(dllimport)
int __stdcall ReleaseSemaphore(
void* hSemaphore,
long lReleaseCount,
long* lpPreviousCount);
409 #elif defined(__MACH__) 410 #include <mach/mach.h> 411 #elif defined(__unix__) 412 #include <semaphore.h> 446 Semaphore(
const Semaphore& other);
447 Semaphore&
operator=(
const Semaphore& other);
450 Semaphore(
int initialCount = 0)
452 assert(initialCount >= 0);
453 const long maxLong = 0x7fffffff;
454 m_hSema = CreateSemaphoreW(
nullptr, initialCount, maxLong,
nullptr);
459 CloseHandle(m_hSema);
464 const unsigned long infinite = 0xffffffff;
465 WaitForSingleObject(m_hSema, infinite);
470 const unsigned long RC_WAIT_TIMEOUT = 0x00000102;
471 return WaitForSingleObject(m_hSema, 0) != RC_WAIT_TIMEOUT;
474 bool timed_wait(std::uint64_t usecs)
476 const unsigned long RC_WAIT_TIMEOUT = 0x00000102;
477 return WaitForSingleObject(m_hSema, (
unsigned long)(usecs / 1000)) != RC_WAIT_TIMEOUT;
480 void signal(
int count = 1)
482 ReleaseSemaphore(m_hSema, count,
nullptr);
485 #elif defined(__MACH__) 495 Semaphore(
const Semaphore& other);
496 Semaphore&
operator=(
const Semaphore& other);
499 Semaphore(
int initialCount = 0)
501 assert(initialCount >= 0);
502 semaphore_create(mach_task_self(), &m_sema, SYNC_POLICY_FIFO, initialCount);
507 semaphore_destroy(mach_task_self(), m_sema);
512 semaphore_wait(m_sema);
517 return timed_wait(0);
520 bool timed_wait(std::int64_t timeout_usecs)
523 ts.tv_sec = timeout_usecs / 1000000;
524 ts.tv_nsec = (timeout_usecs % 1000000) * 1000;
528 kern_return_t rc = semaphore_timedwait(m_sema, ts);
530 return rc != KERN_OPERATION_TIMED_OUT;
535 semaphore_signal(m_sema);
538 void signal(
int count)
542 semaphore_signal(m_sema);
546 #elif defined(__unix__) 555 Semaphore(
const Semaphore& other);
556 Semaphore&
operator=(
const Semaphore& other);
559 Semaphore(
int initialCount = 0)
561 assert(initialCount >= 0);
562 sem_init(&m_sema, 0, initialCount);
567 sem_destroy(&m_sema);
576 rc = sem_wait(&m_sema);
577 }
while (rc == -1 && errno == EINTR);
585 rc = sem_trywait(&m_sema);
586 }
while (rc == -1 && errno == EINTR);
587 return !(rc == -1 && errno == EAGAIN);
590 bool timed_wait(std::uint64_t usecs)
593 const int usecs_in_1_sec = 1000000;
594 const int nsecs_in_1_sec = 1000000000;
595 clock_gettime(CLOCK_REALTIME, &ts);
596 ts.tv_sec += usecs / usecs_in_1_sec;
597 ts.tv_nsec += (usecs % usecs_in_1_sec) * 1000;
600 if (ts.tv_nsec > nsecs_in_1_sec)
602 ts.tv_nsec -= nsecs_in_1_sec;
609 rc = sem_timedwait(&m_sema, &ts);
610 }
while (rc == -1 && errno == EINTR);
611 return !(rc == -1 && errno == ETIMEDOUT);
619 void signal(
int count)
628 #error Unsupported platform! (No semaphore wrapper available) 637 typedef std::make_signed<std::size_t>::type
ssize_t;
652 if (m_count.
load() > 0)
662 if (timeout_usecs < 0)
667 if (m_sema.timed_wait(timeout_usecs))
681 if (oldCount > 0 && m_sema.try_wait())
689 assert(initialCount >= 0);
694 if (m_count.
load() > 0)
705 waitWithPartialSpinning();
708 bool wait(std::int64_t timeout_usecs)
710 return tryWait() || waitWithPartialSpinning(timeout_usecs);
717 assert(oldCount >= -1);
726 ssize_t count = m_count.
load();
727 return count > 0 ? count : 0;
733 #if defined(AE_VCPP) && (_MSC_VER < 1700 || defined(__cplusplus_cli)) 735 #ifdef __cplusplus_cli
ssize_t availableApprox() const
void signal(ssize_t count=1)
AE_FORCEINLINE T fetch_add_release(T increment)
bool wait(std::int64_t timeout_usecs)
bool waitWithPartialSpinning(std::int64_t timeout_usecs=-1)
weak_atomic(weak_atomic &&other)
LightweightSemaphore(ssize_t initialCount=0)
weak_atomic< ssize_t > m_count
weak_atomic(weak_atomic const &other)
TFSIMD_FORCE_INLINE const tfScalar & x() const
AE_FORCEINLINE void compiler_fence(memory_order order)
std::make_signed< std::size_t >::type ssize_t
AE_FORCEINLINE T load() const
AE_FORCEINLINE T fetch_add_acquire(T increment)
AE_FORCEINLINE weak_atomic const & operator=(U &&x)
AE_FORCEINLINE void fence(memory_order order)
AE_FORCEINLINE weak_atomic const & operator=(weak_atomic const &other)
void increment(int *value)