Strictly check whether catalog file is larger enough for the data.
[glibc.git] / linuxthreads / internals.h
blobd68adeb987cff3560d8009ea62c1e8f1f9a9bc44
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. */
15 /* Internal data structures */
17 /* Includes */
19 #include <bits/libc-lock.h> /* for _LIBC_TSD_KEY_N */
20 #include <limits.h>
21 #include <setjmp.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <sys/types.h>
26 #include "pt-machine.h"
28 /* Arguments passed to thread creation routine */
30 struct pthread_start_args {
31 void * (*start_routine)(void *); /* function to run */
32 void * arg; /* its argument */
33 sigset_t mask; /* initial signal mask for thread */
34 int schedpolicy; /* initial scheduling policy (if any) */
35 struct sched_param schedparam; /* initial scheduling parameters (if any) */
39 /* We keep thread specific data in a special data structure, a two-level
40 array. The top-level array contains pointers to dynamically allocated
41 arrays of a certain number of data pointers. So we can implement a
42 sparse array. Each dynamic second-level array has
43 PTHREAD_KEY_2NDLEVEL_SIZE
44 entries. This value shouldn't be too large. */
45 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
47 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
48 keys in each subarray. */
49 #define PTHREAD_KEY_1STLEVEL_SIZE \
50 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
51 / PTHREAD_KEY_2NDLEVEL_SIZE)
54 #define PTHREAD_START_ARGS_INITIALIZER { NULL, NULL, {{0, }}, 0, { 0 } }
56 /* The type of thread descriptors */
58 typedef struct _pthread_descr_struct * pthread_descr;
60 struct _pthread_descr_struct {
61 pthread_descr p_nextlive, p_prevlive;
62 /* Double chaining of active threads */
63 pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
64 pthread_t p_tid; /* Thread identifier */
65 int p_pid; /* PID of Unix process */
66 int p_priority; /* Thread priority (== 0 if not realtime) */
67 struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
68 int p_signal; /* last signal received */
69 sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
70 sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
71 char p_terminated; /* true if terminated e.g. by pthread_exit */
72 char p_detached; /* true if detached */
73 char p_exited; /* true if the assoc. process terminated */
74 void * p_retval; /* placeholder for return value */
75 int p_retcode; /* placeholder for return code */
76 pthread_descr p_joining; /* thread joining on that thread or NULL */
77 struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
78 char p_cancelstate; /* cancellation state */
79 char p_canceltype; /* cancellation type (deferred/async) */
80 char p_canceled; /* cancellation request pending */
81 int * p_errnop; /* pointer to used errno variable */
82 int p_errno; /* error returned by last system call */
83 int * p_h_errnop; /* pointer to used h_errno variable */
84 int p_h_errno; /* error returned by last netdb function */
85 char * p_in_sighandler; /* stack address of sighandler, or NULL */
86 char p_sigwaiting; /* true if a sigwait() is in progress */
87 struct pthread_start_args p_start_args; /* arguments for thread creation */
88 void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
89 void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
90 int p_userstack; /* nonzero if the user provided the stack */
91 void *p_guardaddr; /* address of guard area or NULL */
92 size_t p_guardsize; /* size of guard area */
95 /* The type of thread handles. */
97 typedef struct pthread_handle_struct * pthread_handle;
99 struct pthread_handle_struct {
100 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
101 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
102 char * h_bottom; /* Lowest address in the stack thread */
105 /* The type of messages sent to the thread manager thread */
107 struct pthread_request {
108 pthread_descr req_thread; /* Thread doing the request */
109 enum { /* Request kind */
110 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
111 REQ_POST, REQ_DEBUG
112 } req_kind;
113 union { /* Arguments for request */
114 struct { /* For REQ_CREATE: */
115 const pthread_attr_t * attr; /* thread attributes */
116 void * (*fn)(void *); /* start function */
117 void * arg; /* argument to start function */
118 sigset_t mask; /* signal mask */
119 } create;
120 struct { /* For REQ_FREE: */
121 pthread_t thread_id; /* identifier of thread to free */
122 } free;
123 struct { /* For REQ_PROCESS_EXIT: */
124 int code; /* exit status */
125 } exit;
126 void * post; /* For REQ_POST: the semaphore */
127 } req_args;
131 /* Signals used for suspend/restart and for cancellation notification. */
133 #ifdef SIGRTMIN
134 /* The have real-time signals. */
135 extern int __pthread_sig_restart;
136 extern int __pthread_sig_cancel;
137 # define PTHREAD_SIG_RESTART __pthread_sig_restart
138 # define PTHREAD_SIG_CANCEL __pthread_sig_cancel
139 #else
140 # define PTHREAD_SIG_RESTART SIGUSR1
141 # define PTHREAD_SIG_CANCEL SIGUSR2
142 #endif
144 /* Global array of thread handles, used for validating a thread id
145 and retrieving the corresponding thread descriptor. Also used for
146 mapping the available stack segments. */
148 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
150 /* Descriptor of the initial thread */
152 extern struct _pthread_descr_struct __pthread_initial_thread;
154 /* Descriptor of the manager thread */
156 extern struct _pthread_descr_struct __pthread_manager_thread;
158 /* Descriptor of the main thread */
160 extern pthread_descr __pthread_main_thread;
162 /* Limit between the stack of the initial thread (above) and the
163 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
164 Initially 0, meaning that the current thread is (by definition)
165 the initial thread. */
167 extern char *__pthread_initial_thread_bos;
169 /* Indicate whether at least one thread has a user-defined stack (if 1),
170 or all threads have stacks supplied by LinuxThreads (if 0). */
172 extern int __pthread_nonstandard_stacks;
174 /* File descriptor for sending requests to the thread manager.
175 Initially -1, meaning that __pthread_initialize_manager must be called. */
177 extern int __pthread_manager_request;
179 /* Other end of the pipe for sending requests to the thread manager. */
181 extern int __pthread_manager_reader;
183 /* Limits of the thread manager stack. */
185 extern char *__pthread_manager_thread_bos;
186 extern char *__pthread_manager_thread_tos;
188 /* Pending request for a process-wide exit */
190 extern int __pthread_exit_requested, __pthread_exit_code;
192 /* Set to 1 by gdb if we're debugging */
194 extern volatile int __pthread_threads_debug;
196 /* Return the handle corresponding to a thread id */
198 static inline pthread_handle thread_handle(pthread_t id)
200 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
203 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
205 static inline int invalid_handle(pthread_handle h, pthread_t id)
207 return h->h_descr == NULL || h->h_descr->p_tid != id;
210 /* Fill in defaults left unspecified by pt-machine.h. */
212 /* The page size we can get from the system. This should likely not be
213 changed by the machine file but, you never know. */
214 #ifndef PAGE_SIZE
215 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
216 #endif
218 /* The max size of the thread stack segments. If the default
219 THREAD_SELF implementation is used, this must be a power of two and
220 a multiple of PAGE_SIZE. */
221 #ifndef STACK_SIZE
222 #define STACK_SIZE (2 * 1024 * 1024)
223 #endif
225 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
226 #ifndef INITIAL_STACK_SIZE
227 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
228 #endif
230 /* Size of the thread manager stack. The "- 32" avoids wasting space
231 with some malloc() implementations. */
232 #ifndef THREAD_MANAGER_STACK_SIZE
233 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
234 #endif
236 /* The base of the "array" of thread stacks. The array will grow down from
237 here. Defaults to the calculated bottom of the initial application
238 stack. */
239 #ifndef THREAD_STACK_START_ADDRESS
240 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
241 #endif
243 /* Get some notion of the current stack. Need not be exactly the top
244 of the stack, just something somewhere in the current frame. */
245 #ifndef CURRENT_STACK_FRAME
246 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
247 #endif
249 /* Recover thread descriptor for the current thread */
251 extern pthread_descr __pthread_find_self (void) __attribute__ ((const));
253 static inline pthread_descr thread_self (void) __attribute__ ((const));
254 static inline pthread_descr thread_self (void)
256 #ifdef THREAD_SELF
257 return THREAD_SELF;
258 #else
259 char *sp = CURRENT_STACK_FRAME;
260 if (sp >= __pthread_initial_thread_bos)
261 return &__pthread_initial_thread;
262 else if (sp >= __pthread_manager_thread_bos
263 && sp < __pthread_manager_thread_tos)
264 return &__pthread_manager_thread;
265 else if (__pthread_nonstandard_stacks)
266 return __pthread_find_self();
267 else
268 return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
269 #endif
272 /* Max number of times we must spin on a spinlock calling sched_yield().
273 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
275 #ifndef MAX_SPIN_COUNT
276 #define MAX_SPIN_COUNT 50
277 #endif
279 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
280 after MAX_SPIN_COUNT iterations of sched_yield().
281 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
282 (Otherwise the kernel does busy-waiting for realtime threads,
283 giving other threads no chance to run.) */
285 #ifndef SPIN_SLEEP_DURATION
286 #define SPIN_SLEEP_DURATION 2000001
287 #endif
289 /* Debugging */
291 #ifdef DEBUG
292 #include <assert.h>
293 #define ASSERT assert
294 #define MSG __pthread_message
295 #else
296 #define ASSERT(x)
297 #define MSG(msg,arg...)
298 #endif
300 /* Internal global functions */
302 void __pthread_destroy_specifics(void);
303 void __pthread_perform_cleanup(void);
304 int __pthread_initialize_manager(void);
305 void __pthread_message(char * fmt, ...);
306 int __pthread_manager(void *reqfd);
307 void __pthread_manager_sighandler(int sig);
308 void __pthread_reset_main_thread(void);
309 void __fresetlockfiles(void);
310 void __pthread_manager_adjust_prio(int thread_prio);
312 /* Prototypes for the function without cancelation support when the
313 normal version has it. */
314 extern int __libc_close (int fd);
315 extern int __libc_nanosleep (const struct timespec *requested_time,
316 struct timespec *remaining);
317 extern int __libc_read (int fd, void *buf, size_t count);
318 extern pid_t __libc_waitpid (pid_t pid, int *stat_loc, int options);
319 extern int __libc_write (int fd, const void *buf, size_t count);