1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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. */
16 #define _INTERNALS_H 1
18 /* Internal data structures */
26 #include <sys/types.h>
27 #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
29 #include "pt-machine.h"
30 #include "semaphore.h"
31 #include "../linuxthreads_db/thread_dbP.h"
34 # define THREAD_GETMEM(descr, member) descr->member
36 #ifndef THREAD_GETMEM_NC
37 # define THREAD_GETMEM_NC(descr, member) descr->member
40 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
42 #ifndef THREAD_SETMEM_NC
43 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
46 /* Arguments passed to thread creation routine */
48 struct pthread_start_args
{
49 void * (*start_routine
)(void *); /* function to run */
50 void * arg
; /* its argument */
51 sigset_t mask
; /* initial signal mask for thread */
52 int schedpolicy
; /* initial scheduling policy (if any) */
53 struct sched_param schedparam
; /* initial scheduling parameters (if any) */
57 /* We keep thread specific data in a special data structure, a two-level
58 array. The top-level array contains pointers to dynamically allocated
59 arrays of a certain number of data pointers. So we can implement a
60 sparse array. Each dynamic second-level array has
61 PTHREAD_KEY_2NDLEVEL_SIZE
62 entries. This value shouldn't be too large. */
63 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
65 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
66 keys in each subarray. */
67 #define PTHREAD_KEY_1STLEVEL_SIZE \
68 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
69 / PTHREAD_KEY_2NDLEVEL_SIZE)
71 typedef void (*destr_function
)(void *);
73 struct pthread_key_struct
{
74 int in_use
; /* already allocated? */
75 destr_function destr
; /* destruction routine */
79 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
80 { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
82 /* The type of thread descriptors */
84 typedef struct _pthread_descr_struct
* pthread_descr
;
86 struct _pthread_descr_struct
{
87 pthread_descr p_nextlive
, p_prevlive
;
88 /* Double chaining of active threads */
89 pthread_descr p_nextwaiting
; /* Next element in the queue holding the thr */
90 pthread_descr p_nextlock
; /* can be on a queue and waiting on a lock */
91 pthread_t p_tid
; /* Thread identifier */
92 int p_pid
; /* PID of Unix process */
93 int p_priority
; /* Thread priority (== 0 if not realtime) */
94 struct _pthread_fastlock
* p_lock
; /* Spinlock for synchronized accesses */
95 int p_signal
; /* last signal received */
96 sigjmp_buf
* p_signal_jmp
; /* where to siglongjmp on a signal or NULL */
97 sigjmp_buf
* p_cancel_jmp
; /* where to siglongjmp on a cancel or NULL */
98 char p_terminated
; /* true if terminated e.g. by pthread_exit */
99 char p_detached
; /* true if detached */
100 char p_exited
; /* true if the assoc. process terminated */
101 void * p_retval
; /* placeholder for return value */
102 int p_retcode
; /* placeholder for return code */
103 pthread_descr p_joining
; /* thread joining on that thread or NULL */
104 struct _pthread_cleanup_buffer
* p_cleanup
; /* cleanup functions */
105 char p_cancelstate
; /* cancellation state */
106 char p_canceltype
; /* cancellation type (deferred/async) */
107 char p_canceled
; /* cancellation request pending */
108 int * p_errnop
; /* pointer to used errno variable */
109 int p_errno
; /* error returned by last system call */
110 int * p_h_errnop
; /* pointer to used h_errno variable */
111 int p_h_errno
; /* error returned by last netdb function */
112 char * p_in_sighandler
; /* stack address of sighandler, or NULL */
113 char p_sigwaiting
; /* true if a sigwait() is in progress */
114 struct pthread_start_args p_start_args
; /* arguments for thread creation */
115 void ** p_specific
[PTHREAD_KEY_1STLEVEL_SIZE
]; /* thread-specific data */
116 void * p_libc_specific
[_LIBC_TSD_KEY_N
]; /* thread-specific data for libc */
117 int p_userstack
; /* nonzero if the user provided the stack */
118 void *p_guardaddr
; /* address of guard area or NULL */
119 size_t p_guardsize
; /* size of guard area */
120 pthread_descr p_self
; /* Pointer to this structure */
121 int p_nr
; /* Index of descriptor in __pthread_handles */
122 /* New elements must be added at the end. */
123 int p_report_events
; /* Nonzero if events must be reported. */
124 td_eventbuf_t p_eventbuf
; /* Data for event. */
125 } __attribute__ ((aligned(32))); /* We need to align the structure so that
126 doubles are aligned properly. This is 8
127 bytes on MIPS and 16 bytes on MIPS64.
128 32 bytes might give better cache
132 /* The type of thread handles. */
134 typedef struct pthread_handle_struct
* pthread_handle
;
136 struct pthread_handle_struct
{
137 struct _pthread_fastlock h_lock
; /* Fast lock for sychronized access */
138 pthread_descr h_descr
; /* Thread descriptor or NULL if invalid */
139 char * h_bottom
; /* Lowest address in the stack thread */
142 /* The type of messages sent to the thread manager thread */
144 struct pthread_request
{
145 pthread_descr req_thread
; /* Thread doing the request */
146 enum { /* Request kind */
147 REQ_CREATE
, REQ_FREE
, REQ_PROCESS_EXIT
, REQ_MAIN_THREAD_EXIT
,
150 union { /* Arguments for request */
151 struct { /* For REQ_CREATE: */
152 const pthread_attr_t
* attr
; /* thread attributes */
153 void * (*fn
)(void *); /* start function */
154 void * arg
; /* argument to start function */
155 sigset_t mask
; /* signal mask */
157 struct { /* For REQ_FREE: */
158 pthread_t thread_id
; /* identifier of thread to free */
160 struct { /* For REQ_PROCESS_EXIT: */
161 int code
; /* exit status */
163 void * post
; /* For REQ_POST: the semaphore */
168 /* Signals used for suspend/restart and for cancellation notification. */
170 extern int __pthread_sig_restart
;
171 extern int __pthread_sig_cancel
;
173 /* Signal used for interfacing with gdb */
175 extern int __pthread_sig_debug
;
177 /* Global array of thread handles, used for validating a thread id
178 and retrieving the corresponding thread descriptor. Also used for
179 mapping the available stack segments. */
181 extern struct pthread_handle_struct __pthread_handles
[PTHREAD_THREADS_MAX
];
183 /* Descriptor of the initial thread */
185 extern struct _pthread_descr_struct __pthread_initial_thread
;
187 /* Descriptor of the manager thread */
189 extern struct _pthread_descr_struct __pthread_manager_thread
;
191 /* Descriptor of the main thread */
193 extern pthread_descr __pthread_main_thread
;
195 /* Limit between the stack of the initial thread (above) and the
196 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
197 Initially 0, meaning that the current thread is (by definition)
198 the initial thread. */
200 extern char *__pthread_initial_thread_bos
;
202 /* Indicate whether at least one thread has a user-defined stack (if 1),
203 or all threads have stacks supplied by LinuxThreads (if 0). */
205 extern int __pthread_nonstandard_stacks
;
207 /* File descriptor for sending requests to the thread manager.
208 Initially -1, meaning that __pthread_initialize_manager must be called. */
210 extern int __pthread_manager_request
;
212 /* Other end of the pipe for sending requests to the thread manager. */
214 extern int __pthread_manager_reader
;
216 /* Limits of the thread manager stack. */
218 extern char *__pthread_manager_thread_bos
;
219 extern char *__pthread_manager_thread_tos
;
221 /* Pending request for a process-wide exit */
223 extern int __pthread_exit_requested
, __pthread_exit_code
;
225 /* Set to 1 by gdb if we're debugging */
227 extern volatile int __pthread_threads_debug
;
229 /* Globally enabled events. */
230 extern volatile td_thr_events_t __pthread_threads_events
;
232 /* Pointer to descriptor of thread with last event. */
233 extern volatile pthread_descr __pthread_last_event
;
235 /* Return the handle corresponding to a thread id */
237 static inline pthread_handle
thread_handle(pthread_t id
)
239 return &__pthread_handles
[id
% PTHREAD_THREADS_MAX
];
242 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
244 static inline int invalid_handle(pthread_handle h
, pthread_t id
)
246 return h
->h_descr
== NULL
|| h
->h_descr
->p_tid
!= id
;
249 /* Fill in defaults left unspecified by pt-machine.h. */
251 /* The page size we can get from the system. This should likely not be
252 changed by the machine file but, you never know. */
254 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
257 /* The max size of the thread stack segments. If the default
258 THREAD_SELF implementation is used, this must be a power of two and
259 a multiple of PAGE_SIZE. */
261 #define STACK_SIZE (2 * 1024 * 1024)
264 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
265 #ifndef INITIAL_STACK_SIZE
266 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
269 /* Size of the thread manager stack. The "- 32" avoids wasting space
270 with some malloc() implementations. */
271 #ifndef THREAD_MANAGER_STACK_SIZE
272 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
275 /* The base of the "array" of thread stacks. The array will grow down from
276 here. Defaults to the calculated bottom of the initial application
278 #ifndef THREAD_STACK_START_ADDRESS
279 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
282 /* Get some notion of the current stack. Need not be exactly the top
283 of the stack, just something somewhere in the current frame. */
284 #ifndef CURRENT_STACK_FRAME
285 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
288 /* Recover thread descriptor for the current thread */
290 extern pthread_descr
__pthread_find_self (void) __attribute__ ((const));
292 static inline pthread_descr
thread_self (void) __attribute__ ((const));
293 static inline pthread_descr
thread_self (void)
298 char *sp
= CURRENT_STACK_FRAME
;
299 if (sp
>= __pthread_initial_thread_bos
)
300 return &__pthread_initial_thread
;
301 else if (sp
>= __pthread_manager_thread_bos
302 && sp
< __pthread_manager_thread_tos
)
303 return &__pthread_manager_thread
;
304 else if (__pthread_nonstandard_stacks
)
305 return __pthread_find_self();
307 return (pthread_descr
)(((unsigned long)sp
| (STACK_SIZE
-1))+1) - 1;
311 /* Max number of times we must spin on a spinlock calling sched_yield().
312 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
314 #ifndef MAX_SPIN_COUNT
315 #define MAX_SPIN_COUNT 50
318 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
319 after MAX_SPIN_COUNT iterations of sched_yield().
320 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
321 (Otherwise the kernel does busy-waiting for realtime threads,
322 giving other threads no chance to run.) */
324 #ifndef SPIN_SLEEP_DURATION
325 #define SPIN_SLEEP_DURATION 2000001
332 #define ASSERT assert
333 #define MSG __pthread_message
336 #define MSG(msg,arg...)
339 /* Internal global functions */
341 void __pthread_destroy_specifics(void);
342 void __pthread_perform_cleanup(void);
343 int __pthread_initialize_manager(void);
344 void __pthread_message(char * fmt
, ...);
345 int __pthread_manager(void *reqfd
);
346 int __pthread_manager_event(void *reqfd
);
347 void __pthread_manager_sighandler(int sig
);
348 void __pthread_reset_main_thread(void);
349 void __fresetlockfiles(void);
350 void __pthread_manager_adjust_prio(int thread_prio
);
352 extern int __pthread_attr_setguardsize (pthread_attr_t
*__attr
,
353 size_t __guardsize
) __THROW
;
354 extern int __pthread_attr_getguardsize (__const pthread_attr_t
*__attr
,
355 size_t *__guardsize
) __THROW
;
356 extern int __pthread_attr_setstackaddr (pthread_attr_t
*__attr
,
357 void *__stackaddr
) __THROW
;
358 extern int __pthread_attr_getstackaddr (__const pthread_attr_t
*__attr
,
359 void **__stackaddr
) __THROW
;
360 extern int __pthread_attr_setstacksize (pthread_attr_t
*__attr
,
361 size_t __stacksize
) __THROW
;
362 extern int __pthread_attr_getstacksize (__const pthread_attr_t
*__attr
,
363 size_t *__stacksize
) __THROW
;
364 extern int __pthread_getconcurrency (void) __THROW
;
365 extern int __pthread_setconcurrency (int __level
) __THROW
;
366 extern int __pthread_mutexattr_gettype (__const pthread_mutexattr_t
*__attr
,
367 int *__kind
) __THROW
;
368 extern void __pthread_kill_other_threads_np (void) __THROW
;
370 /* Prototypes for the function without cancelation support when the
371 normal version has it. */
372 extern int __libc_close (int fd
);
373 extern int __libc_nanosleep (const struct timespec
*requested_time
,
374 struct timespec
*remaining
);
375 extern ssize_t
__libc_read (int fd
, void *buf
, size_t count
);
376 extern pid_t
__libc_waitpid (pid_t pid
, int *stat_loc
, int options
);
377 extern ssize_t
__libc_write (int fd
, const void *buf
, size_t count
);
379 /* Prototypes for some of the new semaphore functions. */
380 extern int __new_sem_post (sem_t
* sem
);
382 /* The functions called the signal events. */
383 extern void __linuxthreads_create_event (void);
384 extern void __linuxthreads_death_event (void);
385 extern void __linuxthreads_reap_event (void);
387 #endif /* internals.h */