2
0

threads.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include "threads.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include "uintmap.h"
  26. extern inline althrd_t althrd_current(void);
  27. extern inline int althrd_equal(althrd_t thr0, althrd_t thr1);
  28. extern inline void althrd_exit(int res);
  29. extern inline void althrd_yield(void);
  30. extern inline int almtx_lock(almtx_t *mtx);
  31. extern inline int almtx_unlock(almtx_t *mtx);
  32. extern inline int almtx_trylock(almtx_t *mtx);
  33. extern inline void *altss_get(altss_t tss_id);
  34. extern inline int altss_set(altss_t tss_id, void *val);
  35. #ifndef UNUSED
  36. #if defined(__cplusplus)
  37. #define UNUSED(x)
  38. #elif defined(__GNUC__)
  39. #define UNUSED(x) UNUSED_##x __attribute__((unused))
  40. #elif defined(__LCLINT__)
  41. #define UNUSED(x) /*@unused@*/ x
  42. #else
  43. #define UNUSED(x) x
  44. #endif
  45. #endif
  46. #define THREAD_STACK_SIZE (2*1024*1024) /* 2MB */
  47. #ifdef _WIN32
  48. #define WIN32_LEAN_AND_MEAN
  49. #include <windows.h>
  50. #include <mmsystem.h>
  51. void althrd_setname(althrd_t thr, const char *name)
  52. {
  53. #if defined(_MSC_VER)
  54. #define MS_VC_EXCEPTION 0x406D1388
  55. #pragma pack(push,8)
  56. struct {
  57. DWORD dwType; // Must be 0x1000.
  58. LPCSTR szName; // Pointer to name (in user addr space).
  59. DWORD dwThreadID; // Thread ID (-1=caller thread).
  60. DWORD dwFlags; // Reserved for future use, must be zero.
  61. } info;
  62. #pragma pack(pop)
  63. info.dwType = 0x1000;
  64. info.szName = name;
  65. info.dwThreadID = thr;
  66. info.dwFlags = 0;
  67. __try {
  68. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  69. }
  70. __except(EXCEPTION_CONTINUE_EXECUTION) {
  71. }
  72. #undef MS_VC_EXCEPTION
  73. #else
  74. (void)thr;
  75. (void)name;
  76. #endif
  77. }
  78. static UIntMap ThrdIdHandle = UINTMAP_STATIC_INITIALIZE;
  79. static void NTAPI althrd_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved))
  80. {
  81. if(reason == DLL_PROCESS_DETACH)
  82. ResetUIntMap(&ThrdIdHandle);
  83. }
  84. #ifdef _MSC_VER
  85. #pragma section(".CRT$XLC",read)
  86. __declspec(allocate(".CRT$XLC")) PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback;
  87. #elif defined(__GNUC__)
  88. PIMAGE_TLS_CALLBACK althrd_callback_ __attribute__((section(".CRT$XLC"))) = althrd_callback;
  89. #else
  90. PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback;
  91. #endif
  92. typedef struct thread_cntr {
  93. althrd_start_t func;
  94. void *arg;
  95. } thread_cntr;
  96. static DWORD WINAPI althrd_starter(void *arg)
  97. {
  98. thread_cntr cntr;
  99. memcpy(&cntr, arg, sizeof(cntr));
  100. free(arg);
  101. return (DWORD)((*cntr.func)(cntr.arg));
  102. }
  103. int althrd_create(althrd_t *thr, althrd_start_t func, void *arg)
  104. {
  105. thread_cntr *cntr;
  106. DWORD thrid;
  107. HANDLE hdl;
  108. cntr = malloc(sizeof(*cntr));
  109. if(!cntr) return althrd_nomem;
  110. cntr->func = func;
  111. cntr->arg = arg;
  112. hdl = CreateThread(NULL, THREAD_STACK_SIZE, althrd_starter, cntr, 0, &thrid);
  113. if(!hdl)
  114. {
  115. free(cntr);
  116. return althrd_error;
  117. }
  118. InsertUIntMapEntry(&ThrdIdHandle, thrid, hdl);
  119. *thr = thrid;
  120. return althrd_success;
  121. }
  122. int althrd_detach(althrd_t thr)
  123. {
  124. HANDLE hdl = RemoveUIntMapKey(&ThrdIdHandle, thr);
  125. if(!hdl) return althrd_error;
  126. CloseHandle(hdl);
  127. return althrd_success;
  128. }
  129. int althrd_join(althrd_t thr, int *res)
  130. {
  131. DWORD code;
  132. HANDLE hdl = RemoveUIntMapKey(&ThrdIdHandle, thr);
  133. if(!hdl) return althrd_error;
  134. WaitForSingleObject(hdl, INFINITE);
  135. GetExitCodeThread(hdl, &code);
  136. CloseHandle(hdl);
  137. if(res != NULL)
  138. *res = (int)code;
  139. return althrd_success;
  140. }
  141. int althrd_sleep(const struct timespec *ts, struct timespec* UNUSED(rem))
  142. {
  143. DWORD msec;
  144. if(ts->tv_sec < 0 || ts->tv_sec >= (0x7fffffff / 1000) ||
  145. ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
  146. return -2;
  147. msec = (DWORD)(ts->tv_sec * 1000);
  148. msec += (DWORD)((ts->tv_nsec+999999) / 1000000);
  149. Sleep(msec);
  150. return 0;
  151. }
  152. int almtx_init(almtx_t *mtx, int type)
  153. {
  154. if(!mtx) return althrd_error;
  155. type &= ~almtx_recursive;
  156. if(type != almtx_plain)
  157. return althrd_error;
  158. InitializeCriticalSection(mtx);
  159. return althrd_success;
  160. }
  161. void almtx_destroy(almtx_t *mtx)
  162. {
  163. DeleteCriticalSection(mtx);
  164. }
  165. int almtx_timedlock(almtx_t* UNUSED(mtx), const struct timespec* UNUSED(ts))
  166. {
  167. /* Windows CRITICAL_SECTIONs don't seem to have a timedlock method. */
  168. return althrd_error;
  169. }
  170. #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
  171. int alcnd_init(alcnd_t *cond)
  172. {
  173. InitializeConditionVariable(cond);
  174. return althrd_success;
  175. }
  176. int alcnd_signal(alcnd_t *cond)
  177. {
  178. WakeConditionVariable(cond);
  179. return althrd_success;
  180. }
  181. int alcnd_broadcast(alcnd_t *cond)
  182. {
  183. WakeAllConditionVariable(cond);
  184. return althrd_success;
  185. }
  186. int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
  187. {
  188. if(SleepConditionVariableCS(cond, mtx, INFINITE) != 0)
  189. return althrd_success;
  190. return althrd_error;
  191. }
  192. int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
  193. {
  194. struct timespec curtime;
  195. DWORD sleeptime;
  196. if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
  197. return althrd_error;
  198. if(curtime.tv_sec > time_point->tv_sec || (curtime.tv_sec == time_point->tv_sec &&
  199. curtime.tv_nsec >= time_point->tv_nsec))
  200. {
  201. if(SleepConditionVariableCS(cond, mtx, 0) != 0)
  202. return althrd_success;
  203. }
  204. else
  205. {
  206. sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
  207. sleeptime += (time_point->tv_sec - curtime.tv_sec)*1000;
  208. if(SleepConditionVariableCS(cond, mtx, sleeptime) != 0)
  209. return althrd_success;
  210. }
  211. return (GetLastError()==ERROR_TIMEOUT) ? althrd_timedout : althrd_error;
  212. }
  213. void alcnd_destroy(alcnd_t* UNUSED(cond))
  214. {
  215. /* Nothing to delete? */
  216. }
  217. #else
  218. /* WARNING: This is a rather poor implementation of condition variables, with
  219. * known problems. However, it's simple, efficient, and good enough for now to
  220. * not require Vista. Based on "Strategies for Implementing POSIX Condition
  221. * Variables" by Douglas C. Schmidt and Irfan Pyarali:
  222. * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
  223. */
  224. /* A better solution may be using Wine's implementation. It requires internals
  225. * (NtCreateKeyedEvent, NtReleaseKeyedEvent, and NtWaitForKeyedEvent) from
  226. * ntdll, and implemention of exchange and compare-exchange for RefCounts.
  227. */
  228. typedef struct {
  229. RefCount wait_count;
  230. HANDLE events[2];
  231. } _int_alcnd_t;
  232. enum {
  233. SIGNAL = 0,
  234. BROADCAST = 1
  235. };
  236. int alcnd_init(alcnd_t *cond)
  237. {
  238. _int_alcnd_t *icond = calloc(1, sizeof(*icond));
  239. if(!icond) return althrd_nomem;
  240. InitRef(&icond->wait_count, 0);
  241. icond->events[SIGNAL] = CreateEventW(NULL, FALSE, FALSE, NULL);
  242. icond->events[BROADCAST] = CreateEventW(NULL, TRUE, FALSE, NULL);
  243. if(!icond->events[SIGNAL] || !icond->events[BROADCAST])
  244. {
  245. if(icond->events[SIGNAL])
  246. CloseHandle(icond->events[SIGNAL]);
  247. if(icond->events[BROADCAST])
  248. CloseHandle(icond->events[BROADCAST]);
  249. free(icond);
  250. return althrd_error;
  251. }
  252. cond->Ptr = icond;
  253. return althrd_success;
  254. }
  255. int alcnd_signal(alcnd_t *cond)
  256. {
  257. _int_alcnd_t *icond = cond->Ptr;
  258. if(ReadRef(&icond->wait_count) > 0)
  259. SetEvent(icond->events[SIGNAL]);
  260. return althrd_success;
  261. }
  262. int alcnd_broadcast(alcnd_t *cond)
  263. {
  264. _int_alcnd_t *icond = cond->Ptr;
  265. if(ReadRef(&icond->wait_count) > 0)
  266. SetEvent(icond->events[BROADCAST]);
  267. return althrd_success;
  268. }
  269. int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
  270. {
  271. _int_alcnd_t *icond = cond->Ptr;
  272. int res;
  273. IncrementRef(&icond->wait_count);
  274. LeaveCriticalSection(mtx);
  275. res = WaitForMultipleObjects(2, icond->events, FALSE, INFINITE);
  276. if(DecrementRef(&icond->wait_count) == 0 && res == WAIT_OBJECT_0+BROADCAST)
  277. ResetEvent(icond->events[BROADCAST]);
  278. EnterCriticalSection(mtx);
  279. return althrd_success;
  280. }
  281. int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
  282. {
  283. _int_alcnd_t *icond = cond->Ptr;
  284. struct timespec curtime;
  285. DWORD sleeptime;
  286. int res;
  287. if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
  288. return althrd_error;
  289. if(curtime.tv_sec > time_point->tv_sec || (curtime.tv_sec == time_point->tv_sec &&
  290. curtime.tv_nsec >= time_point->tv_nsec))
  291. sleeptime = 0;
  292. else
  293. {
  294. sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
  295. sleeptime += (time_point->tv_sec - curtime.tv_sec)*1000;
  296. }
  297. IncrementRef(&icond->wait_count);
  298. LeaveCriticalSection(mtx);
  299. res = WaitForMultipleObjects(2, icond->events, FALSE, sleeptime);
  300. if(DecrementRef(&icond->wait_count) == 0 && res == WAIT_OBJECT_0+BROADCAST)
  301. ResetEvent(icond->events[BROADCAST]);
  302. EnterCriticalSection(mtx);
  303. return (res == WAIT_TIMEOUT) ? althrd_timedout : althrd_success;
  304. }
  305. void alcnd_destroy(alcnd_t *cond)
  306. {
  307. _int_alcnd_t *icond = cond->Ptr;
  308. CloseHandle(icond->events[SIGNAL]);
  309. CloseHandle(icond->events[BROADCAST]);
  310. free(icond);
  311. }
  312. #endif /* defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600 */
  313. /* An associative map of uint:void* pairs. The key is the TLS index (given by
  314. * TlsAlloc), and the value is the altss_dtor_t callback. When a thread exits,
  315. * we iterate over the TLS indices for their thread-local value and call the
  316. * destructor function with it if they're both not NULL. To avoid using
  317. * DllMain, a PIMAGE_TLS_CALLBACK function pointer is placed in a ".CRT$XLx"
  318. * section (where x is a character A to Z) which will be called by the CRT.
  319. */
  320. static UIntMap TlsDestructors = UINTMAP_STATIC_INITIALIZE;
  321. static void NTAPI altss_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved))
  322. {
  323. ALsizei i;
  324. if(reason == DLL_PROCESS_DETACH)
  325. {
  326. ResetUIntMap(&TlsDestructors);
  327. return;
  328. }
  329. if(reason != DLL_THREAD_DETACH)
  330. return;
  331. LockUIntMapRead(&TlsDestructors);
  332. for(i = 0;i < TlsDestructors.size;i++)
  333. {
  334. void *ptr = altss_get(TlsDestructors.keys[i]);
  335. altss_dtor_t callback = (altss_dtor_t)TlsDestructors.values[i];
  336. if(ptr && callback)
  337. callback(ptr);
  338. }
  339. UnlockUIntMapRead(&TlsDestructors);
  340. }
  341. #ifdef _MSC_VER
  342. #pragma section(".CRT$XLB",read)
  343. __declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
  344. #elif defined(__GNUC__)
  345. PIMAGE_TLS_CALLBACK altss_callback_ __attribute__((section(".CRT$XLB"))) = altss_callback;
  346. #else
  347. #warning "No TLS callback support, thread-local contexts may leak references on poorly written applications."
  348. PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
  349. #endif
  350. int altss_create(altss_t *tss_id, altss_dtor_t callback)
  351. {
  352. DWORD key = TlsAlloc();
  353. if(key == TLS_OUT_OF_INDEXES)
  354. return althrd_error;
  355. *tss_id = key;
  356. if(callback != NULL)
  357. InsertUIntMapEntry(&TlsDestructors, key, callback);
  358. return althrd_success;
  359. }
  360. void altss_delete(altss_t tss_id)
  361. {
  362. RemoveUIntMapKey(&TlsDestructors, tss_id);
  363. TlsFree(tss_id);
  364. }
  365. int altimespec_get(struct timespec *ts, int base)
  366. {
  367. static_assert(sizeof(FILETIME) == sizeof(ULARGE_INTEGER),
  368. "Size of FILETIME does not match ULARGE_INTEGER");
  369. if(base == AL_TIME_UTC)
  370. {
  371. union {
  372. FILETIME ftime;
  373. ULARGE_INTEGER ulint;
  374. } systime;
  375. GetSystemTimeAsFileTime(&systime.ftime);
  376. /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
  377. ts->tv_sec = systime.ulint.QuadPart/10000000;
  378. ts->tv_nsec = (systime.ulint.QuadPart%10000000) * 100;
  379. return base;
  380. }
  381. return 0;
  382. }
  383. void alcall_once(alonce_flag *once, void (*callback)(void))
  384. {
  385. LONG ret;
  386. while((ret=InterlockedExchange(once, 1)) == 1)
  387. althrd_yield();
  388. if(ret == 0)
  389. (*callback)();
  390. InterlockedExchange(once, 2);
  391. }
  392. #else
  393. #include <sys/time.h>
  394. #include <unistd.h>
  395. #include <pthread.h>
  396. #ifdef HAVE_PTHREAD_NP_H
  397. #include <pthread_np.h>
  398. #endif
  399. extern inline int althrd_sleep(const struct timespec *ts, struct timespec *rem);
  400. extern inline void alcall_once(alonce_flag *once, void (*callback)(void));
  401. void althrd_setname(althrd_t thr, const char *name)
  402. {
  403. #if defined(HAVE_PTHREAD_SETNAME_NP)
  404. #if defined(PTHREAD_SETNAME_NP_ONE_PARAM)
  405. if(althrd_equal(thr, althrd_current()))
  406. pthread_setname_np(name);
  407. #else
  408. pthread_setname_np(thr, name);
  409. #endif
  410. #elif defined(HAVE_PTHREAD_SET_NAME_NP)
  411. pthread_set_name_np(thr, name);
  412. #else
  413. (void)thr;
  414. (void)name;
  415. #endif
  416. }
  417. typedef struct thread_cntr {
  418. althrd_start_t func;
  419. void *arg;
  420. } thread_cntr;
  421. static void *althrd_starter(void *arg)
  422. {
  423. thread_cntr cntr;
  424. memcpy(&cntr, arg, sizeof(cntr));
  425. free(arg);
  426. return (void*)(intptr_t)((*cntr.func)(cntr.arg));
  427. }
  428. int althrd_create(althrd_t *thr, althrd_start_t func, void *arg)
  429. {
  430. thread_cntr *cntr;
  431. pthread_attr_t attr;
  432. size_t stackmult = 1;
  433. int err;
  434. cntr = malloc(sizeof(*cntr));
  435. if(!cntr) return althrd_nomem;
  436. if(pthread_attr_init(&attr) != 0)
  437. {
  438. free(cntr);
  439. return althrd_error;
  440. }
  441. retry_stacksize:
  442. if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE*stackmult) != 0)
  443. {
  444. pthread_attr_destroy(&attr);
  445. free(cntr);
  446. return althrd_error;
  447. }
  448. cntr->func = func;
  449. cntr->arg = arg;
  450. if((err=pthread_create(thr, &attr, althrd_starter, cntr)) == 0)
  451. {
  452. pthread_attr_destroy(&attr);
  453. return althrd_success;
  454. }
  455. if(err == EINVAL)
  456. {
  457. /* If an invalid stack size, try increasing it (limit x4, 8MB). */
  458. if(stackmult < 4)
  459. {
  460. stackmult *= 2;
  461. goto retry_stacksize;
  462. }
  463. /* If still nothing, try defaults and hope they're good enough. */
  464. if(pthread_create(thr, NULL, althrd_starter, cntr) == 0)
  465. {
  466. pthread_attr_destroy(&attr);
  467. return althrd_success;
  468. }
  469. }
  470. pthread_attr_destroy(&attr);
  471. free(cntr);
  472. return althrd_error;
  473. }
  474. int althrd_detach(althrd_t thr)
  475. {
  476. if(pthread_detach(thr) != 0)
  477. return althrd_error;
  478. return althrd_success;
  479. }
  480. int althrd_join(althrd_t thr, int *res)
  481. {
  482. void *code;
  483. if(pthread_join(thr, &code) != 0)
  484. return althrd_error;
  485. if(res != NULL)
  486. *res = (int)(intptr_t)code;
  487. return althrd_success;
  488. }
  489. int almtx_init(almtx_t *mtx, int type)
  490. {
  491. int ret;
  492. if(!mtx) return althrd_error;
  493. #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
  494. if((type&~(almtx_recursive|almtx_timed)) != 0)
  495. return althrd_error;
  496. #else
  497. if((type&~almtx_recursive) != 0)
  498. return althrd_error;
  499. #endif
  500. type &= ~almtx_timed;
  501. if(type == almtx_plain)
  502. ret = pthread_mutex_init(mtx, NULL);
  503. else
  504. {
  505. pthread_mutexattr_t attr;
  506. ret = pthread_mutexattr_init(&attr);
  507. if(ret) return althrd_error;
  508. if(type == almtx_recursive)
  509. {
  510. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  511. #ifdef HAVE_PTHREAD_MUTEXATTR_SETKIND_NP
  512. if(ret != 0)
  513. ret = pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE);
  514. #endif
  515. }
  516. else
  517. ret = 1;
  518. if(ret == 0)
  519. ret = pthread_mutex_init(mtx, &attr);
  520. pthread_mutexattr_destroy(&attr);
  521. }
  522. return ret ? althrd_error : althrd_success;
  523. }
  524. void almtx_destroy(almtx_t *mtx)
  525. {
  526. pthread_mutex_destroy(mtx);
  527. }
  528. int almtx_timedlock(almtx_t *mtx, const struct timespec *ts)
  529. {
  530. #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
  531. int ret = pthread_mutex_timedlock(mtx, ts);
  532. switch(ret)
  533. {
  534. case 0: return althrd_success;
  535. case ETIMEDOUT: return althrd_timedout;
  536. case EBUSY: return althrd_busy;
  537. }
  538. #endif
  539. return althrd_error;
  540. }
  541. int alcnd_init(alcnd_t *cond)
  542. {
  543. if(pthread_cond_init(cond, NULL) == 0)
  544. return althrd_success;
  545. return althrd_error;
  546. }
  547. int alcnd_signal(alcnd_t *cond)
  548. {
  549. if(pthread_cond_signal(cond) == 0)
  550. return althrd_success;
  551. return althrd_error;
  552. }
  553. int alcnd_broadcast(alcnd_t *cond)
  554. {
  555. if(pthread_cond_broadcast(cond) == 0)
  556. return althrd_success;
  557. return althrd_error;
  558. }
  559. int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
  560. {
  561. if(pthread_cond_wait(cond, mtx) == 0)
  562. return althrd_success;
  563. return althrd_error;
  564. }
  565. int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
  566. {
  567. if(pthread_cond_timedwait(cond, mtx, time_point) == 0)
  568. return althrd_success;
  569. return althrd_error;
  570. }
  571. void alcnd_destroy(alcnd_t *cond)
  572. {
  573. pthread_cond_destroy(cond);
  574. }
  575. int altss_create(altss_t *tss_id, altss_dtor_t callback)
  576. {
  577. if(pthread_key_create(tss_id, callback) != 0)
  578. return althrd_error;
  579. return althrd_success;
  580. }
  581. void altss_delete(altss_t tss_id)
  582. {
  583. pthread_key_delete(tss_id);
  584. }
  585. int altimespec_get(struct timespec *ts, int base)
  586. {
  587. if(base == AL_TIME_UTC)
  588. {
  589. int ret;
  590. #if _POSIX_TIMERS > 0
  591. ret = clock_gettime(CLOCK_REALTIME, ts);
  592. if(ret == 0) return base;
  593. #else /* _POSIX_TIMERS > 0 */
  594. struct timeval tv;
  595. ret = gettimeofday(&tv, NULL);
  596. if(ret == 0)
  597. {
  598. ts->tv_sec = tv.tv_sec;
  599. ts->tv_nsec = tv.tv_usec * 1000;
  600. return base;
  601. }
  602. #endif
  603. }
  604. return 0;
  605. }
  606. #endif
  607. void al_nssleep(unsigned long nsec)
  608. {
  609. struct timespec ts, rem;
  610. ts.tv_sec = nsec / 1000000000ul;
  611. ts.tv_nsec = nsec % 1000000000ul;
  612. while(althrd_sleep(&ts, &rem) == -1)
  613. ts = rem;
  614. }