33 #ifndef FIFTYONE_DEGREES_THREADING_INCLUDED 34 #define FIFTYONE_DEGREES_THREADING_INCLUDED 55 #define FIFTYONE_DEGREES_THREAD_ROUTINE LPTHREAD_START_ROUTINE 57 typedef void*(*FIFTYONE_DEGREES_THREAD_ROUTINE)(
void*);
61 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG) 66 #pragma warning (push) 67 #pragma warning (disable: 5105) 69 #pragma warning (default: 5105) 72 #pragma intrinsic (_InterlockedIncrement) 73 #pragma intrinsic (_InterlockedDecrement) 121 typedef struct fiftyone_degrees_signal_t {
171 #define FIFTYONE_DEGREES_THREAD HANDLE 173 #define FIFTYONE_DEGREES_THREAD pthread_t 181 #define FIFTYONE_DEGREES_SIGNAL_CREATE(s) s = fiftyoneDegreesSignalCreate() 187 #define FIFTYONE_DEGREES_SIGNAL_CLOSE(s) fiftyoneDegreesSignalClose(s) 193 #define FIFTYONE_DEGREES_SIGNAL_SET(s) fiftyoneDegreesSignalSet(s) 199 #define FIFTYONE_DEGREES_SIGNAL_WAIT(s) fiftyoneDegreesSignalWait(s) 206 #define FIFTYONE_DEGREES_MUTEX_CREATE(m) m = CreateMutex(NULL,FALSE,NULL) 208 #define FIFTYONE_DEGREES_MUTEX_CREATE(m) fiftyoneDegreesMutexCreate(&m) 216 #define FIFTYONE_DEGREES_MUTEX_CLOSE(m) if (m != NULL) { CloseHandle(m); } 218 #define FIFTYONE_DEGREES_MUTEX_CLOSE(m) fiftyoneDegreesMutexClose(&m) 226 #define FIFTYONE_DEGREES_MUTEX_LOCK(m) WaitForSingleObject(*m, INFINITE) 228 #define FIFTYONE_DEGREES_MUTEX_LOCK(m) fiftyoneDegreesMutexLock(m) 236 #define FIFTYONE_DEGREES_MUTEX_UNLOCK(m) ReleaseMutex(*m) 238 #define FIFTYONE_DEGREES_MUTEX_UNLOCK(m) fiftyoneDegreesMutexUnlock(m) 247 #define FIFTYONE_DEGREES_MUTEX_VALID(m) (*m != NULL) 249 #define FIFTYONE_DEGREES_MUTEX_VALID(m) fiftyoneDegreesMutexValid(m) 260 #define FIFTYONE_DEGREES_THREAD_CREATE(t, m, s) t = \ 261 (FIFTYONE_DEGREES_THREAD)CreateThread(NULL, 0, m, s, 0, NULL) 263 #define FIFTYONE_DEGREES_THREAD_CREATE(t, m, s) pthread_create(&t, NULL, m, s) 272 #define FIFTYONE_DEGREES_THREAD_JOIN(t) WaitForSingleObject(t, INFINITE) 274 #define FIFTYONE_DEGREES_THREAD_JOIN(t) pthread_join(t, NULL) 282 #define FIFTYONE_DEGREES_THREAD_CLOSE(t) CloseHandle(t) 284 #define FIFTYONE_DEGREES_THREAD_CLOSE(t) 291 #define FIFTYONE_DEGREES_THREAD_EXIT ExitThread(0) 293 #define FIFTYONE_DEGREES_THREAD_EXIT pthread_exit(NULL) 302 #define FIFTYONE_DEGREES_INTERLOCK_INC(v) _InterlockedIncrement(v) 304 #define FIFTYONE_DEGREES_INTERLOCK_INC(v) (__atomic_add_fetch(v, 1, __ATOMIC_SEQ_CST)) 313 #define FIFTYONE_DEGREES_INTERLOCK_DEC(v) _InterlockedDecrement(v) 315 #define FIFTYONE_DEGREES_INTERLOCK_DEC(v) (__atomic_add_fetch(v, -1, __ATOMIC_SEQ_CST)) 327 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE(d,e,c) \ 328 InterlockedCompareExchange(&d, e, c) 333 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE(d,e,c) \ 334 __sync_val_compare_and_swap(&d,c,e) 346 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE_64(d,e,c) \ 347 InterlockedCompareExchange64((volatile __int64*)&d, (__int64)e, (__int64)c) 349 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE_64(d,e,c) \ 350 FIFTYONE_DEGREES_INTERLOCK_EXCHANGE(d,e,c) 362 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE_PTR(d,e,c) \ 363 InterlockedCompareExchangePointer((volatile PVOID*)&d,e,c) 365 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE_PTR(d,e,c) \ 366 FIFTYONE_DEGREES_INTERLOCK_EXCHANGE(d,e,c) 402 typedef struct fiftyone_degrees_interlock_dw_type_t {
406 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE_DW(d,e,c) \ 407 InterlockedCompareExchange128(&d.low, e.high, e.low, &c.low) 409 typedef struct fiftyone_degrees_interlock_dw_type_t {
412 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE_DW(d,e,c) \ 413 InterlockedCompareExchange64(&d.value, e.value, c.value) == c.value 416 #if (defined(_LP64) || defined(__MINGW64__)) 417 typedef struct fiftyone_degrees_interlock_dw_type_t {
422 typedef struct fiftyone_degrees_interlock_dw_type_t {
426 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE_DW(d,e,c) \ 427 (__atomic_compare_exchange( \ 428 (fiftyoneDegreesInterlockDoubleWidth*)&d, \ 429 (fiftyoneDegreesInterlockDoubleWidth*)&c, \ 430 (fiftyoneDegreesInterlockDoubleWidth*)&e, \ 439 #define FIFTYONE_DEGREES_IS_LOCK_FREE IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) 441 #define FIFTYONE_DEGREES_IS_LOCK_FREE true 444 #define FIFTYONE_DEGREES_IS_LOCK_FREE __atomic_is_lock_free(sizeof(fiftyoneDegreesInterlockDoubleWidth), NULL) void fiftyoneDegreesMutexCreate(fiftyoneDegreesMutex *mutex)
Initialises the mutex passed to the method.
void fiftyoneDegreesMutexUnlock(fiftyoneDegreesMutex *mutex)
Unlocks the mutex passed to the method.
pthread_mutex_t mutex
Mutex for the signal.
Definition: threading.h:124
void fiftyoneDegreesMutexLock(fiftyoneDegreesMutex *mutex)
Locks the mutex passed to the method.
fiftyoneDegreesSignal * fiftyoneDegreesSignalCreate()
Initialises the signal pointer by setting the condition first followed by the mutex if the condition ...
pthread_mutex_t fiftyoneDegreesMutex
MUTEX AND THREADING MACROS.
Definition: threading.h:91
void fiftyoneDegreesMutexClose(fiftyoneDegreesMutex *mutex)
Closes the mutex passed to the method.
pthread_cond_t cond
Condition variable for the signal.
Definition: threading.h:123
A signal used to limit the number of items that can be created by the pool.
Definition: threading.h:121
bool fiftyoneDegreesThreadingGetIsThreadSafe()
Determines if the methods that should be thread safe have been compiled so they are thread safe.
void fiftyoneDegreesSignalWait(fiftyoneDegreesSignal *signal)
Wait for a signal to be set.
void fiftyoneDegreesSignalClose(fiftyoneDegreesSignal *signal)
Closes the signal ensuring there is a lock on the signal before destroying the signal.
volatile bool wait
Flag indicating if the thread should wait.
Definition: threading.h:122
void fiftyoneDegreesSignalSet(fiftyoneDegreesSignal *signal)
If the signal has not been destroyed then sends a signal to a waiting thread that the signal has been...
Double width (64 or 128 depending on the architecture) compare and exchange.
Definition: threading.h:422