threads.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 (1*1024*1024) /* 1MB */
  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|almtx_timed);
  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 *mtx, const struct timespec *ts)
  166. {
  167. int ret;
  168. if(!mtx || !ts)
  169. return althrd_error;
  170. while((ret=almtx_trylock(mtx)) == althrd_busy)
  171. {
  172. struct timespec now;
  173. if(ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000 ||
  174. altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC)
  175. return althrd_error;
  176. if(now.tv_sec > ts->tv_sec || (now.tv_sec == ts->tv_sec && now.tv_nsec >= ts->tv_nsec))
  177. return althrd_timedout;
  178. althrd_yield();
  179. }
  180. return ret;
  181. }
  182. #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
  183. int alcnd_init(alcnd_t *cond)
  184. {
  185. InitializeConditionVariable(cond);
  186. return althrd_success;
  187. }
  188. int alcnd_signal(alcnd_t *cond)
  189. {
  190. WakeConditionVariable(cond);
  191. return althrd_success;
  192. }
  193. int alcnd_broadcast(alcnd_t *cond)
  194. {
  195. WakeAllConditionVariable(cond);
  196. return althrd_success;
  197. }
  198. int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
  199. {
  200. if(SleepConditionVariableCS(cond, mtx, INFINITE) != 0)
  201. return althrd_success;
  202. return althrd_error;
  203. }
  204. int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
  205. {
  206. struct timespec curtime;
  207. DWORD sleeptime;
  208. if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
  209. return althrd_error;
  210. sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
  211. sleeptime += (time_point->tv_sec - curtime.tv_sec)*1000;
  212. if(SleepConditionVariableCS(cond, mtx, sleeptime) != 0)
  213. return althrd_success;
  214. return (GetLastError()==ERROR_TIMEOUT) ? althrd_timedout : althrd_error;
  215. }
  216. void alcnd_destroy(alcnd_t* UNUSED(cond))
  217. {
  218. /* Nothing to delete? */
  219. }
  220. #else
  221. /* WARNING: This is a rather poor implementation of condition variables, with
  222. * known problems. However, it's simple, efficient, and good enough for now to
  223. * not require Vista. Based on "Strategies for Implementing POSIX Condition
  224. * Variables" by Douglas C. Schmidt and Irfan Pyarali:
  225. * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
  226. */
  227. /* A better solution may be using Wine's implementation. It requires internals
  228. * (NtCreateKeyedEvent, NtReleaseKeyedEvent, and NtWaitForKeyedEvent) from
  229. * ntdll, and implemention of exchange and compare-exchange for RefCounts.
  230. */
  231. typedef struct {
  232. RefCount wait_count;
  233. HANDLE events[2];
  234. } _int_alcnd_t;
  235. enum {
  236. SIGNAL = 0,
  237. BROADCAST = 1
  238. };
  239. int alcnd_init(alcnd_t *cond)
  240. {
  241. _int_alcnd_t *icond = calloc(1, sizeof(*icond));
  242. if(!icond) return althrd_nomem;
  243. InitRef(&icond->wait_count, 0);
  244. icond->events[SIGNAL] = CreateEvent(NULL, FALSE, FALSE, NULL);
  245. icond->events[BROADCAST] = CreateEvent(NULL, TRUE, FALSE, NULL);
  246. if(!icond->events[SIGNAL] || !icond->events[BROADCAST])
  247. {
  248. if(icond->events[SIGNAL])
  249. CloseHandle(icond->events[SIGNAL]);
  250. if(icond->events[BROADCAST])
  251. CloseHandle(icond->events[BROADCAST]);
  252. free(icond);
  253. return althrd_error;
  254. }
  255. cond->Ptr = icond;
  256. return althrd_success;
  257. }
  258. int alcnd_signal(alcnd_t *cond)
  259. {
  260. _int_alcnd_t *icond = cond->Ptr;
  261. if(ReadRef(&icond->wait_count) > 0)
  262. SetEvent(icond->events[SIGNAL]);
  263. return althrd_success;
  264. }
  265. int alcnd_broadcast(alcnd_t *cond)
  266. {
  267. _int_alcnd_t *icond = cond->Ptr;
  268. if(ReadRef(&icond->wait_count) > 0)
  269. SetEvent(icond->events[BROADCAST]);
  270. return althrd_success;
  271. }
  272. int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
  273. {
  274. _int_alcnd_t *icond = cond->Ptr;
  275. int res;
  276. IncrementRef(&icond->wait_count);
  277. LeaveCriticalSection(mtx);
  278. res = WaitForMultipleObjects(2, icond->events, FALSE, INFINITE);
  279. if(DecrementRef(&icond->wait_count) == 0 && res == WAIT_OBJECT_0+BROADCAST)
  280. ResetEvent(icond->events[BROADCAST]);
  281. EnterCriticalSection(mtx);
  282. return althrd_success;
  283. }
  284. int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
  285. {
  286. _int_alcnd_t *icond = cond->Ptr;
  287. struct timespec curtime;
  288. DWORD sleeptime;
  289. int res;
  290. if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
  291. return althrd_error;
  292. sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
  293. sleeptime += (time_point->tv_sec - curtime.tv_sec)*1000;
  294. IncrementRef(&icond->wait_count);
  295. LeaveCriticalSection(mtx);
  296. res = WaitForMultipleObjects(2, icond->events, FALSE, sleeptime);
  297. if(DecrementRef(&icond->wait_count) == 0 && res == WAIT_OBJECT_0+BROADCAST)
  298. ResetEvent(icond->events[BROADCAST]);
  299. EnterCriticalSection(mtx);
  300. return (res == WAIT_TIMEOUT) ? althrd_timedout : althrd_success;
  301. }
  302. void alcnd_destroy(alcnd_t *cond)
  303. {
  304. _int_alcnd_t *icond = cond->Ptr;
  305. CloseHandle(icond->events[SIGNAL]);
  306. CloseHandle(icond->events[BROADCAST]);
  307. free(icond);
  308. }
  309. #endif /* defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600 */
  310. /* An associative map of uint:void* pairs. The key is the TLS index (given by
  311. * TlsAlloc), and the value is the altss_dtor_t callback. When a thread exits,
  312. * we iterate over the TLS indices for their thread-local value and call the
  313. * destructor function with it if they're both not NULL. To avoid using
  314. * DllMain, a PIMAGE_TLS_CALLBACK function pointer is placed in a ".CRT$XLx"
  315. * section (where x is a character A to Z) which will be called by the CRT.
  316. */
  317. static UIntMap TlsDestructors = UINTMAP_STATIC_INITIALIZE;
  318. static void NTAPI altss_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved))
  319. {
  320. ALsizei i;
  321. if(reason == DLL_PROCESS_DETACH)
  322. {
  323. ResetUIntMap(&TlsDestructors);
  324. return;
  325. }
  326. if(reason != DLL_THREAD_DETACH)
  327. return;
  328. LockUIntMapRead(&TlsDestructors);
  329. for(i = 0;i < TlsDestructors.size;i++)
  330. {
  331. void *ptr = altss_get(TlsDestructors.array[i].key);
  332. altss_dtor_t callback = (altss_dtor_t)TlsDestructors.array[i].value;
  333. if(ptr && callback)
  334. callback(ptr);
  335. }
  336. UnlockUIntMapRead(&TlsDestructors);
  337. }
  338. #ifdef _MSC_VER
  339. #pragma section(".CRT$XLB",read)
  340. __declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
  341. #elif defined(__GNUC__)
  342. PIMAGE_TLS_CALLBACK altss_callback_ __attribute__((section(".CRT$XLB"))) = altss_callback;
  343. #else
  344. #warning "No TLS callback support, thread-local contexts may leak references on poorly written applications."
  345. PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
  346. #endif
  347. int altss_create(altss_t *tss_id, altss_dtor_t callback)
  348. {
  349. DWORD key = TlsAlloc();
  350. if(key == TLS_OUT_OF_INDEXES)
  351. return althrd_error;
  352. *tss_id = key;
  353. if(callback != NULL)
  354. InsertUIntMapEntry(&TlsDestructors, key, callback);
  355. return althrd_success;
  356. }
  357. void altss_delete(altss_t tss_id)
  358. {
  359. RemoveUIntMapKey(&TlsDestructors, tss_id);
  360. TlsFree(tss_id);
  361. }
  362. int altimespec_get(struct timespec *ts, int base)
  363. {
  364. static_assert(sizeof(FILETIME) == sizeof(ULARGE_INTEGER),
  365. "Size of FILETIME does not match ULARGE_INTEGER");
  366. if(base == AL_TIME_UTC)
  367. {
  368. union {
  369. FILETIME ftime;
  370. ULARGE_INTEGER ulint;
  371. } systime;
  372. GetSystemTimeAsFileTime(&systime.ftime);
  373. /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
  374. ts->tv_sec = systime.ulint.QuadPart/10000000;
  375. ts->tv_nsec = (systime.ulint.QuadPart%10000000) * 100;
  376. return base;
  377. }
  378. return 0;
  379. }
  380. void alcall_once(alonce_flag *once, void (*callback)(void))
  381. {
  382. LONG ret;
  383. while((ret=InterlockedExchange(once, 1)) == 1)
  384. althrd_yield();
  385. if(ret == 0)
  386. (*callback)();
  387. InterlockedExchange(once, 2);
  388. }
  389. #else
  390. #include <sys/time.h>
  391. #include <unistd.h>
  392. #include <pthread.h>
  393. #ifdef HAVE_PTHREAD_NP_H
  394. #include <pthread_np.h>
  395. #endif
  396. extern inline int althrd_sleep(const struct timespec *ts, struct timespec *rem);
  397. extern inline void alcall_once(alonce_flag *once, void (*callback)(void));
  398. void althrd_setname(althrd_t thr, const char *name)
  399. {
  400. #if defined(HAVE_PTHREAD_SETNAME_NP)
  401. #if defined(__GNUC__)
  402. pthread_setname_np(thr, name);
  403. #elif defined(__APPLE__)
  404. if(althrd_equal(thr, althrd_current())
  405. pthread_setname_np(name);
  406. #endif
  407. #elif defined(HAVE_PTHREAD_SET_NAME_NP)
  408. pthread_set_name_np(thr, name);
  409. #else
  410. (void)thr;
  411. (void)name;
  412. #endif
  413. }
  414. typedef struct thread_cntr {
  415. althrd_start_t func;
  416. void *arg;
  417. } thread_cntr;
  418. static void *althrd_starter(void *arg)
  419. {
  420. thread_cntr cntr;
  421. memcpy(&cntr, arg, sizeof(cntr));
  422. free(arg);
  423. return (void*)(intptr_t)((*cntr.func)(cntr.arg));
  424. }
  425. int althrd_create(althrd_t *thr, althrd_start_t func, void *arg)
  426. {
  427. thread_cntr *cntr;
  428. pthread_attr_t attr;
  429. cntr = malloc(sizeof(*cntr));
  430. if(!cntr) return althrd_nomem;
  431. if(pthread_attr_init(&attr) != 0)
  432. {
  433. free(cntr);
  434. return althrd_error;
  435. }
  436. if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0)
  437. {
  438. pthread_attr_destroy(&attr);
  439. free(cntr);
  440. return althrd_error;
  441. }
  442. cntr->func = func;
  443. cntr->arg = arg;
  444. if(pthread_create(thr, &attr, althrd_starter, cntr) != 0)
  445. {
  446. pthread_attr_destroy(&attr);
  447. free(cntr);
  448. return althrd_error;
  449. }
  450. pthread_attr_destroy(&attr);
  451. return althrd_success;
  452. }
  453. int althrd_detach(althrd_t thr)
  454. {
  455. if(pthread_detach(thr) != 0)
  456. return althrd_error;
  457. return althrd_success;
  458. }
  459. int althrd_join(althrd_t thr, int *res)
  460. {
  461. void *code;
  462. if(pthread_join(thr, &code) != 0)
  463. return althrd_error;
  464. if(res != NULL)
  465. *res = (int)(intptr_t)code;
  466. return althrd_success;
  467. }
  468. int almtx_init(almtx_t *mtx, int type)
  469. {
  470. int ret;
  471. if(!mtx) return althrd_error;
  472. if((type&~(almtx_recursive|almtx_timed)) != 0)
  473. return althrd_error;
  474. type &= ~almtx_timed;
  475. if(type == almtx_plain)
  476. ret = pthread_mutex_init(mtx, NULL);
  477. else
  478. {
  479. pthread_mutexattr_t attr;
  480. ret = pthread_mutexattr_init(&attr);
  481. if(ret) return althrd_error;
  482. if(type == almtx_recursive)
  483. {
  484. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  485. #ifdef HAVE_PTHREAD_MUTEXATTR_SETKIND_NP
  486. if(ret != 0)
  487. ret = pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE);
  488. #endif
  489. }
  490. else
  491. ret = 1;
  492. if(ret == 0)
  493. ret = pthread_mutex_init(mtx, &attr);
  494. pthread_mutexattr_destroy(&attr);
  495. }
  496. return ret ? althrd_error : althrd_success;
  497. }
  498. void almtx_destroy(almtx_t *mtx)
  499. {
  500. pthread_mutex_destroy(mtx);
  501. }
  502. int almtx_timedlock(almtx_t *mtx, const struct timespec *ts)
  503. {
  504. int ret;
  505. #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
  506. ret = pthread_mutex_timedlock(mtx, ts);
  507. switch(ret)
  508. {
  509. case 0: return althrd_success;
  510. case ETIMEDOUT: return althrd_timedout;
  511. case EBUSY: return althrd_busy;
  512. }
  513. return althrd_error;
  514. #else
  515. if(!mtx || !ts)
  516. return althrd_error;
  517. while((ret=almtx_trylock(mtx)) == althrd_busy)
  518. {
  519. struct timespec now;
  520. if(ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000 ||
  521. altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC)
  522. return althrd_error;
  523. if(now.tv_sec > ts->tv_sec || (now.tv_sec == ts->tv_sec && now.tv_nsec >= ts->tv_nsec))
  524. return althrd_timedout;
  525. althrd_yield();
  526. }
  527. return ret;
  528. #endif
  529. }
  530. int alcnd_init(alcnd_t *cond)
  531. {
  532. if(pthread_cond_init(cond, NULL) == 0)
  533. return althrd_success;
  534. return althrd_error;
  535. }
  536. int alcnd_signal(alcnd_t *cond)
  537. {
  538. if(pthread_cond_signal(cond) == 0)
  539. return althrd_success;
  540. return althrd_error;
  541. }
  542. int alcnd_broadcast(alcnd_t *cond)
  543. {
  544. if(pthread_cond_broadcast(cond) == 0)
  545. return althrd_success;
  546. return althrd_error;
  547. }
  548. int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
  549. {
  550. if(pthread_cond_wait(cond, mtx) == 0)
  551. return althrd_success;
  552. return althrd_error;
  553. }
  554. int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
  555. {
  556. if(pthread_cond_timedwait(cond, mtx, time_point) == 0)
  557. return althrd_success;
  558. return althrd_error;
  559. }
  560. void alcnd_destroy(alcnd_t *cond)
  561. {
  562. pthread_cond_destroy(cond);
  563. }
  564. int altss_create(altss_t *tss_id, altss_dtor_t callback)
  565. {
  566. if(pthread_key_create(tss_id, callback) != 0)
  567. return althrd_error;
  568. return althrd_success;
  569. }
  570. void altss_delete(altss_t tss_id)
  571. {
  572. pthread_key_delete(tss_id);
  573. }
  574. int altimespec_get(struct timespec *ts, int base)
  575. {
  576. if(base == AL_TIME_UTC)
  577. {
  578. int ret;
  579. #if _POSIX_TIMERS > 0
  580. ret = clock_gettime(CLOCK_REALTIME, ts);
  581. if(ret == 0) return base;
  582. #else /* _POSIX_TIMERS > 0 */
  583. struct timeval tv;
  584. ret = gettimeofday(&tv, NULL);
  585. if(ret == 0)
  586. {
  587. ts->tv_sec = tv.tv_sec;
  588. ts->tv_nsec = tv.tv_usec * 1000;
  589. return base;
  590. }
  591. #endif
  592. }
  593. return 0;
  594. }
  595. #endif
  596. void al_nssleep(unsigned long nsec)
  597. {
  598. struct timespec ts, rem;
  599. ts.tv_sec = nsec / 1000000000ul;
  600. ts.tv_nsec = nsec % 1000000000ul;
  601. while(althrd_sleep(&ts, &rem) == -1)
  602. ts = rem;
  603. }