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. */
18 #define __need_res_state
23 #include <sys/types.h>
24 #include <hp-timing.h>
25 #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
28 /* The type of thread descriptors */
29 typedef struct _pthread_descr_struct
*pthread_descr
;
32 /* Some more includes. */
33 #include <pt-machine.h>
34 #include <linuxthreads_db/thread_dbP.h>
37 /* Arguments passed to thread creation routine */
38 struct pthread_start_args
{
39 void *(*start_routine
)(void *); /* function to run */
40 void *arg
; /* its argument */
41 sigset_t mask
; /* initial signal mask for thread */
42 int schedpolicy
; /* initial scheduling policy (if any) */
43 struct sched_param schedparam
; /* initial scheduling parameters (if any) */
47 /* Callback interface for removing the thread from waiting on an
48 object if it is cancelled while waiting or about to wait.
49 This hold a pointer to the object, and a pointer to a function
50 which ``extricates'' the thread from its enqueued state.
51 The function takes two arguments: pointer to the wait object,
52 and a pointer to the thread. It returns 1 if an extrication
53 actually occured, and hence the thread must also be signalled.
54 It returns 0 if the thread had already been extricated. */
55 typedef struct _pthread_extricate_struct
{
57 int (*pu_extricate_func
)(void *, pthread_descr
);
58 } pthread_extricate_if
;
61 /* Atomic counter made possible by compare_and_swap */
62 struct pthread_atomic
{
68 /* Context info for read write locks. The pthread_rwlock_info structure
69 is information about a lock that has been read-locked by the thread
70 in whose list this structure appears. The pthread_rwlock_context
71 is embedded in the thread context and contains a pointer to the
72 head of the list of lock info structures, as well as a count of
73 read locks that are untracked, because no info structure could be
74 allocated for them. */
75 struct _pthread_rwlock_t
;
76 typedef struct _pthread_rwlock_info
{
77 struct _pthread_rwlock_info
*pr_next
;
78 struct _pthread_rwlock_t
*pr_lock
;
80 } pthread_readlock_info
;
83 /* We keep thread specific data in a special data structure, a two-level
84 array. The top-level array contains pointers to dynamically allocated
85 arrays of a certain number of data pointers. So we can implement a
86 sparse array. Each dynamic second-level array has
87 PTHREAD_KEY_2NDLEVEL_SIZE
88 entries. This value shouldn't be too large. */
89 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
91 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
92 keys in each subarray. */
93 #define PTHREAD_KEY_1STLEVEL_SIZE \
94 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
95 / PTHREAD_KEY_2NDLEVEL_SIZE)
101 struct _pthread_descr_struct
{
102 /* XXX Remove this union for IA-64 style TLS module */
105 void *tcb
; /* Pointer to the TCB. This is not always
106 the address of this thread descriptor. */
108 pthread_descr self
; /* Pointer to this structure */
112 pthread_descr p_nextlive
, p_prevlive
;
113 /* Double chaining of active threads */
114 pthread_descr p_nextwaiting
; /* Next element in the queue holding the thr */
115 pthread_descr p_nextlock
; /* can be on a queue and waiting on a lock */
116 pthread_t p_tid
; /* Thread identifier */
117 int p_pid
; /* PID of Unix process */
118 int p_priority
; /* Thread priority (== 0 if not realtime) */
119 struct _pthread_fastlock
* p_lock
; /* Spinlock for synchronized accesses */
120 int p_signal
; /* last signal received */
121 sigjmp_buf
* p_signal_jmp
; /* where to siglongjmp on a signal or NULL */
122 sigjmp_buf
* p_cancel_jmp
; /* where to siglongjmp on a cancel or NULL */
123 char p_terminated
; /* true if terminated e.g. by pthread_exit */
124 char p_detached
; /* true if detached */
125 char p_exited
; /* true if the assoc. process terminated */
126 void * p_retval
; /* placeholder for return value */
127 int p_retcode
; /* placeholder for return code */
128 pthread_descr p_joining
; /* thread joining on that thread or NULL */
129 struct _pthread_cleanup_buffer
* p_cleanup
; /* cleanup functions */
130 char p_cancelstate
; /* cancellation state */
131 char p_canceltype
; /* cancellation type (deferred/async) */
132 char p_canceled
; /* cancellation request pending */
133 int * p_errnop
; /* pointer to used errno variable */
134 int p_errno
; /* error returned by last system call */
135 int * p_h_errnop
; /* pointer to used h_errno variable */
136 int p_h_errno
; /* error returned by last netdb function */
137 char * p_in_sighandler
; /* stack address of sighandler, or NULL */
138 char p_sigwaiting
; /* true if a sigwait() is in progress */
139 struct pthread_start_args p_start_args
; /* arguments for thread creation */
140 void ** p_specific
[PTHREAD_KEY_1STLEVEL_SIZE
]; /* thread-specific data */
141 void * p_libc_specific
[_LIBC_TSD_KEY_N
]; /* thread-specific data for libc */
142 int p_userstack
; /* nonzero if the user provided the stack */
143 void *p_guardaddr
; /* address of guard area or NULL */
144 size_t p_guardsize
; /* size of guard area */
145 int p_nr
; /* Index of descriptor in __pthread_handles */
146 int p_report_events
; /* Nonzero if events must be reported. */
147 td_eventbuf_t p_eventbuf
; /* Data for event. */
148 struct pthread_atomic p_resume_count
; /* number of times restart() was
150 char p_woken_by_cancel
; /* cancellation performed wakeup */
151 char p_condvar_avail
; /* flag if conditional variable became avail */
152 char p_sem_avail
; /* flag if semaphore became available */
153 pthread_extricate_if
*p_extricate
; /* See above */
154 pthread_readlock_info
*p_readlock_list
; /* List of readlock info structs */
155 pthread_readlock_info
*p_readlock_free
; /* Free list of structs */
156 int p_untracked_readlock_count
; /* Readlocks not tracked by list */
157 struct __res_state
*p_resp
; /* Pointer to resolver state */
158 struct __res_state p_res
; /* per-thread resolver state */
159 int p_inheritsched
; /* copied from the thread attribute */
161 hp_timing_t p_cpuclock_offset
; /* Initial CPU clock for thread. */
164 char *p_stackaddr
; /* Stack address. */
166 /* New elements must be added at the end. */
167 } __attribute__ ((aligned(32))); /* We need to align the structure so that
168 doubles are aligned properly. This is 8
169 bytes on MIPS and 16 bytes on MIPS64.
170 32 bytes might give better cache
175 /* Limit between the stack of the initial thread (above) and the
176 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
177 Initially 0, meaning that the current thread is (by definition)
178 the initial thread. */
180 extern char *__pthread_initial_thread_bos
;
182 /* Descriptor of the initial thread */
184 extern struct _pthread_descr_struct __pthread_initial_thread
;
186 /* Limits of the thread manager stack. */
188 extern char *__pthread_manager_thread_bos
;
189 extern char *__pthread_manager_thread_tos
;
191 /* Descriptor of the manager thread */
193 extern struct _pthread_descr_struct __pthread_manager_thread
;
195 /* Indicate whether at least one thread has a user-defined stack (if 1),
196 or all threads have stacks supplied by LinuxThreads (if 0). */
198 extern int __pthread_nonstandard_stacks
;
200 /* The max size of the thread stack segments. If the default
201 THREAD_SELF implementation is used, this must be a power of two and
202 a multiple of PAGE_SIZE. */
204 #define STACK_SIZE (2 * 1024 * 1024)
207 /* Get some notion of the current stack. Need not be exactly the top
208 of the stack, just something somewhere in the current frame. */
209 #ifndef CURRENT_STACK_FRAME
210 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
213 /* Recover thread descriptor for the current thread */
215 extern pthread_descr
__pthread_find_self (void) __attribute__ ((const));
217 static inline pthread_descr
thread_self (void) __attribute__ ((const));
218 static inline pthread_descr
thread_self (void)
223 char *sp
= CURRENT_STACK_FRAME
;
224 if (sp
>= __pthread_initial_thread_bos
)
225 return &__pthread_initial_thread
;
226 else if (sp
>= __pthread_manager_thread_bos
227 && sp
< __pthread_manager_thread_tos
)
228 return &__pthread_manager_thread
;
229 else if (__pthread_nonstandard_stacks
)
230 return __pthread_find_self();
232 #ifdef _STACK_GROWS_DOWN
233 return (pthread_descr
)(((unsigned long)sp
| (STACK_SIZE
-1))+1) - 1;
235 return (pthread_descr
)((unsigned long)sp
&~ (STACK_SIZE
-1));