internals.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. #ifndef _INTERNALS_H
  15. #define _INTERNALS_H 1
  16. #include "uClibc-glue.h"
  17. /* Internal data structures */
  18. /* Includes */
  19. #include <limits.h>
  20. #include <signal.h>
  21. #include <stdbool.h>
  22. #include <unistd.h>
  23. #include <sys/ucontext.h>
  24. #include <bits/stackinfo.h>
  25. #include <bits/sigcontextinfo.h>
  26. #include <bits/pthreadtypes.h>
  27. #ifdef __UCLIBC_HAS_TLS__
  28. #include <tls.h>
  29. #endif
  30. #include "descr.h"
  31. #include "semaphore.h"
  32. #include <pthread-functions.h>
  33. #ifndef THREAD_GETMEM
  34. # define THREAD_GETMEM(descr, member) descr->member
  35. #endif
  36. #ifndef THREAD_GETMEM_NC
  37. # define THREAD_GETMEM_NC(descr, member) descr->member
  38. #endif
  39. #ifndef THREAD_SETMEM
  40. # define THREAD_SETMEM(descr, member, value) descr->member = (value)
  41. #endif
  42. #ifndef THREAD_SETMEM_NC
  43. # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
  44. #endif
  45. #if !defined NOT_IN_libc && defined FLOATING_STACKS
  46. # define LIBC_THREAD_GETMEM(descr, member) THREAD_GETMEM (descr, member)
  47. # define LIBC_THREAD_SETMEM(descr, member, value) \
  48. THREAD_SETMEM (descr, member, value)
  49. #else
  50. # define LIBC_THREAD_GETMEM(descr, member) descr->member
  51. # define LIBC_THREAD_SETMEM(descr, member, value) descr->member = (value)
  52. #endif
  53. typedef void (*destr_function)(void *);
  54. struct pthread_key_struct {
  55. int in_use; /* already allocated? */
  56. destr_function destr; /* destruction routine */
  57. };
  58. #define PTHREAD_START_ARGS_INITIALIZER(fct) \
  59. { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
  60. /* The type of thread handles. */
  61. typedef struct pthread_handle_struct * pthread_handle;
  62. struct pthread_handle_struct {
  63. struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
  64. pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
  65. char * h_bottom; /* Lowest address in the stack thread */
  66. };
  67. /* The type of messages sent to the thread manager thread */
  68. struct pthread_request {
  69. pthread_descr req_thread; /* Thread doing the request */
  70. enum { /* Request kind */
  71. REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
  72. REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD
  73. } req_kind;
  74. union { /* Arguments for request */
  75. struct { /* For REQ_CREATE: */
  76. const pthread_attr_t * attr; /* thread attributes */
  77. void * (*fn)(void *); /* start function */
  78. void * arg; /* argument to start function */
  79. sigset_t mask; /* signal mask */
  80. } create;
  81. struct { /* For REQ_FREE: */
  82. pthread_t thread_id; /* identifier of thread to free */
  83. } free;
  84. struct { /* For REQ_PROCESS_EXIT: */
  85. int code; /* exit status */
  86. } exit;
  87. void * post; /* For REQ_POST: the semaphore */
  88. struct { /* For REQ_FOR_EACH_THREAD: callback */
  89. void (*fn)(void *, pthread_descr);
  90. void *arg;
  91. } for_each;
  92. } req_args;
  93. };
  94. typedef void (*arch_sighandler_t) (int, SIGCONTEXT);
  95. union sighandler
  96. {
  97. arch_sighandler_t old;
  98. void (*rt) (int, struct siginfo *, struct ucontext *);
  99. };
  100. extern union sighandler __sighandler[NSIG];
  101. /* Signals used for suspend/restart and for cancellation notification. */
  102. extern int __pthread_sig_restart;
  103. extern int __pthread_sig_cancel;
  104. /* Signal used for interfacing with gdb */
  105. extern int __pthread_sig_debug;
  106. /* Global array of thread handles, used for validating a thread id
  107. and retrieving the corresponding thread descriptor. Also used for
  108. mapping the available stack segments. */
  109. extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
  110. /* Descriptor of the main thread */
  111. extern pthread_descr __pthread_main_thread;
  112. /* File descriptor for sending requests to the thread manager.
  113. Initially -1, meaning that __pthread_initialize_manager must be called. */
  114. extern int __pthread_manager_request;
  115. /* Other end of the pipe for sending requests to the thread manager. */
  116. extern int __pthread_manager_reader;
  117. #ifdef FLOATING_STACKS
  118. /* Maximum stack size. */
  119. extern size_t __pthread_max_stacksize;
  120. #endif
  121. /* Pending request for a process-wide exit */
  122. extern int __pthread_exit_requested, __pthread_exit_code;
  123. /* Set to 1 by gdb if we're debugging */
  124. extern __volatile__ int __pthread_threads_debug;
  125. /* Globally enabled events. */
  126. extern __volatile__ td_thr_events_t __pthread_threads_events;
  127. /* Pointer to descriptor of thread with last event. */
  128. extern __volatile__ pthread_descr __pthread_last_event;
  129. /* Flag which tells whether we are executing on SMP kernel. */
  130. extern int __pthread_smp_kernel;
  131. /* Return the handle corresponding to a thread id */
  132. static __inline__ pthread_handle thread_handle(pthread_t id)
  133. {
  134. return &__pthread_handles[id % PTHREAD_THREADS_MAX];
  135. }
  136. /* Validate a thread handle. Must have acquired h->h_spinlock before. */
  137. static __inline__ int invalid_handle(pthread_handle h, pthread_t id)
  138. {
  139. return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
  140. }
  141. static __inline__ int nonexisting_handle(pthread_handle h, pthread_t id)
  142. {
  143. return h->h_descr == NULL || h->h_descr->p_tid != id;
  144. }
  145. /* Fill in defaults left unspecified by pt-machine.h. */
  146. /* We round up a value with page size. */
  147. #ifndef page_roundup
  148. #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
  149. #endif
  150. /* The page size we can get from the system. This should likely not be
  151. changed by the machine file but, you never know. */
  152. #define __PAGE_SIZE (sysconf (_SC_PAGESIZE))
  153. /* The initial size of the thread stack. Must be a multiple of __PAGE_SIZE. */
  154. #ifndef INITIAL_STACK_SIZE
  155. #define INITIAL_STACK_SIZE (4 * __PAGE_SIZE)
  156. #endif
  157. /* Size of the thread manager stack. The "- 32" avoids wasting space
  158. with some malloc() implementations. */
  159. #ifndef THREAD_MANAGER_STACK_SIZE
  160. #define THREAD_MANAGER_STACK_SIZE (2 * __PAGE_SIZE - 32)
  161. #endif
  162. /* The base of the "array" of thread stacks. The array will grow down from
  163. here. Defaults to the calculated bottom of the initial application
  164. stack. */
  165. #ifndef THREAD_STACK_START_ADDRESS
  166. #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
  167. #endif
  168. /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
  169. architecture doesn't need a memory barrier instruction (e.g. Intel
  170. x86). Still we need the compiler to respect the barrier and emit
  171. all outstanding operations which modify memory. Some architectures
  172. distinguish between full, read and write barriers. */
  173. #ifndef MEMORY_BARRIER
  174. #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
  175. #endif
  176. #ifndef READ_MEMORY_BARRIER
  177. #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
  178. #endif
  179. #ifndef WRITE_MEMORY_BARRIER
  180. #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
  181. #endif
  182. /* Max number of times we must spin on a spinlock calling sched_yield().
  183. After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
  184. #ifndef MAX_SPIN_COUNT
  185. #define MAX_SPIN_COUNT 50
  186. #endif
  187. /* Max number of times the spinlock in the adaptive mutex implementation
  188. spins actively on SMP systems. */
  189. #ifndef MAX_ADAPTIVE_SPIN_COUNT
  190. #define MAX_ADAPTIVE_SPIN_COUNT 100
  191. #endif
  192. /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
  193. after MAX_SPIN_COUNT iterations of sched_yield().
  194. With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
  195. (Otherwise the kernel does busy-waiting for realtime threads,
  196. giving other threads no chance to run.) */
  197. #ifndef SPIN_SLEEP_DURATION
  198. #define SPIN_SLEEP_DURATION 2000001
  199. #endif
  200. /* Defined and used in libc.so. */
  201. extern int __libc_multiple_threads attribute_hidden;
  202. extern int __librt_multiple_threads;
  203. /* Debugging */
  204. #ifdef DEBUG
  205. #include <assert.h>
  206. #define ASSERT assert
  207. #define MSG __pthread_message
  208. #else
  209. #define ASSERT(x)
  210. #define MSG(msg,arg...)
  211. #endif
  212. /* Internal global functions */
  213. extern void __pthread_do_exit (void *retval, char *currentframe)
  214. __attribute__ ((__noreturn__));
  215. extern void __pthread_destroy_specifics (void);
  216. extern void __pthread_perform_cleanup (char *currentframe);
  217. extern void __pthread_init_max_stacksize (void);
  218. extern int __pthread_initialize_manager (void);
  219. extern void __pthread_message (const char * fmt, ...) attribute_hidden;
  220. extern int __pthread_manager (void *reqfd);
  221. extern int __pthread_manager_event (void *reqfd);
  222. extern void __pthread_manager_sighandler (int sig);
  223. extern void __pthread_reset_main_thread (void);
  224. extern void __pthread_once_fork_prepare (void);
  225. extern void __pthread_once_fork_parent (void);
  226. extern void __pthread_once_fork_child (void);
  227. extern void __flockfilelist (void);
  228. extern void __funlockfilelist (void);
  229. extern void __fresetlockfiles (void);
  230. extern void __pthread_manager_adjust_prio (int thread_prio);
  231. extern void __pthread_initialize_minimal (void);
  232. extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
  233. size_t __guardsize);
  234. extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
  235. size_t *__guardsize);
  236. #if 0 /* uClibc: deprecated stuff disabled */
  237. extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
  238. void *__stackaddr);
  239. extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
  240. void **__stackaddr);
  241. #endif
  242. extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
  243. size_t __stacksize);
  244. extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
  245. size_t *__stacksize);
  246. extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
  247. size_t __stacksize);
  248. extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
  249. size_t *__stacksize);
  250. extern int __pthread_attr_destroy (pthread_attr_t *attr);
  251. extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
  252. int detachstate);
  253. extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
  254. int *detachstate);
  255. extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
  256. const struct sched_param *param);
  257. extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
  258. struct sched_param *param);
  259. extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
  260. extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
  261. int *policy);
  262. extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
  263. extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
  264. int *inherit);
  265. extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
  266. extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
  267. extern int __pthread_getconcurrency (void);
  268. extern int __pthread_setconcurrency (int __level);
  269. extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
  270. const struct timespec *__abstime);
  271. extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
  272. int *__pshared);
  273. extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
  274. int __pshared);
  275. extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
  276. int *__kind);
  277. extern void __pthread_kill_other_threads_np (void);
  278. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  279. const pthread_mutexattr_t *__mutex_attr);
  280. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  281. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  282. extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
  283. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  284. extern int __pthread_cond_init (pthread_cond_t *cond,
  285. const pthread_condattr_t *cond_attr);
  286. extern int __pthread_cond_destroy (pthread_cond_t *cond);
  287. extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
  288. extern int __pthread_cond_timedwait (pthread_cond_t *cond,
  289. pthread_mutex_t *mutex,
  290. const struct timespec *abstime);
  291. extern int __pthread_cond_signal (pthread_cond_t *cond);
  292. extern int __pthread_cond_broadcast (pthread_cond_t *cond);
  293. extern int __pthread_condattr_init (pthread_condattr_t *attr);
  294. extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
  295. extern pthread_t __pthread_self (void);
  296. extern pthread_descr __pthread_thread_self (void);
  297. extern pthread_descr __pthread_self_stack (void) attribute_hidden;
  298. extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
  299. extern void __pthread_exit (void *retval)
  300. #if defined NOT_IN_libc && defined IS_IN_libpthread
  301. attribute_noreturn
  302. #endif
  303. ;
  304. extern int __pthread_getschedparam (pthread_t thread, int *policy,
  305. struct sched_param *param);
  306. extern int __pthread_setschedparam (pthread_t thread, int policy,
  307. const struct sched_param *param);
  308. extern int __pthread_setcancelstate (int state, int * oldstate);
  309. extern int __pthread_setcanceltype (int type, int * oldtype);
  310. extern void __pthread_restart_old(pthread_descr th);
  311. extern void __pthread_suspend_old(pthread_descr self);
  312. extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime);
  313. extern void __pthread_restart_new(pthread_descr th);
  314. extern void __pthread_suspend_new(pthread_descr self);
  315. extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime);
  316. extern void __pthread_wait_for_restart_signal(pthread_descr self);
  317. extern void __pthread_sigsuspend (const sigset_t *mask) attribute_hidden;
  318. extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
  319. const struct timespec *__restrict
  320. __abstime);
  321. extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
  322. const struct timespec *__restrict
  323. __abstime);
  324. extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
  325. extern int __pthread_barrierattr_getpshared (const pthread_barrierattr_t *
  326. __restrict __attr,
  327. int *__restrict __pshared);
  328. extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
  329. extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
  330. extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
  331. extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
  332. extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
  333. /* Global pointers to old or new suspend functions */
  334. extern void (*__pthread_restart)(pthread_descr);
  335. extern void (*__pthread_suspend)(pthread_descr);
  336. extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
  337. /* Prototypes for some of the new semaphore functions. */
  338. extern int sem_post (sem_t * sem);
  339. extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value);
  340. extern int sem_wait (sem_t *__sem);
  341. extern int sem_trywait (sem_t *__sem);
  342. extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
  343. extern int sem_destroy (sem_t *__sem);
  344. /* Prototypes for compatibility functions. */
  345. extern int __pthread_attr_init (pthread_attr_t *__attr);
  346. extern int __pthread_create (pthread_t *__restrict __threadp,
  347. const pthread_attr_t *__attr,
  348. void *(*__start_routine) (void *),
  349. void *__restrict __arg);
  350. /* The functions called the signal events. */
  351. extern void __linuxthreads_create_event (void);
  352. extern void __linuxthreads_death_event (void);
  353. extern void __linuxthreads_reap_event (void);
  354. /* This function is called to initialize the pthread library. */
  355. extern void __pthread_initialize (void);
  356. /* TSD. */
  357. #if !defined __UCLIBC_HAS_TLS__ && defined __UCLIBC_HAS_RPC__
  358. extern int __pthread_internal_tsd_set (int key, const void * pointer);
  359. extern void * __pthread_internal_tsd_get (int key);
  360. extern void ** __attribute__ ((__const__))
  361. __pthread_internal_tsd_address (int key);
  362. #endif
  363. /* Sighandler wrappers. */
  364. extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
  365. extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
  366. struct ucontext *uc);
  367. extern void __pthread_null_sighandler(int sig);
  368. extern int __pthread_sigaction (int sig, const struct sigaction *act,
  369. struct sigaction *oact);
  370. extern int __pthread_sigwait (const sigset_t *set, int *sig);
  371. extern int __pthread_raise (int sig);
  372. /* Cancellation. */
  373. extern int __pthread_enable_asynccancel (void) attribute_hidden;
  374. extern void __pthread_disable_asynccancel (int oldtype)
  375. internal_function attribute_hidden;
  376. /* The two functions are in libc.so and not exported. */
  377. extern int __libc_enable_asynccancel (void) attribute_hidden;
  378. extern void __libc_disable_asynccancel (int oldtype)
  379. internal_function attribute_hidden;
  380. /* The two functions are in libc.so and are exported. */
  381. extern int __librt_enable_asynccancel (void);
  382. extern void __librt_disable_asynccancel (int oldtype) internal_function;
  383. extern void __pthread_cleanup_upto (__jmp_buf target,
  384. char *targetframe) attribute_hidden;
  385. extern pid_t __pthread_fork (struct fork_block *b) attribute_hidden;
  386. #define asm_handle(name) _asm_handle(name)
  387. #define _asm_handle(name) #name
  388. #define ASM_GLOBAL asm_handle(ASM_GLOBAL_DIRECTIVE)
  389. #define ASM_CANCEL(name) asm_handle(C_SYMBOL_NAME(name))
  390. #if !defined NOT_IN_libc
  391. # define LIBC_CANCEL_ASYNC() \
  392. __libc_enable_asynccancel ()
  393. # define LIBC_CANCEL_RESET(oldtype) \
  394. __libc_disable_asynccancel (oldtype)
  395. # define LIBC_CANCEL_HANDLED() \
  396. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_enable_asynccancel)); \
  397. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_disable_asynccancel))
  398. #elif defined IS_IN_libpthread
  399. # define LIBC_CANCEL_ASYNC() \
  400. __pthread_enable_asynccancel ()
  401. # define LIBC_CANCEL_RESET(oldtype) \
  402. __pthread_disable_asynccancel (oldtype)
  403. # define LIBC_CANCEL_HANDLED() \
  404. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_enable_asynccancel)); \
  405. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_disable_asynccancel))
  406. #elif defined IS_IN_librt
  407. # define LIBC_CANCEL_ASYNC() \
  408. __librt_enable_asynccancel ()
  409. # define LIBC_CANCEL_RESET(oldtype) \
  410. __librt_disable_asynccancel (oldtype)
  411. # define LIBC_CANCEL_HANDLED() \
  412. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_enable_asynccancel)); \
  413. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_disable_asynccancel))
  414. #else
  415. # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
  416. # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
  417. # define LIBC_CANCEL_HANDLED() /* Nothing. */
  418. #endif
  419. #if !defined NOT_IN_libc && !defined FLOATING_STACKS
  420. # ifdef SHARED
  421. # define thread_self() \
  422. (*__libc_pthread_functions.ptr_pthread_thread_self) ()
  423. # else
  424. weak_extern (__pthread_thread_self)
  425. # define thread_self() __pthread_thread_self ()
  426. # endif
  427. #endif
  428. #ifndef __UCLIBC_HAS_TLS__
  429. # define __manager_thread (&__pthread_manager_thread)
  430. #else
  431. # define __manager_thread __pthread_manager_threadp
  432. #endif
  433. static __always_inline pthread_descr
  434. check_thread_self (void);
  435. static __always_inline pthread_descr
  436. check_thread_self (void)
  437. {
  438. pthread_descr self = thread_self ();
  439. #if defined THREAD_SELF && defined INIT_THREAD_SELF
  440. if (self == __manager_thread)
  441. {
  442. /* A new thread might get a cancel signal before it is fully
  443. initialized, so that the thread register might still point to the
  444. manager thread. Double check that this is really the manager
  445. thread. */
  446. self = __pthread_self_stack();
  447. if (self != __manager_thread)
  448. /* Oops, thread_self() isn't working yet.. */
  449. INIT_THREAD_SELF(self, self->p_nr);
  450. }
  451. #endif
  452. return self;
  453. }
  454. #endif /* internals.h */