gthr-posix.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /* Threads compatibility routines for libgcc2 and libobjc. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1997-2015 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #ifndef GCC_GTHR_POSIX_H
  21. #define GCC_GTHR_POSIX_H
  22. /* POSIX threads specific definitions.
  23. Easy, since the interface is just one-to-one mapping. */
  24. #define __GTHREADS 1
  25. #define __GTHREADS_CXX0X 1
  26. #include <pthread.h>
  27. #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
  28. || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK))
  29. # include <unistd.h>
  30. # if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 0
  31. # define _GTHREAD_USE_MUTEX_TIMEDLOCK 1
  32. # else
  33. # define _GTHREAD_USE_MUTEX_TIMEDLOCK 0
  34. # endif
  35. #endif
  36. typedef pthread_t __gthread_t;
  37. typedef pthread_key_t __gthread_key_t;
  38. typedef pthread_once_t __gthread_once_t;
  39. typedef pthread_mutex_t __gthread_mutex_t;
  40. typedef pthread_mutex_t __gthread_recursive_mutex_t;
  41. typedef pthread_cond_t __gthread_cond_t;
  42. typedef struct timespec __gthread_time_t;
  43. /* POSIX like conditional variables are supported. Please look at comments
  44. in gthr.h for details. */
  45. #define __GTHREAD_HAS_COND 1
  46. #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
  47. #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
  48. #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
  49. #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
  50. #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  51. #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  52. #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  53. #else
  54. #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
  55. #endif
  56. #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
  57. #define __GTHREAD_TIME_INIT {0,0}
  58. #ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
  59. # undef __GTHREAD_MUTEX_INIT
  60. #endif
  61. #ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
  62. # undef __GTHREAD_RECURSIVE_MUTEX_INIT
  63. # undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
  64. # define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
  65. #endif
  66. #ifdef _GTHREAD_USE_COND_INIT_FUNC
  67. # undef __GTHREAD_COND_INIT
  68. # define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
  69. #endif
  70. #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
  71. # ifndef __gthrw_pragma
  72. # define __gthrw_pragma(pragma)
  73. # endif
  74. # define __gthrw2(name,name2,type) \
  75. static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
  76. __gthrw_pragma(weak type)
  77. # define __gthrw_(name) __gthrw_ ## name
  78. #else
  79. # define __gthrw2(name,name2,type)
  80. # define __gthrw_(name) name
  81. #endif
  82. /* Typically, __gthrw_foo is a weak reference to symbol foo. */
  83. #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
  84. __gthrw(pthread_once)
  85. __gthrw(pthread_getspecific)
  86. __gthrw(pthread_setspecific)
  87. __gthrw(pthread_create)
  88. __gthrw(pthread_join)
  89. __gthrw(pthread_equal)
  90. __gthrw(pthread_self)
  91. __gthrw(pthread_detach)
  92. #ifndef __BIONIC__
  93. __gthrw(pthread_cancel)
  94. #endif
  95. __gthrw(sched_yield)
  96. __gthrw(pthread_mutex_lock)
  97. __gthrw(pthread_mutex_trylock)
  98. #if _GTHREAD_USE_MUTEX_TIMEDLOCK
  99. __gthrw(pthread_mutex_timedlock)
  100. #endif
  101. __gthrw(pthread_mutex_unlock)
  102. __gthrw(pthread_mutex_init)
  103. __gthrw(pthread_mutex_destroy)
  104. __gthrw(pthread_cond_init)
  105. __gthrw(pthread_cond_broadcast)
  106. __gthrw(pthread_cond_signal)
  107. __gthrw(pthread_cond_wait)
  108. __gthrw(pthread_cond_timedwait)
  109. __gthrw(pthread_cond_destroy)
  110. __gthrw(pthread_key_create)
  111. __gthrw(pthread_key_delete)
  112. __gthrw(pthread_mutexattr_init)
  113. __gthrw(pthread_mutexattr_settype)
  114. __gthrw(pthread_mutexattr_destroy)
  115. #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
  116. /* Objective-C. */
  117. __gthrw(pthread_exit)
  118. #ifdef _POSIX_PRIORITY_SCHEDULING
  119. #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
  120. __gthrw(sched_get_priority_max)
  121. __gthrw(sched_get_priority_min)
  122. #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
  123. #endif /* _POSIX_PRIORITY_SCHEDULING */
  124. __gthrw(pthread_attr_destroy)
  125. __gthrw(pthread_attr_init)
  126. __gthrw(pthread_attr_setdetachstate)
  127. #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
  128. __gthrw(pthread_getschedparam)
  129. __gthrw(pthread_setschedparam)
  130. #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
  131. #endif /* _LIBOBJC || _LIBOBJC_WEAK */
  132. #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
  133. /* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
  134. -pthreads is not specified. The functions are dummies and most return an
  135. error value. However pthread_once returns 0 without invoking the routine
  136. it is passed so we cannot pretend that the interface is active if -pthreads
  137. is not specified. On Solaris 2.5.1, the interface is not exposed at all so
  138. we need to play the usual game with weak symbols. On Solaris 10 and up, a
  139. working interface is always exposed. On FreeBSD 6 and later, libc also
  140. exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
  141. to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
  142. which means the alternate __gthread_active_p below cannot be used there. */
  143. #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
  144. static volatile int __gthread_active = -1;
  145. static void
  146. __gthread_trigger (void)
  147. {
  148. __gthread_active = 1;
  149. }
  150. static inline int
  151. __gthread_active_p (void)
  152. {
  153. static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
  154. static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
  155. /* Avoid reading __gthread_active twice on the main code path. */
  156. int __gthread_active_latest_value = __gthread_active;
  157. /* This test is not protected to avoid taking a lock on the main code
  158. path so every update of __gthread_active in a threaded program must
  159. be atomic with regard to the result of the test. */
  160. if (__builtin_expect (__gthread_active_latest_value < 0, 0))
  161. {
  162. if (__gthrw_(pthread_once))
  163. {
  164. /* If this really is a threaded program, then we must ensure that
  165. __gthread_active has been set to 1 before exiting this block. */
  166. __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
  167. __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
  168. __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
  169. }
  170. /* Make sure we'll never enter this block again. */
  171. if (__gthread_active < 0)
  172. __gthread_active = 0;
  173. __gthread_active_latest_value = __gthread_active;
  174. }
  175. return __gthread_active_latest_value != 0;
  176. }
  177. #else /* neither FreeBSD nor Solaris */
  178. /* For a program to be multi-threaded the only thing that it certainly must
  179. be using is pthread_create. However, there may be other libraries that
  180. intercept pthread_create with their own definitions to wrap pthreads
  181. functionality for some purpose. In those cases, pthread_create being
  182. defined might not necessarily mean that libpthread is actually linked
  183. in.
  184. For the GNU C library, we can use a known internal name. This is always
  185. available in the ABI, but no other library would define it. That is
  186. ideal, since any public pthread function might be intercepted just as
  187. pthread_create might be. __pthread_key_create is an "internal"
  188. implementation symbol, but it is part of the public exported ABI. Also,
  189. it's among the symbols that the static libpthread.a always links in
  190. whenever pthread_create is used, so there is no danger of a false
  191. negative result in any statically-linked, multi-threaded program.
  192. For others, we choose pthread_cancel as a function that seems unlikely
  193. to be redefined by an interceptor library. The bionic (Android) C
  194. library does not provide pthread_cancel, so we do use pthread_create
  195. there (and interceptor libraries lose). */
  196. #ifdef __GLIBC__
  197. __gthrw2(__gthrw_(__pthread_key_create),
  198. __pthread_key_create,
  199. pthread_key_create)
  200. # define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)
  201. #elif defined (__BIONIC__)
  202. # define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)
  203. #else
  204. # define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)
  205. #endif
  206. static inline int
  207. __gthread_active_p (void)
  208. {
  209. static void *const __gthread_active_ptr
  210. = __extension__ (void *) &GTHR_ACTIVE_PROXY;
  211. return __gthread_active_ptr != 0;
  212. }
  213. #endif /* FreeBSD or Solaris */
  214. #else /* not SUPPORTS_WEAK */
  215. /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
  216. calls in shared flavors of the HP-UX C library. Most of the stubs
  217. have no functionality. The details are described in the "libc cumulative
  218. patch" for each subversion of HP-UX 11. There are two special interfaces
  219. provided for checking whether an application is linked to a shared pthread
  220. library or not. However, these interfaces aren't available in early
  221. libpthread libraries. We also need a test that works for archive
  222. libraries. We can't use pthread_once as some libc versions call the
  223. init function. We also can't use pthread_create or pthread_attr_init
  224. as these create a thread and thereby prevent changing the default stack
  225. size. The function pthread_default_stacksize_np is available in both
  226. the archive and shared versions of libpthread. It can be used to
  227. determine the default pthread stack size. There is a stub in some
  228. shared libc versions which returns a zero size if pthreads are not
  229. active. We provide an equivalent stub to handle cases where libc
  230. doesn't provide one. */
  231. #if defined(__hppa__) && defined(__hpux__)
  232. static volatile int __gthread_active = -1;
  233. static inline int
  234. __gthread_active_p (void)
  235. {
  236. /* Avoid reading __gthread_active twice on the main code path. */
  237. int __gthread_active_latest_value = __gthread_active;
  238. size_t __s;
  239. if (__builtin_expect (__gthread_active_latest_value < 0, 0))
  240. {
  241. pthread_default_stacksize_np (0, &__s);
  242. __gthread_active = __s ? 1 : 0;
  243. __gthread_active_latest_value = __gthread_active;
  244. }
  245. return __gthread_active_latest_value != 0;
  246. }
  247. #else /* not hppa-hpux */
  248. static inline int
  249. __gthread_active_p (void)
  250. {
  251. return 1;
  252. }
  253. #endif /* hppa-hpux */
  254. #endif /* SUPPORTS_WEAK */
  255. #ifdef _LIBOBJC
  256. /* This is the config.h file in libobjc/ */
  257. #include <config.h>
  258. #ifdef HAVE_SCHED_H
  259. # include <sched.h>
  260. #endif
  261. /* Key structure for maintaining thread specific storage */
  262. static pthread_key_t _objc_thread_storage;
  263. static pthread_attr_t _objc_thread_attribs;
  264. /* Thread local storage for a single thread */
  265. static void *thread_local_storage = NULL;
  266. /* Backend initialization functions */
  267. /* Initialize the threads subsystem. */
  268. static inline int
  269. __gthread_objc_init_thread_system (void)
  270. {
  271. if (__gthread_active_p ())
  272. {
  273. /* Initialize the thread storage key. */
  274. if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
  275. {
  276. /* The normal default detach state for threads is
  277. * PTHREAD_CREATE_JOINABLE which causes threads to not die
  278. * when you think they should. */
  279. if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
  280. && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
  281. PTHREAD_CREATE_DETACHED) == 0)
  282. return 0;
  283. }
  284. }
  285. return -1;
  286. }
  287. /* Close the threads subsystem. */
  288. static inline int
  289. __gthread_objc_close_thread_system (void)
  290. {
  291. if (__gthread_active_p ()
  292. && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
  293. && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
  294. return 0;
  295. return -1;
  296. }
  297. /* Backend thread functions */
  298. /* Create a new thread of execution. */
  299. static inline objc_thread_t
  300. __gthread_objc_thread_detach (void (*func)(void *), void *arg)
  301. {
  302. objc_thread_t thread_id;
  303. pthread_t new_thread_handle;
  304. if (!__gthread_active_p ())
  305. return NULL;
  306. if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
  307. (void *) func, arg)))
  308. thread_id = (objc_thread_t) new_thread_handle;
  309. else
  310. thread_id = NULL;
  311. return thread_id;
  312. }
  313. /* Set the current thread's priority. */
  314. static inline int
  315. __gthread_objc_thread_set_priority (int priority)
  316. {
  317. if (!__gthread_active_p ())
  318. return -1;
  319. else
  320. {
  321. #ifdef _POSIX_PRIORITY_SCHEDULING
  322. #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
  323. pthread_t thread_id = __gthrw_(pthread_self) ();
  324. int policy;
  325. struct sched_param params;
  326. int priority_min, priority_max;
  327. if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
  328. {
  329. if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
  330. return -1;
  331. if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
  332. return -1;
  333. if (priority > priority_max)
  334. priority = priority_max;
  335. else if (priority < priority_min)
  336. priority = priority_min;
  337. params.sched_priority = priority;
  338. /*
  339. * The solaris 7 and several other man pages incorrectly state that
  340. * this should be a pointer to policy but pthread.h is universally
  341. * at odds with this.
  342. */
  343. if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
  344. return 0;
  345. }
  346. #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
  347. #endif /* _POSIX_PRIORITY_SCHEDULING */
  348. return -1;
  349. }
  350. }
  351. /* Return the current thread's priority. */
  352. static inline int
  353. __gthread_objc_thread_get_priority (void)
  354. {
  355. #ifdef _POSIX_PRIORITY_SCHEDULING
  356. #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
  357. if (__gthread_active_p ())
  358. {
  359. int policy;
  360. struct sched_param params;
  361. if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
  362. return params.sched_priority;
  363. else
  364. return -1;
  365. }
  366. else
  367. #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
  368. #endif /* _POSIX_PRIORITY_SCHEDULING */
  369. return OBJC_THREAD_INTERACTIVE_PRIORITY;
  370. }
  371. /* Yield our process time to another thread. */
  372. static inline void
  373. __gthread_objc_thread_yield (void)
  374. {
  375. if (__gthread_active_p ())
  376. __gthrw_(sched_yield) ();
  377. }
  378. /* Terminate the current thread. */
  379. static inline int
  380. __gthread_objc_thread_exit (void)
  381. {
  382. if (__gthread_active_p ())
  383. /* exit the thread */
  384. __gthrw_(pthread_exit) (&__objc_thread_exit_status);
  385. /* Failed if we reached here */
  386. return -1;
  387. }
  388. /* Returns an integer value which uniquely describes a thread. */
  389. static inline objc_thread_t
  390. __gthread_objc_thread_id (void)
  391. {
  392. if (__gthread_active_p ())
  393. return (objc_thread_t) __gthrw_(pthread_self) ();
  394. else
  395. return (objc_thread_t) 1;
  396. }
  397. /* Sets the thread's local storage pointer. */
  398. static inline int
  399. __gthread_objc_thread_set_data (void *value)
  400. {
  401. if (__gthread_active_p ())
  402. return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
  403. else
  404. {
  405. thread_local_storage = value;
  406. return 0;
  407. }
  408. }
  409. /* Returns the thread's local storage pointer. */
  410. static inline void *
  411. __gthread_objc_thread_get_data (void)
  412. {
  413. if (__gthread_active_p ())
  414. return __gthrw_(pthread_getspecific) (_objc_thread_storage);
  415. else
  416. return thread_local_storage;
  417. }
  418. /* Backend mutex functions */
  419. /* Allocate a mutex. */
  420. static inline int
  421. __gthread_objc_mutex_allocate (objc_mutex_t mutex)
  422. {
  423. if (__gthread_active_p ())
  424. {
  425. mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
  426. if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
  427. {
  428. objc_free (mutex->backend);
  429. mutex->backend = NULL;
  430. return -1;
  431. }
  432. }
  433. return 0;
  434. }
  435. /* Deallocate a mutex. */
  436. static inline int
  437. __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
  438. {
  439. if (__gthread_active_p ())
  440. {
  441. int count;
  442. /*
  443. * Posix Threads specifically require that the thread be unlocked
  444. * for __gthrw_(pthread_mutex_destroy) to work.
  445. */
  446. do
  447. {
  448. count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
  449. if (count < 0)
  450. return -1;
  451. }
  452. while (count);
  453. if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
  454. return -1;
  455. objc_free (mutex->backend);
  456. mutex->backend = NULL;
  457. }
  458. return 0;
  459. }
  460. /* Grab a lock on a mutex. */
  461. static inline int
  462. __gthread_objc_mutex_lock (objc_mutex_t mutex)
  463. {
  464. if (__gthread_active_p ()
  465. && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
  466. {
  467. return -1;
  468. }
  469. return 0;
  470. }
  471. /* Try to grab a lock on a mutex. */
  472. static inline int
  473. __gthread_objc_mutex_trylock (objc_mutex_t mutex)
  474. {
  475. if (__gthread_active_p ()
  476. && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
  477. {
  478. return -1;
  479. }
  480. return 0;
  481. }
  482. /* Unlock the mutex */
  483. static inline int
  484. __gthread_objc_mutex_unlock (objc_mutex_t mutex)
  485. {
  486. if (__gthread_active_p ()
  487. && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
  488. {
  489. return -1;
  490. }
  491. return 0;
  492. }
  493. /* Backend condition mutex functions */
  494. /* Allocate a condition. */
  495. static inline int
  496. __gthread_objc_condition_allocate (objc_condition_t condition)
  497. {
  498. if (__gthread_active_p ())
  499. {
  500. condition->backend = objc_malloc (sizeof (pthread_cond_t));
  501. if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
  502. {
  503. objc_free (condition->backend);
  504. condition->backend = NULL;
  505. return -1;
  506. }
  507. }
  508. return 0;
  509. }
  510. /* Deallocate a condition. */
  511. static inline int
  512. __gthread_objc_condition_deallocate (objc_condition_t condition)
  513. {
  514. if (__gthread_active_p ())
  515. {
  516. if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
  517. return -1;
  518. objc_free (condition->backend);
  519. condition->backend = NULL;
  520. }
  521. return 0;
  522. }
  523. /* Wait on the condition */
  524. static inline int
  525. __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
  526. {
  527. if (__gthread_active_p ())
  528. return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
  529. (pthread_mutex_t *) mutex->backend);
  530. else
  531. return 0;
  532. }
  533. /* Wake up all threads waiting on this condition. */
  534. static inline int
  535. __gthread_objc_condition_broadcast (objc_condition_t condition)
  536. {
  537. if (__gthread_active_p ())
  538. return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
  539. else
  540. return 0;
  541. }
  542. /* Wake up one thread waiting on this condition. */
  543. static inline int
  544. __gthread_objc_condition_signal (objc_condition_t condition)
  545. {
  546. if (__gthread_active_p ())
  547. return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
  548. else
  549. return 0;
  550. }
  551. #else /* _LIBOBJC */
  552. static inline int
  553. __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
  554. void *__args)
  555. {
  556. return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
  557. }
  558. static inline int
  559. __gthread_join (__gthread_t __threadid, void **__value_ptr)
  560. {
  561. return __gthrw_(pthread_join) (__threadid, __value_ptr);
  562. }
  563. static inline int
  564. __gthread_detach (__gthread_t __threadid)
  565. {
  566. return __gthrw_(pthread_detach) (__threadid);
  567. }
  568. static inline int
  569. __gthread_equal (__gthread_t __t1, __gthread_t __t2)
  570. {
  571. return __gthrw_(pthread_equal) (__t1, __t2);
  572. }
  573. static inline __gthread_t
  574. __gthread_self (void)
  575. {
  576. return __gthrw_(pthread_self) ();
  577. }
  578. static inline int
  579. __gthread_yield (void)
  580. {
  581. return __gthrw_(sched_yield) ();
  582. }
  583. static inline int
  584. __gthread_once (__gthread_once_t *__once, void (*__func) (void))
  585. {
  586. if (__gthread_active_p ())
  587. return __gthrw_(pthread_once) (__once, __func);
  588. else
  589. return -1;
  590. }
  591. static inline int
  592. __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
  593. {
  594. return __gthrw_(pthread_key_create) (__key, __dtor);
  595. }
  596. static inline int
  597. __gthread_key_delete (__gthread_key_t __key)
  598. {
  599. return __gthrw_(pthread_key_delete) (__key);
  600. }
  601. static inline void *
  602. __gthread_getspecific (__gthread_key_t __key)
  603. {
  604. return __gthrw_(pthread_getspecific) (__key);
  605. }
  606. static inline int
  607. __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
  608. {
  609. return __gthrw_(pthread_setspecific) (__key, __ptr);
  610. }
  611. static inline void
  612. __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
  613. {
  614. if (__gthread_active_p ())
  615. __gthrw_(pthread_mutex_init) (__mutex, NULL);
  616. }
  617. static inline int
  618. __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
  619. {
  620. if (__gthread_active_p ())
  621. return __gthrw_(pthread_mutex_destroy) (__mutex);
  622. else
  623. return 0;
  624. }
  625. static inline int
  626. __gthread_mutex_lock (__gthread_mutex_t *__mutex)
  627. {
  628. if (__gthread_active_p ())
  629. return __gthrw_(pthread_mutex_lock) (__mutex);
  630. else
  631. return 0;
  632. }
  633. static inline int
  634. __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
  635. {
  636. if (__gthread_active_p ())
  637. return __gthrw_(pthread_mutex_trylock) (__mutex);
  638. else
  639. return 0;
  640. }
  641. #if _GTHREAD_USE_MUTEX_TIMEDLOCK
  642. static inline int
  643. __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
  644. const __gthread_time_t *__abs_timeout)
  645. {
  646. if (__gthread_active_p ())
  647. return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
  648. else
  649. return 0;
  650. }
  651. #endif
  652. static inline int
  653. __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
  654. {
  655. if (__gthread_active_p ())
  656. return __gthrw_(pthread_mutex_unlock) (__mutex);
  657. else
  658. return 0;
  659. }
  660. #if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
  661. || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
  662. static inline int
  663. __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
  664. {
  665. if (__gthread_active_p ())
  666. {
  667. pthread_mutexattr_t __attr;
  668. int __r;
  669. __r = __gthrw_(pthread_mutexattr_init) (&__attr);
  670. if (!__r)
  671. __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
  672. PTHREAD_MUTEX_RECURSIVE);
  673. if (!__r)
  674. __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
  675. if (!__r)
  676. __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
  677. return __r;
  678. }
  679. return 0;
  680. }
  681. #endif
  682. static inline int
  683. __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
  684. {
  685. return __gthread_mutex_lock (__mutex);
  686. }
  687. static inline int
  688. __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
  689. {
  690. return __gthread_mutex_trylock (__mutex);
  691. }
  692. #if _GTHREAD_USE_MUTEX_TIMEDLOCK
  693. static inline int
  694. __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
  695. const __gthread_time_t *__abs_timeout)
  696. {
  697. return __gthread_mutex_timedlock (__mutex, __abs_timeout);
  698. }
  699. #endif
  700. static inline int
  701. __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
  702. {
  703. return __gthread_mutex_unlock (__mutex);
  704. }
  705. static inline int
  706. __gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
  707. {
  708. return __gthread_mutex_destroy (__mutex);
  709. }
  710. #ifdef _GTHREAD_USE_COND_INIT_FUNC
  711. static inline void
  712. __gthread_cond_init_function (__gthread_cond_t *__cond)
  713. {
  714. if (__gthread_active_p ())
  715. __gthrw_(pthread_cond_init) (__cond, NULL);
  716. }
  717. #endif
  718. static inline int
  719. __gthread_cond_broadcast (__gthread_cond_t *__cond)
  720. {
  721. return __gthrw_(pthread_cond_broadcast) (__cond);
  722. }
  723. static inline int
  724. __gthread_cond_signal (__gthread_cond_t *__cond)
  725. {
  726. return __gthrw_(pthread_cond_signal) (__cond);
  727. }
  728. static inline int
  729. __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
  730. {
  731. return __gthrw_(pthread_cond_wait) (__cond, __mutex);
  732. }
  733. static inline int
  734. __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
  735. const __gthread_time_t *__abs_timeout)
  736. {
  737. return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
  738. }
  739. static inline int
  740. __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
  741. __gthread_recursive_mutex_t *__mutex)
  742. {
  743. return __gthread_cond_wait (__cond, __mutex);
  744. }
  745. static inline int
  746. __gthread_cond_destroy (__gthread_cond_t* __cond)
  747. {
  748. return __gthrw_(pthread_cond_destroy) (__cond);
  749. }
  750. #endif /* _LIBOBJC */
  751. #endif /* ! GCC_GTHR_POSIX_H */