26 #ifndef FIFTYONE_DEGREES_THREADING_INCLUDED 27 #define FIFTYONE_DEGREES_THREADING_INCLUDED 32 #define EXTERNAL extern "C" 50 #define FIFTYONE_DEGREES_THREAD_ROUTINE LPTHREAD_START_ROUTINE 52 typedef void*(*FIFTYONE_DEGREES_THREAD_ROUTINE)(
void*);
56 #if !defined(DEBUG) && !defined(_DEBUG) && !defined(NDEBUG) 63 #pragma intrinsic (_InterlockedIncrement) 64 #pragma intrinsic (_InterlockedDecrement) 112 typedef struct fiftyone_degrees_signal_t {
162 #define FIFTYONE_DEGREES_THREAD HANDLE 164 #define FIFTYONE_DEGREES_THREAD pthread_t 172 #define FIFTYONE_DEGREES_SIGNAL_CREATE(s) s = fiftyoneDegreesSignalCreate() 178 #define FIFTYONE_DEGREES_SIGNAL_CLOSE(s) fiftyoneDegreesSignalClose(s) 184 #define FIFTYONE_DEGREES_SIGNAL_SET(s) fiftyoneDegreesSignalSet(s) 190 #define FIFTYONE_DEGREES_SIGNAL_WAIT(s) fiftyoneDegreesSignalWait(s) 197 #define FIFTYONE_DEGREES_MUTEX_CREATE(m) m = CreateMutex(NULL,FALSE,NULL) 199 #define FIFTYONE_DEGREES_MUTEX_CREATE(m) fiftyoneDegreesMutexCreate(&m) 207 #define FIFTYONE_DEGREES_MUTEX_CLOSE(m) if (m != NULL) { CloseHandle(m); } 209 #define FIFTYONE_DEGREES_MUTEX_CLOSE(m) fiftyoneDegreesMutexClose(&m) 217 #define FIFTYONE_DEGREES_MUTEX_LOCK(m) WaitForSingleObject(*m, INFINITE) 219 #define FIFTYONE_DEGREES_MUTEX_LOCK(m) fiftyoneDegreesMutexLock(m) 227 #define FIFTYONE_DEGREES_MUTEX_UNLOCK(m) ReleaseMutex(*m) 229 #define FIFTYONE_DEGREES_MUTEX_UNLOCK(m) fiftyoneDegreesMutexUnlock(m) 238 #define FIFTYONE_DEGREES_MUTEX_VALID(m) (*m != NULL) 240 #define FIFTYONE_DEGREES_MUTEX_VALID(m) fiftyoneDegreesMutexValid(m) 251 #define FIFTYONE_DEGREES_THREAD_CREATE(t, m, s) t = \ 252 (FIFTYONE_DEGREES_THREAD)CreateThread(NULL, 0, m, s, 0, NULL) 254 #define FIFTYONE_DEGREES_THREAD_CREATE(t, m, s) pthread_create(&t, NULL, m, s) 263 #define FIFTYONE_DEGREES_THREAD_JOIN(t) WaitForSingleObject(t, INFINITE) 265 #define FIFTYONE_DEGREES_THREAD_JOIN(t) pthread_join(t, NULL) 273 #define FIFTYONE_DEGREES_THREAD_CLOSE(t) CloseHandle(t) 275 #define FIFTYONE_DEGREES_THREAD_CLOSE(t) 282 #define FIFTYONE_DEGREES_THREAD_EXIT ExitThread(0) 284 #define FIFTYONE_DEGREES_THREAD_EXIT pthread_exit(NULL) 293 #define FIFTYONE_DEGREES_INTERLOCK_INC(v) _InterlockedIncrement(v) 295 #define FIFTYONE_DEGREES_INTERLOCK_INC(v) (__sync_add_and_fetch(v, 1)) 304 #define FIFTYONE_DEGREES_INTERLOCK_DEC(v) _InterlockedDecrement(v) 306 #define FIFTYONE_DEGREES_INTERLOCK_DEC(v) (__sync_add_and_fetch(v, -1)) 318 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE(d,e,c) \ 319 InterlockedCompareExchange(&d, e, c) 321 #define FIFTYONE_DEGREES_INTERLOCK_EXCHANGE(d,e,c) \ 322 __sync_val_compare_and_swap(&d,c,e) EXTERNAL void fiftyoneDegreesMutexUnlock(fiftyoneDegreesMutex *mutex)
Unlocks the mutex passed to the method.
Definition: threading.c:86
EXTERNAL void fiftyoneDegreesMutexCreate(fiftyoneDegreesMutex *mutex)
Initialises the mutex passed to the method.
Definition: threading.c:70
pthread_mutex_t mutex
Mutex for the signal.
Definition: threading.h:115
fiftyoneDegreesSignal * fiftyoneDegreesSignalCreate()
Initialises the signal pointer by setting the condition first followed by the mutex if the condition ...
Definition: threading.c:90
pthread_mutex_t fiftyoneDegreesMutex
MUTEX AND THREADING MACROS.
Definition: threading.h:82
EXTERNAL void fiftyoneDegreesMutexClose(fiftyoneDegreesMutex *mutex)
Closes the mutex passed to the method.
Definition: threading.c:78
pthread_cond_t cond
Condition variable for the signal.
Definition: threading.h:114
EXTERNAL void fiftyoneDegreesMutexLock(fiftyoneDegreesMutex *mutex)
Locks the mutex passed to the method.
Definition: threading.c:82
A signal used to limit the number of items that can be created by the pool.
Definition: threading.h:112
void fiftyoneDegreesSignalWait(fiftyoneDegreesSignal *signal)
Wait for a signal to be set.
Definition: threading.c:118
void fiftyoneDegreesSignalClose(fiftyoneDegreesSignal *signal)
Closes the signal ensuring there is a lock on the signal before destroying the signal.
Definition: threading.c:103
volatile bool wait
Flag indicating if the thread should wait.
Definition: threading.h:113
void fiftyoneDegreesSignalSet(fiftyoneDegreesSignal *signal)
If the signal has not been destroyed then sends a signal to a waiting thread that the signal has been...
Definition: threading.c:110
EXTERNAL bool fiftyoneDegreesThreadingGetIsThreadSafe()
Determines if the methods that should be thread safe have been compiled so they are thread safe.
Definition: threading.c:134