pthreadP.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _PTHREADP_H
  17. #define _PTHREADP_H 1
  18. #include <pthread.h>
  19. #include <setjmp.h>
  20. #include <stdbool.h>
  21. #include <sys/syscall.h>
  22. #include "descr.h"
  23. #include <tls.h>
  24. #include <lowlevellock.h>
  25. #include <bits/stackinfo.h>
  26. #include <internaltypes.h>
  27. #include <pthread-functions.h>
  28. #include <atomic.h>
  29. /* Atomic operations on TLS memory. */
  30. #ifndef THREAD_ATOMIC_CMPXCHG_VAL
  31. # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, new, old) \
  32. atomic_compare_and_exchange_val_acq (&(descr)->member, new, old)
  33. #endif
  34. #ifndef THREAD_ATOMIC_BIT_SET
  35. # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
  36. atomic_bit_set (&(descr)->member, bit)
  37. #endif
  38. /* Adaptive mutex definitions. */
  39. #ifndef MAX_ADAPTIVE_COUNT
  40. # define MAX_ADAPTIVE_COUNT 100
  41. #endif
  42. /* Internal variables. */
  43. /* Default stack size. */
  44. extern size_t __default_stacksize attribute_hidden;
  45. /* Size and alignment of static TLS block. */
  46. extern size_t __static_tls_size attribute_hidden;
  47. extern size_t __static_tls_align_m1 attribute_hidden;
  48. /* Flag whether the machine is SMP or not. */
  49. extern int __is_smp attribute_hidden;
  50. /* Thread descriptor handling. */
  51. extern list_t __stack_user;
  52. hidden_proto (__stack_user)
  53. /* Attribute handling. */
  54. extern struct pthread_attr *__attr_list attribute_hidden;
  55. extern lll_lock_t __attr_list_lock attribute_hidden;
  56. /* First available RT signal. */
  57. extern int __current_sigrtmin attribute_hidden;
  58. /* Last available RT signal. */
  59. extern int __current_sigrtmax attribute_hidden;
  60. /* Concurrency handling. */
  61. extern int __concurrency_level attribute_hidden;
  62. /* Thread-local data key handling. */
  63. extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
  64. hidden_proto (__pthread_keys)
  65. /* Number of threads running. */
  66. extern unsigned int __nptl_nthreads attribute_hidden;
  67. /* The library can run in debugging mode where it performs a lot more
  68. tests. */
  69. extern int __pthread_debug attribute_hidden;
  70. /** For now disable debugging support. */
  71. #if 0
  72. # define DEBUGGING_P __builtin_expect (__pthread_debug, 0)
  73. # define INVALID_TD_P(pd) (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
  74. # define INVALID_NOT_TERMINATED_TD_P(pd) INVALID_TD_P (pd)
  75. #else
  76. # define DEBUGGING_P 0
  77. /* Simplified test. This will not catch all invalid descriptors but
  78. is better than nothing. And if the test triggers the thread
  79. descriptor is guaranteed to be invalid. */
  80. # define INVALID_TD_P(pd) __builtin_expect ((pd)->tid <= 0, 0)
  81. # define INVALID_NOT_TERMINATED_TD_P(pd) __builtin_expect ((pd)->tid < 0, 0)
  82. #endif
  83. /* Cancellation test. */
  84. #define CANCELLATION_P(self) \
  85. do { \
  86. int cancelhandling = THREAD_GETMEM (self, cancelhandling); \
  87. if (CANCEL_ENABLED_AND_CANCELED (cancelhandling)) \
  88. { \
  89. THREAD_SETMEM (self, result, PTHREAD_CANCELED); \
  90. __do_cancel (); \
  91. } \
  92. } while (0)
  93. extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
  94. __cleanup_fct_attribute __attribute ((__noreturn__))
  95. #if !defined SHARED && !defined IS_IN_libpthread
  96. weak_function
  97. #endif
  98. ;
  99. extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf)
  100. __cleanup_fct_attribute __attribute ((__noreturn__))
  101. #ifndef SHARED
  102. weak_function
  103. #endif
  104. ;
  105. extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
  106. __cleanup_fct_attribute;
  107. extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
  108. __cleanup_fct_attribute;
  109. #if defined NOT_IN_libc && defined IS_IN_libpthread
  110. hidden_proto (__pthread_unwind)
  111. hidden_proto (__pthread_unwind_next)
  112. hidden_proto (__pthread_register_cancel)
  113. hidden_proto (__pthread_unregister_cancel)
  114. # ifdef SHARED
  115. extern void attribute_hidden pthread_cancel_init (void);
  116. # endif
  117. #endif
  118. /* Called when a thread reacts on a cancellation request. */
  119. static inline void
  120. __attribute ((noreturn, always_inline))
  121. __do_cancel (void)
  122. {
  123. struct pthread *self = THREAD_SELF;
  124. /* Make sure we get no more cancellations. */
  125. THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT);
  126. __pthread_unwind ((__pthread_unwind_buf_t *)
  127. THREAD_GETMEM (self, cleanup_jmp_buf));
  128. }
  129. /* Set cancellation mode to asynchronous. */
  130. #define CANCEL_ASYNC() \
  131. __pthread_enable_asynccancel ()
  132. /* Reset to previous cancellation mode. */
  133. #define CANCEL_RESET(oldtype) \
  134. __pthread_disable_asynccancel (oldtype)
  135. #if !defined NOT_IN_libc
  136. /* Same as CANCEL_ASYNC, but for use in libc.so. */
  137. # define LIBC_CANCEL_ASYNC() \
  138. __libc_enable_asynccancel ()
  139. /* Same as CANCEL_RESET, but for use in libc.so. */
  140. # define LIBC_CANCEL_RESET(oldtype) \
  141. __libc_disable_asynccancel (oldtype)
  142. # define LIBC_CANCEL_HANDLED() \
  143. __asm (".globl " __USER_LABEL_PREFIX__ "__libc_enable_asynccancel"); \
  144. __asm (".globl " __USER_LABEL_PREFIX__ "__libc_disable_asynccancel")
  145. #elif defined NOT_IN_libc && defined IS_IN_libpthread
  146. # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
  147. # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
  148. # define LIBC_CANCEL_HANDLED() \
  149. __asm (".globl " __USER_LABEL_PREFIX__ "__pthread_enable_asynccancel"); \
  150. __asm (".globl " __USER_LABEL_PREFIX__ "__pthread_disable_asynccancel")
  151. #elif defined NOT_IN_libc && defined IS_IN_librt
  152. # define LIBC_CANCEL_ASYNC() \
  153. __librt_enable_asynccancel ()
  154. # define LIBC_CANCEL_RESET(val) \
  155. __librt_disable_asynccancel (val)
  156. # define LIBC_CANCEL_HANDLED() \
  157. __asm (".globl " __USER_LABEL_PREFIX__ "__librt_enable_asynccancel"); \
  158. __asm (".globl " __USER_LABEL_PREFIX__ "__librt_disable_asynccancel")
  159. #else
  160. # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
  161. # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
  162. # define LIBC_CANCEL_HANDLED() /* Nothing. */
  163. #endif
  164. /* The signal used for asynchronous cancelation. */
  165. #define SIGCANCEL __SIGRTMIN
  166. /* Signal needed for the kernel-supported POSIX timer implementation.
  167. We can reuse the cancellation signal since we can distinguish
  168. cancellation from timer expirations. */
  169. #define SIGTIMER SIGCANCEL
  170. /* Signal used to implement the setuid et.al. functions. */
  171. #define SIGSETXID (__SIGRTMIN + 1)
  172. /* Used to communicate with signal handler. */
  173. extern struct xid_command *__xidcmd attribute_hidden;
  174. /* Internal prototypes. */
  175. /* Thread list handling. */
  176. extern struct pthread *__find_in_stack_list (struct pthread *pd)
  177. attribute_hidden internal_function;
  178. /* Deallocate a thread's stack after optionally making sure the thread
  179. descriptor is still valid. */
  180. extern void __free_tcb (struct pthread *pd) attribute_hidden internal_function;
  181. /* Free allocated stack. */
  182. extern void __deallocate_stack (struct pthread *pd)
  183. attribute_hidden internal_function;
  184. /* Mark all the stacks except for the current one as available. This
  185. function also re-initializes the lock for the stack cache. */
  186. extern void __reclaim_stacks (void) attribute_hidden;
  187. /* Make all threads's stacks executable. */
  188. extern int __make_stacks_executable (void **stack_endp)
  189. internal_function attribute_hidden;
  190. /* longjmp handling. */
  191. extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
  192. #if defined NOT_IN_libc && defined IS_IN_libpthread
  193. hidden_proto (__pthread_cleanup_upto)
  194. #endif
  195. /* Functions with versioned interfaces. */
  196. extern int __pthread_create_2_1 (pthread_t *newthread,
  197. const pthread_attr_t *attr,
  198. void *(*start_routine) (void *), void *arg);
  199. extern int __pthread_create_2_0 (pthread_t *newthread,
  200. const pthread_attr_t *attr,
  201. void *(*start_routine) (void *), void *arg);
  202. extern int __pthread_attr_init_2_1 (pthread_attr_t *attr);
  203. extern int __pthread_attr_init_2_0 (pthread_attr_t *attr);
  204. /* Event handlers for libthread_db interface. */
  205. extern void __nptl_create_event (void);
  206. extern void __nptl_death_event (void);
  207. hidden_proto (__nptl_create_event)
  208. hidden_proto (__nptl_death_event)
  209. /* Register the generation counter in the libpthread with the libc. */
  210. #ifdef TLS_MULTIPLE_THREADS_IN_TCB
  211. extern void __libc_pthread_init (unsigned long int *ptr,
  212. void (*reclaim) (void),
  213. const struct pthread_functions *functions)
  214. internal_function;
  215. #else
  216. extern int *__libc_pthread_init (unsigned long int *ptr,
  217. void (*reclaim) (void),
  218. const struct pthread_functions *functions)
  219. internal_function;
  220. /* Variable set to a nonzero value if more than one thread runs or ran. */
  221. extern int __pthread_multiple_threads attribute_hidden;
  222. /* Pointer to the corresponding variable in libc. */
  223. extern int *__libc_multiple_threads_ptr attribute_hidden;
  224. #endif
  225. /* Find a thread given its TID. */
  226. extern struct pthread *__find_thread_by_id (pid_t tid) attribute_hidden
  227. #ifdef SHARED
  228. ;
  229. #else
  230. weak_function;
  231. #define __find_thread_by_id(tid) \
  232. (__find_thread_by_id ? (__find_thread_by_id) (tid) : (struct pthread *) NULL)
  233. #endif
  234. extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
  235. /* Namespace save aliases. */
  236. extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
  237. struct sched_param *param);
  238. extern int __pthread_setschedparam (pthread_t thread_id, int policy,
  239. const struct sched_param *param);
  240. extern int __pthread_setcancelstate (int state, int *oldstate);
  241. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  242. __const pthread_mutexattr_t *__mutexattr);
  243. extern int __pthread_mutex_init_internal (pthread_mutex_t *__mutex,
  244. __const pthread_mutexattr_t *__mutexattr)
  245. attribute_hidden;
  246. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  247. extern int __pthread_mutex_destroy_internal (pthread_mutex_t *__mutex)
  248. attribute_hidden;
  249. extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
  250. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  251. extern int __pthread_mutex_lock_internal (pthread_mutex_t *__mutex)
  252. attribute_hidden;
  253. extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
  254. attribute_hidden internal_function;
  255. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  256. extern int __pthread_mutex_unlock_internal (pthread_mutex_t *__mutex)
  257. attribute_hidden;
  258. extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
  259. int __decr)
  260. attribute_hidden internal_function;
  261. extern int __pthread_mutexattr_init (pthread_mutexattr_t *attr);
  262. extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *attr);
  263. extern int __pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
  264. extern int __pthread_attr_destroy (pthread_attr_t *attr);
  265. extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
  266. int *detachstate);
  267. extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
  268. int detachstate);
  269. extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
  270. int *inherit);
  271. extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
  272. extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
  273. struct sched_param *param);
  274. extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
  275. const struct sched_param *param);
  276. extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
  277. int *policy);
  278. extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
  279. extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
  280. extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
  281. extern int __pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
  282. __attr, void **__restrict __stackaddr);
  283. extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
  284. void *__stackaddr);
  285. extern int __pthread_attr_getstacksize (__const pthread_attr_t *__restrict
  286. __attr,
  287. size_t *__restrict __stacksize);
  288. extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
  289. size_t __stacksize);
  290. extern int __pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
  291. void **__restrict __stackaddr,
  292. size_t *__restrict __stacksize);
  293. extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
  294. size_t __stacksize);
  295. extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
  296. __const pthread_rwlockattr_t *__restrict
  297. __attr);
  298. extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  299. extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  300. extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t *__rwlock);
  301. extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
  302. extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  303. extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t *__rwlock);
  304. extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
  305. extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  306. extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t *__rwlock);
  307. extern int __pthread_cond_broadcast (pthread_cond_t *cond);
  308. extern int __pthread_cond_destroy (pthread_cond_t *cond);
  309. extern int __pthread_cond_init (pthread_cond_t *cond,
  310. const pthread_condattr_t *cond_attr);
  311. extern int __pthread_cond_signal (pthread_cond_t *cond);
  312. extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
  313. extern int __pthread_cond_timedwait (pthread_cond_t *cond,
  314. pthread_mutex_t *mutex,
  315. const struct timespec *abstime);
  316. extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
  317. extern int __pthread_condattr_init (pthread_condattr_t *attr);
  318. extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
  319. extern int __pthread_key_create_internal (pthread_key_t *key,
  320. void (*destr) (void *));
  321. extern void *__pthread_getspecific (pthread_key_t key);
  322. extern void *__pthread_getspecific_internal (pthread_key_t key);
  323. extern int __pthread_setspecific (pthread_key_t key, const void *value);
  324. extern int __pthread_setspecific_internal (pthread_key_t key,
  325. const void *value);
  326. extern int __pthread_once (pthread_once_t *once_control,
  327. void (*init_routine) (void));
  328. extern int __pthread_once_internal (pthread_once_t *once_control,
  329. void (*init_routine) (void));
  330. extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
  331. void (*child) (void));
  332. extern pthread_t __pthread_self (void);
  333. extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
  334. extern int __pthread_kill (pthread_t threadid, int signo);
  335. extern void __pthread_exit (void *value);
  336. extern int __pthread_setcanceltype (int type, int *oldtype);
  337. extern int __pthread_enable_asynccancel (void) attribute_hidden;
  338. extern void __pthread_disable_asynccancel (int oldtype)
  339. internal_function attribute_hidden;
  340. extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
  341. extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
  342. extern int __pthread_cond_init_2_0 (pthread_cond_2_0_t *cond,
  343. const pthread_condattr_t *cond_attr);
  344. extern int __pthread_cond_signal_2_0 (pthread_cond_2_0_t *cond);
  345. extern int __pthread_cond_timedwait_2_0 (pthread_cond_2_0_t *cond,
  346. pthread_mutex_t *mutex,
  347. const struct timespec *abstime);
  348. extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond,
  349. pthread_mutex_t *mutex);
  350. extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize,
  351. cpu_set_t *cpuset);
  352. /* The two functions are in libc.so and not exported. */
  353. extern int __libc_enable_asynccancel (void) attribute_hidden;
  354. extern void __libc_disable_asynccancel (int oldtype)
  355. internal_function attribute_hidden;
  356. /* The two functions are in librt.so and not exported. */
  357. extern int __librt_enable_asynccancel (void) attribute_hidden;
  358. extern void __librt_disable_asynccancel (int oldtype)
  359. internal_function attribute_hidden;
  360. #ifdef IS_IN_libpthread
  361. /* Special versions which use non-exported functions. */
  362. extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
  363. void (*routine) (void *), void *arg)
  364. attribute_hidden;
  365. # undef pthread_cleanup_push
  366. # define pthread_cleanup_push(routine,arg) \
  367. { struct _pthread_cleanup_buffer _buffer; \
  368. __pthread_cleanup_push (&_buffer, (routine), (arg));
  369. extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
  370. int execute) attribute_hidden;
  371. # undef pthread_cleanup_pop
  372. # define pthread_cleanup_pop(execute) \
  373. __pthread_cleanup_pop (&_buffer, (execute)); }
  374. #endif
  375. extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
  376. void (*routine) (void *), void *arg);
  377. extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
  378. int execute);
  379. /* Old cleanup interfaces, still used in libc.so. */
  380. extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
  381. void (*routine) (void *), void *arg);
  382. extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
  383. int execute);
  384. extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
  385. void (*routine) (void *), void *arg);
  386. extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
  387. int execute);
  388. extern void __nptl_deallocate_tsd (void) attribute_hidden;
  389. extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
  390. #ifdef SHARED
  391. # define PTHREAD_STATIC_FN_REQUIRE(name)
  392. #else
  393. # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
  394. #endif
  395. #endif /* pthreadP.h */