internals.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 USE_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. #ifndef PAGE_SIZE
  153. #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
  154. #endif
  155. /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
  156. #ifndef INITIAL_STACK_SIZE
  157. #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
  158. #endif
  159. /* Size of the thread manager stack. The "- 32" avoids wasting space
  160. with some malloc() implementations. */
  161. #ifndef THREAD_MANAGER_STACK_SIZE
  162. #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
  163. #endif
  164. /* The base of the "array" of thread stacks. The array will grow down from
  165. here. Defaults to the calculated bottom of the initial application
  166. stack. */
  167. #ifndef THREAD_STACK_START_ADDRESS
  168. #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
  169. #endif
  170. /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
  171. architecture doesn't need a memory barrier instruction (e.g. Intel
  172. x86). Still we need the compiler to respect the barrier and emit
  173. all outstanding operations which modify memory. Some architectures
  174. distinguish between full, read and write barriers. */
  175. #ifndef MEMORY_BARRIER
  176. #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
  177. #endif
  178. #ifndef READ_MEMORY_BARRIER
  179. #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
  180. #endif
  181. #ifndef WRITE_MEMORY_BARRIER
  182. #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
  183. #endif
  184. /* Max number of times we must spin on a spinlock calling sched_yield().
  185. After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
  186. #ifndef MAX_SPIN_COUNT
  187. #define MAX_SPIN_COUNT 50
  188. #endif
  189. /* Max number of times the spinlock in the adaptive mutex implementation
  190. spins actively on SMP systems. */
  191. #ifndef MAX_ADAPTIVE_SPIN_COUNT
  192. #define MAX_ADAPTIVE_SPIN_COUNT 100
  193. #endif
  194. /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
  195. after MAX_SPIN_COUNT iterations of sched_yield().
  196. With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
  197. (Otherwise the kernel does busy-waiting for realtime threads,
  198. giving other threads no chance to run.) */
  199. #ifndef SPIN_SLEEP_DURATION
  200. #define SPIN_SLEEP_DURATION 2000001
  201. #endif
  202. /* Defined and used in libc.so. */
  203. extern int __libc_multiple_threads attribute_hidden;
  204. extern int __librt_multiple_threads;
  205. /* Debugging */
  206. #ifdef DEBUG
  207. #include <assert.h>
  208. #define ASSERT assert
  209. #define MSG __pthread_message
  210. #else
  211. #define ASSERT(x)
  212. #define MSG(msg,arg...)
  213. #endif
  214. /* Internal global functions */
  215. extern void __pthread_do_exit (void *retval, char *currentframe)
  216. __attribute__ ((__noreturn__));
  217. extern void __pthread_destroy_specifics (void);
  218. extern void __pthread_perform_cleanup (char *currentframe);
  219. extern void __pthread_init_max_stacksize (void);
  220. extern int __pthread_initialize_manager (void);
  221. extern void __pthread_message (const char * fmt, ...);
  222. extern int __pthread_manager (void *reqfd);
  223. extern int __pthread_manager_event (void *reqfd);
  224. extern void __pthread_manager_sighandler (int sig);
  225. extern void __pthread_reset_main_thread (void);
  226. extern void __pthread_once_fork_prepare (void);
  227. extern void __pthread_once_fork_parent (void);
  228. extern void __pthread_once_fork_child (void);
  229. extern void __flockfilelist (void);
  230. extern void __funlockfilelist (void);
  231. extern void __fresetlockfiles (void);
  232. extern void __pthread_manager_adjust_prio (int thread_prio);
  233. extern void __pthread_initialize_minimal (void);
  234. extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
  235. size_t __guardsize);
  236. extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
  237. size_t *__guardsize);
  238. #if 0 /* uClibc: deprecated stuff disabled */
  239. extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
  240. void *__stackaddr);
  241. extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
  242. void **__stackaddr);
  243. #endif
  244. extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
  245. size_t __stacksize);
  246. extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
  247. size_t *__stacksize);
  248. extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
  249. size_t __stacksize);
  250. extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
  251. size_t *__stacksize);
  252. extern int __pthread_attr_destroy (pthread_attr_t *attr);
  253. extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
  254. int detachstate);
  255. extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
  256. int *detachstate);
  257. extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
  258. const struct sched_param *param);
  259. extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
  260. struct sched_param *param);
  261. extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
  262. extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
  263. int *policy);
  264. extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
  265. extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
  266. int *inherit);
  267. extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
  268. extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
  269. extern int __pthread_getconcurrency (void);
  270. extern int __pthread_setconcurrency (int __level);
  271. extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
  272. const struct timespec *__abstime);
  273. extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
  274. int *__pshared);
  275. extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
  276. int __pshared);
  277. extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
  278. int *__kind);
  279. extern void __pthread_kill_other_threads_np (void);
  280. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  281. __const pthread_mutexattr_t *__mutex_attr);
  282. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  283. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  284. extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
  285. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  286. extern int __pthread_cond_init (pthread_cond_t *cond,
  287. const pthread_condattr_t *cond_attr);
  288. extern int __pthread_cond_destroy (pthread_cond_t *cond);
  289. extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
  290. extern int __pthread_cond_timedwait (pthread_cond_t *cond,
  291. pthread_mutex_t *mutex,
  292. const struct timespec *abstime);
  293. extern int __pthread_cond_signal (pthread_cond_t *cond);
  294. extern int __pthread_cond_broadcast (pthread_cond_t *cond);
  295. extern int __pthread_condattr_init (pthread_condattr_t *attr);
  296. extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
  297. extern pthread_t __pthread_self (void);
  298. extern pthread_descr __pthread_thread_self (void);
  299. extern pthread_descr __pthread_self_stack (void) attribute_hidden;
  300. extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
  301. extern void __pthread_exit (void *retval)
  302. #if defined NOT_IN_libc && defined IS_IN_libpthread
  303. attribute_noreturn
  304. #endif
  305. ;
  306. extern int __pthread_getschedparam (pthread_t thread, int *policy,
  307. struct sched_param *param);
  308. extern int __pthread_setschedparam (pthread_t thread, int policy,
  309. const struct sched_param *param);
  310. extern int __pthread_setcancelstate (int state, int * oldstate);
  311. extern int __pthread_setcanceltype (int type, int * oldtype);
  312. extern void __pthread_restart_old(pthread_descr th);
  313. extern void __pthread_suspend_old(pthread_descr self);
  314. extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime);
  315. extern void __pthread_restart_new(pthread_descr th);
  316. extern void __pthread_suspend_new(pthread_descr self);
  317. extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime);
  318. extern void __pthread_wait_for_restart_signal(pthread_descr self);
  319. extern void __pthread_sigsuspend (const sigset_t *mask) attribute_hidden;
  320. extern int __pthread_yield (void);
  321. extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
  322. __const struct timespec *__restrict
  323. __abstime);
  324. extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
  325. __const struct timespec *__restrict
  326. __abstime);
  327. extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
  328. extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
  329. __restrict __attr,
  330. int *__restrict __pshared);
  331. extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
  332. extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
  333. extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
  334. extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
  335. extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
  336. /* Global pointers to old or new suspend functions */
  337. extern void (*__pthread_restart)(pthread_descr);
  338. extern void (*__pthread_suspend)(pthread_descr);
  339. extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
  340. /* Prototypes for the function without cancelation support when the
  341. normal version has it. */
  342. extern int __libc_close (int fd);
  343. extern int __libc_nanosleep (const struct timespec *requested_time,
  344. struct timespec *remaining);
  345. /* Prototypes for some of the new semaphore functions. */
  346. extern int sem_post (sem_t * sem);
  347. extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value);
  348. extern int sem_wait (sem_t *__sem);
  349. extern int sem_trywait (sem_t *__sem);
  350. extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
  351. extern int sem_destroy (sem_t *__sem);
  352. /* Prototypes for compatibility functions. */
  353. extern int __pthread_attr_init (pthread_attr_t *__attr);
  354. extern int __pthread_create (pthread_t *__restrict __threadp,
  355. const pthread_attr_t *__attr,
  356. void *(*__start_routine) (void *),
  357. void *__restrict __arg);
  358. /* The functions called the signal events. */
  359. extern void __linuxthreads_create_event (void);
  360. extern void __linuxthreads_death_event (void);
  361. extern void __linuxthreads_reap_event (void);
  362. /* This function is called to initialize the pthread library. */
  363. extern void __pthread_initialize (void);
  364. /* TSD. */
  365. extern int __pthread_internal_tsd_set (int key, const void * pointer);
  366. extern void * __pthread_internal_tsd_get (int key);
  367. extern void ** __attribute__ ((__const__))
  368. __pthread_internal_tsd_address (int key);
  369. /* Sighandler wrappers. */
  370. extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
  371. extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
  372. struct ucontext *uc);
  373. extern void __pthread_null_sighandler(int sig);
  374. extern int __pthread_sigaction (int sig, const struct sigaction *act,
  375. struct sigaction *oact);
  376. extern int __pthread_sigwait (const sigset_t *set, int *sig);
  377. extern int __pthread_raise (int sig);
  378. /* Cancellation. */
  379. extern int __pthread_enable_asynccancel (void) attribute_hidden;
  380. extern void __pthread_disable_asynccancel (int oldtype)
  381. internal_function attribute_hidden;
  382. /* The two functions are in libc.so and not exported. */
  383. extern int __libc_enable_asynccancel (void) attribute_hidden;
  384. extern void __libc_disable_asynccancel (int oldtype)
  385. internal_function attribute_hidden;
  386. /* The two functions are in libc.so and are exported. */
  387. extern int __librt_enable_asynccancel (void);
  388. extern void __librt_disable_asynccancel (int oldtype) internal_function;
  389. extern void __pthread_cleanup_upto (__jmp_buf target,
  390. char *targetframe) attribute_hidden;
  391. extern pid_t __pthread_fork (struct fork_block *b) attribute_hidden;
  392. #define asm_handle(name) _asm_handle(name)
  393. #define _asm_handle(name) #name
  394. #define ASM_GLOBAL asm_handle(ASM_GLOBAL_DIRECTIVE)
  395. #define ASM_CANCEL(name) asm_handle(C_SYMBOL_NAME(name))
  396. #if !defined NOT_IN_libc
  397. # define LIBC_CANCEL_ASYNC() \
  398. __libc_enable_asynccancel ()
  399. # define LIBC_CANCEL_RESET(oldtype) \
  400. __libc_disable_asynccancel (oldtype)
  401. # define LIBC_CANCEL_HANDLED() \
  402. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_enable_asynccancel)); \
  403. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_disable_asynccancel))
  404. #elif defined IS_IN_libpthread
  405. # define LIBC_CANCEL_ASYNC() \
  406. __pthread_enable_asynccancel ()
  407. # define LIBC_CANCEL_RESET(oldtype) \
  408. __pthread_disable_asynccancel (oldtype)
  409. # define LIBC_CANCEL_HANDLED() \
  410. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_enable_asynccancel)); \
  411. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_disable_asynccancel))
  412. #elif defined IS_IN_librt
  413. # define LIBC_CANCEL_ASYNC() \
  414. __librt_enable_asynccancel ()
  415. # define LIBC_CANCEL_RESET(oldtype) \
  416. __librt_disable_asynccancel (oldtype)
  417. # define LIBC_CANCEL_HANDLED() \
  418. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_enable_asynccancel)); \
  419. __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_disable_asynccancel))
  420. #else
  421. # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
  422. # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
  423. # define LIBC_CANCEL_HANDLED() /* Nothing. */
  424. #endif
  425. extern int * __libc_pthread_init (const struct pthread_functions *functions);
  426. #if !defined NOT_IN_libc && !defined FLOATING_STACKS
  427. # ifdef SHARED
  428. # define thread_self() \
  429. (*__libc_pthread_functions.ptr_pthread_thread_self) ()
  430. # else
  431. weak_extern (__pthread_thread_self)
  432. # define thread_self() __pthread_thread_self ()
  433. # endif
  434. #endif
  435. #ifndef USE_TLS
  436. # define __manager_thread (&__pthread_manager_thread)
  437. #else
  438. # define __manager_thread __pthread_manager_threadp
  439. #endif
  440. extern __always_inline pthread_descr
  441. check_thread_self (void);
  442. extern __always_inline pthread_descr
  443. check_thread_self (void)
  444. {
  445. pthread_descr self = thread_self ();
  446. #if defined THREAD_SELF && defined INIT_THREAD_SELF
  447. if (self == __manager_thread)
  448. {
  449. /* A new thread might get a cancel signal before it is fully
  450. initialized, so that the thread register might still point to the
  451. manager thread. Double check that this is really the manager
  452. thread. */
  453. self = __pthread_self_stack();
  454. if (self != __manager_thread)
  455. /* Oops, thread_self() isn't working yet.. */
  456. INIT_THREAD_SELF(self, self->p_nr);
  457. }
  458. #endif
  459. return self;
  460. }
  461. #endif /* internals.h */