gthr-posix.h 24 KB

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