13 # include <semaphore.h>
17 # include <mach/semaphore.h>
18 # include <mach/task.h>
19 # include <mach/mach_init.h>
21 # include <libkern/OSAtomic.h>
28 #endif // !BB_OS_WIN32
33 typedef pthread_mutex_t bb_mutex_t;
34 typedef sem_t bb_sem_t;
38 typedef pthread_mutex_t bb_mutex_t;
39 typedef semaphore_t bb_sem_t;
43 typedef CRITICAL_SECTION bb_mutex_t;
44 typedef HANDLE bb_sem_t;
48 extern pthread_mutexattr_t _bb_mutexattr;
49 #endif // !BB_OS_WIN32
52 #ifdef BB_ENABLE_EXPERIMENTAL
54 inline bool bb_mutex_init(bb_mutex_t *mutex)
57 InitializeCriticalSection(mutex);
60 return pthread_mutex_init(mutex, &_bb_mutexattr) >= 0;
64 inline bool bb_mutex_destroy(bb_mutex_t *mutex)
67 DeleteCriticalSection(mutex);
70 return pthread_mutex_destroy(mutex) >= 0;
74 inline bool bb_mutex_lock(bb_mutex_t *mutex)
77 DeleteCriticalSection(mutex);
80 return pthread_mutex_lock(mutex) >= 0;
84 inline bool bb_mutex_unlock(bb_mutex_t *mutex)
87 LeaveCriticalSection(mutex);
90 return pthread_mutex_lock(mutex) >= 0;
94 inline bool bb_mutex_trylock(bb_mutex_t *mutex)
97 return TryEnterCriticalSection(mutex) != 0;
99 return pthread_mutex_trylock(mutex) == 0;
105 inline bool bb_sem_init(bb_sem_t *sem,
size_t count)
107 #if defined (BB_OS_LINUX)
108 return sem_init(sem, 0, count) >= 0;
109 #elif defined (BB_OS_MACOS)
110 return semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, count) >= 0;
111 #elif defined (BB_OS_WIN32)
112 *sem = CreateSemaphore(0, count, 0x7fffffff, 0);
117 inline bool bb_sem_destroy(bb_sem_t *sem)
119 #if defined (BB_OS_LINUX)
120 return sem_destroy(sem);
121 #elif defined (BB_OS_MACOS)
122 return semaphore_destroy(mach_task_self(), *sem);
123 #elif defined (BB_OS_WIN32)
128 inline bool bb_sem_wait(bb_sem_t *sem)
130 #if defined (BB_OS_LINUX)
131 return sem_wait(sem);
132 #elif defined (BB_OS_MACOS)
133 return semaphore_wait(*sem);
134 #elif defined (BB_OS_WIN32)
135 return WaitForSingleObject(*sem, INFINITE);
139 inline bool bb_sem_post(bb_sem_t *sem)
141 #if defined (BB_OS_LINUX)
142 return sem_post(sem);
143 #elif defined (BB_OS_MACOS)
144 return semaphore_signal(*sem);
145 #elif defined (BB_OS_WIN32)
146 return ReleaseSemaphore(*sem, 1, 0);
150 #else // BB_ENABLE_EXPERIMENTAL
154 # define bb_mutex_init(MUTPTR) (InitializeCriticalSection(MUTPTR), 1)
155 # define bb_mutex_destroy(MUTPTR) DeleteCriticalSection(MUTPTR)
156 # define bb_mutex_lock(MUTPTR) EnterCriticalSection(MUTPTR)
157 # define bb_mutex_unlock(MUTPTR) LeaveCriticalSection(MUTPTR)
158 # define bb_mutex_trylock(MUTPTR) (TryEnterCriticalSection(MUTPTR) != 0)
160 # define bb_sem_init(SEMPTR,COUNT) ((*(SEMPTR) = CreateSemaphore(0, (COUNT), 0x7fffffff, 0)) != 0)
161 # define bb_sem_destroy(SEMPTR) CloseHandle(*(SEMPTR))
162 # define bb_sem_wait(SEMPTR) WaitForSingleObject(*(SEMPTR), INFINITE)
163 # define bb_sem_post(SEMPTR) ReleaseSemaphore(*(SEMPTR), 1, 0)
167 extern pthread_mutexattr_t _bb_mutexattr;
169 # define bb_mutex_init(MUTPTR) (pthread_mutex_init((MUTPTR),&_bb_mutexattr) >= 0)
170 # define bb_mutex_destroy(MUTPTR) pthread_mutex_destroy(MUTPTR)
171 # define bb_mutex_lock(MUTPTR) pthread_mutex_lock(MUTPTR)
172 # define bb_mutex_unlock(MUTPTR) pthread_mutex_unlock(MUTPTR)
173 # define bb_mutex_trylock(MUTPTR) (pthread_mutex_trylock(MUTPTR) == 0)
175 #endif // !BB_OS_WIN32
178 # define bb_sem_init(SEMPTR,COUNT) (semaphore_create(mach_task_self(), (SEMPTR), SYNC_POLICY_FIFO, (COUNT)) >= 0)
179 # define bb_sem_destroy(SEMPTR) semaphore_destroy(mach_task_self(), *(SEMPTR))
180 # define bb_sem_wait(SEMPTR) semaphore_wait(*(SEMPTR))
181 # define bb_sem_post(SEMPTR) semaphore_signal(*(SEMPTR))
182 #endif // BB_OS_MACOS
185 # define bb_sem_init(SEMPTR, COUNT) (sem_init((SEMPTR), 0, (COUNT)) >= 0)
186 # define bb_sem_destroy sem_destroy
187 # define bb_sem_wait sem_wait
188 # define bb_sem_post sem_post
189 #endif // BB_OS_LINUX
191 #endif // !BB_ENABLE_EXPERIMENTAL
194 # define BBTHREAD_NUM_REGS 7
196 # define BBTHREAD_NUM_REGS 19
198 # define BBTHREAD_NUM_REGS 4
209 #define BBTHREAD_DATA_SIZE 32
218 void *data[BBTHREAD_DATA_SIZE];
222 int locked_regs[BBTHREAD_NUM_REGS];
231 #endif // !BB_OS_WIN32
236 void bbThreadStartup();
249 void bbThreadDetach(
BBThread *thread);
286 BBInt bbThreadAllocData();
300 void _bbThreadUnlockThreads();
317 extern int _bbNeedsLock;
318 extern bb_mutex_t _bbLock;
320 #define BB_LOCK if (_bbNeedsLock) { bb_mutex_lock(&_bbLock); }
321 #define BB_UNLOCK if (_bbNeedsLock) { bb_mutex_unlock(&_bbLock); }
325 #ifndef BB_DISABLE_DEPRECATED
326 # define BB_THREADREGS BBTHREAD_NUM_REGS
327 #endif // !BB_DISABLE_DEPRECATED
331 #endif // BLITZ_THREAD_H