1 /* Copyright (C) 2002-2006, 2007, 2008 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <sys/types.h>
28 #include <hp-timing.h>
30 #include <lowlevellock.h>
31 #include <pthreaddef.h>
32 #include <dl-sysdep.h>
33 #include "../nptl_db/thread_db.h"
35 #ifdef HAVE_FORCED_UNWIND
38 #define __need_res_state
40 #include <kernel-features.h>
43 # define TCB_ALIGNMENT sizeof (double)
47 /* We keep thread specific data in a special data structure, a two-level
48 array. The top-level array contains pointers to dynamically allocated
49 arrays of a certain number of data pointers. So we can implement a
50 sparse array. Each dynamic second-level array has
51 PTHREAD_KEY_2NDLEVEL_SIZE
52 entries. This value shouldn't be too large. */
53 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
55 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
56 keys in each subarray. */
57 #define PTHREAD_KEY_1STLEVEL_SIZE \
58 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
59 / PTHREAD_KEY_2NDLEVEL_SIZE)
64 /* Internal version of the buffer to store cancellation handler
66 struct pthread_unwind_buf
76 /* This is the placeholder of the public version. */
81 /* Pointer to the previous cleanup buffer. */
82 struct pthread_unwind_buf
*prev
;
84 /* Backward compatibility: state of the old-style cleanup
85 handler at the time of the previous new-style cleanup handler
87 struct _pthread_cleanup_buffer
*cleanup
;
89 /* Cancellation type before the push call. */
96 /* Opcodes and data types for communication with the signal handler to
97 change user/group IDs. */
106 /* Data structure used by the kernel to find robust futexes. */
107 struct robust_list_head
110 long int futex_offset
;
111 void *list_op_pending
;
115 /* Data strcture used to handle thread priority protection. */
116 struct priority_protection_data
119 unsigned int priomap
[];
123 /* Thread descriptor data structure. */
129 /* This overlaps the TCB as used for TLS without threads (see tls.h). */
134 int multiple_threads
;
136 # ifndef __ASSUME_PRIVATE_FUTEX
142 /* This extra padding has no special purpose, and this structure layout
143 is private and subject to change without affecting the official ABI.
144 We just have it here in case it might be convenient for some
145 implementation-specific instrumentation hack or suchlike. */
149 /* This descriptor's link on the `stack_used' or `__stack_user' list. */
152 /* Thread ID - which is also a 'is this thread descriptor (and
153 therefore stack) used' flag. */
156 /* Process ID - thread group ID in kernel speak. */
159 /* List of robust mutexes the thread is holding. */
160 #ifdef __PTHREAD_MUTEX_HAVE_PREV
162 struct robust_list_head robust_head
;
164 /* The list above is strange. It is basically a double linked list
165 but the pointer to the next/previous element of the list points
166 in the middle of the object, the __next element. Whenever
167 casting to __pthread_list_t we need to adjust the pointer
169 # define QUEUE_PTR_ADJUST (offsetof (__pthread_list_t, __next))
171 # define ENQUEUE_MUTEX_BOTH(mutex, val) \
173 __pthread_list_t *next = (__pthread_list_t *) \
174 ((((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_head.list)) & ~1ul) \
175 - QUEUE_PTR_ADJUST); \
176 next->__prev = (void *) &mutex->__data.__list.__next; \
177 mutex->__data.__list.__next = THREAD_GETMEM (THREAD_SELF, \
179 mutex->__data.__list.__prev = (void *) &THREAD_SELF->robust_head; \
180 THREAD_SETMEM (THREAD_SELF, robust_head.list, \
181 (void *) (((uintptr_t) &mutex->__data.__list.__next) \
184 # define DEQUEUE_MUTEX(mutex) \
186 __pthread_list_t *next = (__pthread_list_t *) \
187 ((char *) (((uintptr_t) mutex->__data.__list.__next) & ~1ul) \
188 - QUEUE_PTR_ADJUST); \
189 next->__prev = mutex->__data.__list.__prev; \
190 __pthread_list_t *prev = (__pthread_list_t *) \
191 ((char *) (((uintptr_t) mutex->__data.__list.__prev) & ~1ul) \
192 - QUEUE_PTR_ADJUST); \
193 prev->__next = mutex->__data.__list.__next; \
194 mutex->__data.__list.__prev = NULL; \
195 mutex->__data.__list.__next = NULL; \
200 __pthread_slist_t robust_list
;
201 struct robust_list_head robust_head
;
204 # define ENQUEUE_MUTEX_BOTH(mutex, val) \
206 mutex->__data.__list.__next \
207 = THREAD_GETMEM (THREAD_SELF, robust_list.__next); \
208 THREAD_SETMEM (THREAD_SELF, robust_list.__next, \
209 (void *) (((uintptr_t) &mutex->__data.__list) | val)); \
211 # define DEQUEUE_MUTEX(mutex) \
213 __pthread_slist_t *runp = (__pthread_slist_t *) \
214 (((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_list.__next)) & ~1ul); \
215 if (runp == &mutex->__data.__list) \
216 THREAD_SETMEM (THREAD_SELF, robust_list.__next, runp->__next); \
219 __pthread_slist_t *next = (__pthread_slist_t *) \
220 (((uintptr_t) runp->__next) & ~1ul); \
221 while (next != &mutex->__data.__list) \
224 next = (__pthread_slist_t *) (((uintptr_t) runp->__next) & ~1ul); \
227 runp->__next = next->__next; \
228 mutex->__data.__list.__next = NULL; \
232 #define ENQUEUE_MUTEX(mutex) ENQUEUE_MUTEX_BOTH (mutex, 0)
233 #define ENQUEUE_MUTEX_PI(mutex) ENQUEUE_MUTEX_BOTH (mutex, 1)
235 /* List of cleanup buffers. */
236 struct _pthread_cleanup_buffer
*cleanup
;
238 /* Unwind information. */
239 struct pthread_unwind_buf
*cleanup_jmp_buf
;
240 #define HAVE_CLEANUP_JMP_BUF
242 /* Flags determining processing of cancellation. */
244 /* Bit set if cancellation is disabled. */
245 #define CANCELSTATE_BIT 0
246 #define CANCELSTATE_BITMASK 0x01
247 /* Bit set if asynchronous cancellation mode is selected. */
248 #define CANCELTYPE_BIT 1
249 #define CANCELTYPE_BITMASK 0x02
250 /* Bit set if canceling has been initiated. */
251 #define CANCELING_BIT 2
252 #define CANCELING_BITMASK 0x04
253 /* Bit set if canceled. */
254 #define CANCELED_BIT 3
255 #define CANCELED_BITMASK 0x08
256 /* Bit set if thread is exiting. */
257 #define EXITING_BIT 4
258 #define EXITING_BITMASK 0x10
259 /* Bit set if thread terminated and TCB is freed. */
260 #define TERMINATED_BIT 5
261 #define TERMINATED_BITMASK 0x20
262 /* Bit set if thread is supposed to change XID. */
264 #define SETXID_BITMASK 0x40
265 /* Mask for the rest. Helps the compiler to optimize. */
266 #define CANCEL_RESTMASK 0xffffff80
268 #define CANCEL_ENABLED_AND_CANCELED(value) \
269 (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \
270 | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
271 #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
272 (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \
273 | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \
274 == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
276 /* Flags. Including those copied from the thread attribute. */
279 /* We allocate one block of references here. This should be enough
280 to avoid allocating any memory dynamically for most applications. */
281 struct pthread_key_data
283 /* Sequence number. We use uintptr_t to not require padding on
284 32- and 64-bit machines. On 64-bit machines it helps to avoid
290 } specific_1stblock
[PTHREAD_KEY_2NDLEVEL_SIZE
];
292 /* Two-level array for the thread-specific data. */
293 struct pthread_key_data
*specific
[PTHREAD_KEY_1STLEVEL_SIZE
];
295 /* Flag which is set when specific data is set. */
298 /* True if events must be reported. */
301 /* True if the user provided the stack. */
304 /* True if thread must stop at startup time. */
307 /* The parent's cancel handling at the time of the pthread_create
308 call. This might be needed to undo the effects of a cancellation. */
309 int parent_cancelhandling
;
311 /* Lock to synchronize access to the descriptor. */
314 /* Lock for synchronizing setxid calls. */
318 /* Offset of the CPU clock at start thread start time. */
319 hp_timing_t cpuclock_offset
;
322 /* If the thread waits to join another one the ID of the latter is
325 In case a thread is detached this field contains a pointer of the
326 TCB if the thread itself. This is something which cannot happen
327 in normal operation. */
328 struct pthread
*joinid
;
329 /* Check whether a thread is detached. */
330 #define IS_DETACHED(pd) ((pd)->joinid == (pd))
332 /* The result of the thread function. */
335 /* Scheduling parameters for the new thread. */
336 struct sched_param schedparam
;
339 /* Start position of the code to be executed and the argument passed
341 void *(*start_routine
) (void *);
345 td_eventbuf_t eventbuf
;
346 /* Next descriptor with a pending event. */
347 struct pthread
*nextevent
;
349 #ifdef HAVE_FORCED_UNWIND
350 /* Machine-specific unwind info. */
351 struct _Unwind_Exception exc
;
354 /* If nonzero pointer to area allocated for the stack and its
357 size_t stackblock_size
;
358 /* Size of the included guard area. */
360 /* This is what the user specified and what we will report. */
361 size_t reported_guardsize
;
363 /* Thread Priority Protection data. */
364 struct priority_protection_data
*tpp
;
366 /* Resolver state. */
367 struct __res_state res
;
369 /* This member must be last. */
372 #define PTHREAD_STRUCT_END_PADDING \
373 (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
374 } __attribute ((aligned (TCB_ALIGNMENT
)));