internals.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /* Internal data structures */
  17. /* Includes */
  18. #include <features.h>
  19. #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
  20. #include <limits.h>
  21. #include <setjmp.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include <bits/stackinfo.h>
  25. #include <sys/types.h>
  26. #include "pt-machine.h"
  27. #include <ucontext.h>
  28. #include <bits/sigcontextinfo.h>
  29. #include "semaphore.h"
  30. #include "descr.h"
  31. #ifdef __UCLIBC_HAS_XLOCALE__
  32. #include <bits/uClibc_locale.h>
  33. #endif /* __UCLIBC_HAS_XLOCALE__ */
  34. /* Use a funky version in a probably vein attempt at preventing gdb
  35. * from dlopen()'ing glibc's libthread_db library... */
  36. #define STRINGIFY(s) STRINGIFY2 (s)
  37. #define STRINGIFY2(s) #s
  38. #define VERSION STRINGIFY(__UCLIBC_MAJOR__) "." STRINGIFY(__UCLIBC_MINOR__) "." STRINGIFY(__UCLIBC_SUBLEVEL__)
  39. #ifndef THREAD_GETMEM
  40. # define THREAD_GETMEM(descr, member) descr->member
  41. #endif
  42. #ifndef THREAD_GETMEM_NC
  43. # define THREAD_GETMEM_NC(descr, member) descr->member
  44. #endif
  45. #ifndef THREAD_SETMEM
  46. # define THREAD_SETMEM(descr, member, value) descr->member = (value)
  47. #endif
  48. #ifndef THREAD_SETMEM_NC
  49. # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
  50. #endif
  51. #if !defined NOT_IN_libc && defined FLOATING_STACKS
  52. # define LIBC_THREAD_GETMEM(descr, member) THREAD_GETMEM (descr, member)
  53. # define LIBC_THREAD_SETMEM(descr, member, value) \
  54. THREAD_SETMEM (descr, member, value)
  55. #else
  56. # define LIBC_THREAD_GETMEM(descr, member) descr->member
  57. # define LIBC_THREAD_SETMEM(descr, member, value) descr->member = (value)
  58. #endif
  59. typedef void (*destr_function)(void *);
  60. struct pthread_key_struct {
  61. int in_use; /* already allocated? */
  62. destr_function destr; /* destruction routine */
  63. };
  64. #define PTHREAD_START_ARGS_INITIALIZER(fct) \
  65. { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
  66. /* The type of thread handles. */
  67. typedef struct pthread_handle_struct * pthread_handle;
  68. struct pthread_handle_struct {
  69. struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
  70. pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
  71. char * h_bottom; /* Lowest address in the stack thread */
  72. };
  73. /* The type of messages sent to the thread manager thread */
  74. struct pthread_request {
  75. pthread_descr req_thread; /* Thread doing the request */
  76. enum { /* Request kind */
  77. REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
  78. REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD
  79. } req_kind;
  80. union { /* Arguments for request */
  81. struct { /* For REQ_CREATE: */
  82. const pthread_attr_t * attr; /* thread attributes */
  83. void * (*fn)(void *); /* start function */
  84. void * arg; /* argument to start function */
  85. sigset_t mask; /* signal mask */
  86. } create;
  87. struct { /* For REQ_FREE: */
  88. pthread_t thread_id; /* identifier of thread to free */
  89. } free;
  90. struct { /* For REQ_PROCESS_EXIT: */
  91. int code; /* exit status */
  92. } exit;
  93. void * post; /* For REQ_POST: the semaphore */
  94. struct { /* For REQ_FOR_EACH_THREAD: callback */
  95. void (*fn)(void *, pthread_descr);
  96. void *arg;
  97. } for_each;
  98. } req_args;
  99. };
  100. typedef void (*arch_sighandler_t) (int, SIGCONTEXT);
  101. union sighandler
  102. {
  103. arch_sighandler_t old;
  104. void (*rt) (int, struct siginfo *, struct ucontext *);
  105. };
  106. extern union sighandler __sighandler[NSIG];
  107. /* Signals used for suspend/restart and for cancellation notification. */
  108. extern int __pthread_sig_restart;
  109. extern int __pthread_sig_cancel;
  110. /* Signal used for interfacing with gdb */
  111. extern int __pthread_sig_debug;
  112. /* Global array of thread handles, used for validating a thread id
  113. and retrieving the corresponding thread descriptor. Also used for
  114. mapping the available stack segments. */
  115. extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
  116. /* Descriptor of the main thread */
  117. extern pthread_descr __pthread_main_thread;
  118. /* File descriptor for sending requests to the thread manager.
  119. Initially -1, meaning that __pthread_initialize_manager must be called. */
  120. extern int __pthread_manager_request;
  121. /* Other end of the pipe for sending requests to the thread manager. */
  122. extern int __pthread_manager_reader;
  123. #ifdef FLOATING_STACKS
  124. /* Maximum stack size. */
  125. extern size_t __pthread_max_stacksize;
  126. #endif
  127. /* Pending request for a process-wide exit */
  128. extern int __pthread_exit_requested, __pthread_exit_code;
  129. /* Set to 1 by gdb if we're debugging */
  130. extern volatile int __pthread_threads_debug;
  131. /* Globally enabled events. */
  132. extern volatile td_thr_events_t __pthread_threads_events;
  133. /* Pointer to descriptor of thread with last event. */
  134. extern volatile pthread_descr __pthread_last_event;
  135. /* Flag which tells whether we are executing on SMP kernel. */
  136. extern int __pthread_smp_kernel;
  137. /* Return the handle corresponding to a thread id */
  138. static inline pthread_handle thread_handle(pthread_t id)
  139. {
  140. return &__pthread_handles[id % PTHREAD_THREADS_MAX];
  141. }
  142. /* Validate a thread handle. Must have acquired h->h_spinlock before. */
  143. static inline int invalid_handle(pthread_handle h, pthread_t id)
  144. {
  145. return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
  146. }
  147. static inline int nonexisting_handle(pthread_handle h, pthread_t id)
  148. {
  149. return h->h_descr == NULL || h->h_descr->p_tid != id;
  150. }
  151. /* Fill in defaults left unspecified by pt-machine.h. */
  152. /* We round up a value with page size. */
  153. #ifndef page_roundup
  154. #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
  155. #endif
  156. /* The page size we can get from the system. This should likely not be
  157. changed by the machine file but, you never know. */
  158. extern size_t __pagesize;
  159. #include <bits/uClibc_page.h>
  160. #ifndef PAGE_SIZE
  161. #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
  162. #endif
  163. /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
  164. #ifndef INITIAL_STACK_SIZE
  165. #define INITIAL_STACK_SIZE (4 * __pagesize)
  166. #endif
  167. /* Size of the thread manager stack. The "- 32" avoids wasting space
  168. with some malloc() implementations. */
  169. #ifndef THREAD_MANAGER_STACK_SIZE
  170. #define THREAD_MANAGER_STACK_SIZE (2 * __pagesize - 32)
  171. #endif
  172. /* The base of the "array" of thread stacks. The array will grow down from
  173. here. Defaults to the calculated bottom of the initial application
  174. stack. */
  175. #ifndef THREAD_STACK_START_ADDRESS
  176. #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
  177. #endif
  178. /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
  179. architecture doesn't need a memory barrier instruction (e.g. Intel
  180. x86). Still we need the compiler to respect the barrier and emit
  181. all outstanding operations which modify memory. Some architectures
  182. distinguish between full, read and write barriers. */
  183. #ifndef MEMORY_BARRIER
  184. #define MEMORY_BARRIER() asm ("" : : : "memory")
  185. #endif
  186. #ifndef READ_MEMORY_BARRIER
  187. #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
  188. #endif
  189. #ifndef WRITE_MEMORY_BARRIER
  190. #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
  191. #endif
  192. /* Max number of times we must spin on a spinlock calling sched_yield().
  193. After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
  194. #ifndef MAX_SPIN_COUNT
  195. #define MAX_SPIN_COUNT 50
  196. #endif
  197. /* Max number of times the spinlock in the adaptive mutex implementation
  198. spins actively on SMP systems. */
  199. #ifndef MAX_ADAPTIVE_SPIN_COUNT
  200. #define MAX_ADAPTIVE_SPIN_COUNT 100
  201. #endif
  202. /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
  203. after MAX_SPIN_COUNT iterations of sched_yield().
  204. With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
  205. (Otherwise the kernel does busy-waiting for realtime threads,
  206. giving other threads no chance to run.) */
  207. #ifndef SPIN_SLEEP_DURATION
  208. #define SPIN_SLEEP_DURATION 2000001
  209. #endif
  210. /* Debugging */
  211. #ifdef DEBUG
  212. #include <assert.h>
  213. #define ASSERT assert
  214. #define MSG __pthread_message
  215. #else
  216. #define ASSERT(x)
  217. #define MSG(msg,arg...)
  218. #endif
  219. /* Internal global functions */
  220. void __pthread_destroy_specifics(void);
  221. void __pthread_perform_cleanup(void);
  222. int __pthread_initialize_manager(void);
  223. void __pthread_message(char * fmt, ...);
  224. int __pthread_manager(void *reqfd);
  225. int __pthread_manager_event(void *reqfd);
  226. void __pthread_manager_sighandler(int sig);
  227. void __pthread_reset_main_thread(void);
  228. void __fresetlockfiles(void);
  229. void __pthread_manager_adjust_prio(int thread_prio);
  230. void __pthread_initialize_minimal (void);
  231. extern int __pthread_attr_setguardsize __P ((pthread_attr_t *__attr,
  232. size_t __guardsize));
  233. extern int __pthread_attr_getguardsize __P ((__const pthread_attr_t *__attr,
  234. size_t *__guardsize));
  235. extern int __pthread_attr_setstackaddr __P ((pthread_attr_t *__attr,
  236. void *__stackaddr));
  237. extern int __pthread_attr_getstackaddr __P ((__const pthread_attr_t *__attr,
  238. void **__stackaddr));
  239. extern int __pthread_attr_setstacksize __P ((pthread_attr_t *__attr,
  240. size_t __stacksize));
  241. extern int __pthread_attr_getstacksize __P ((__const pthread_attr_t *__attr,
  242. size_t *__stacksize));
  243. extern int __pthread_getconcurrency __P ((void));
  244. extern int __pthread_setconcurrency __P ((int __level));
  245. extern int __pthread_mutexattr_gettype __P ((__const pthread_mutexattr_t *__attr,
  246. int *__kind));
  247. extern void __pthread_kill_other_threads_np __P ((void));
  248. extern void __pthread_restart_old(pthread_descr th);
  249. extern void __pthread_suspend_old(pthread_descr self);
  250. extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
  251. extern void __pthread_restart_new(pthread_descr th);
  252. extern void __pthread_suspend_new(pthread_descr self);
  253. extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
  254. extern void __pthread_wait_for_restart_signal(pthread_descr self);
  255. /* Global pointers to old or new suspend functions */
  256. extern void (*__pthread_restart)(pthread_descr);
  257. extern void (*__pthread_suspend)(pthread_descr);
  258. /* Prototypes for the function without cancelation support when the
  259. normal version has it. */
  260. extern int __libc_close (int fd);
  261. extern int __libc_nanosleep (const struct timespec *requested_time,
  262. struct timespec *remaining);
  263. extern ssize_t __libc_read (int fd, void *buf, size_t count);
  264. extern pid_t __libc_waitpid (pid_t pid, int *stat_loc, int options);
  265. extern ssize_t __libc_write (int fd, const void *buf, size_t count);
  266. /* Prototypes for some of the new semaphore functions. */
  267. extern int __new_sem_post (sem_t * sem);
  268. /* The functions called the signal events. */
  269. extern void __linuxthreads_create_event (void);
  270. extern void __linuxthreads_death_event (void);
  271. extern void __linuxthreads_reap_event (void);
  272. #endif /* internals.h */