1 /* Copyright (C) 2002-2006, 2007, 2008, 2009 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>
31 #include <lowlevellock.h>
32 #include <pthreaddef.h>
33 #include <dl-sysdep.h>
34 #include "../nptl_db/thread_db.h"
36 #ifdef HAVE_FORCED_UNWIND
39 #define __need_res_state
41 #include <kernel-features.h>
44 # define TCB_ALIGNMENT sizeof (double)
48 /* We keep thread specific data in a special data structure, a two-level
49 array. The top-level array contains pointers to dynamically allocated
50 arrays of a certain number of data pointers. So we can implement a
51 sparse array. Each dynamic second-level array has
52 PTHREAD_KEY_2NDLEVEL_SIZE
53 entries. This value shouldn't be too large. */
54 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
56 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
57 keys in each subarray. */
58 #define PTHREAD_KEY_1STLEVEL_SIZE \
59 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
60 / PTHREAD_KEY_2NDLEVEL_SIZE)
65 /* Internal version of the buffer to store cancellation handler
67 struct pthread_unwind_buf
77 /* This is the placeholder of the public version. */
82 /* Pointer to the previous cleanup buffer. */
83 struct pthread_unwind_buf
*prev
;
85 /* Backward compatibility: state of the old-style cleanup
86 handler at the time of the previous new-style cleanup handler
88 struct _pthread_cleanup_buffer
*cleanup
;
90 /* Cancellation type before the push call. */
97 /* Opcodes and data types for communication with the signal handler to
98 change user/group IDs. */
107 /* Data structure used by the kernel to find robust futexes. */
108 struct robust_list_head
111 long int futex_offset
;
112 void *list_op_pending
;
116 /* Data strcture used to handle thread priority protection. */
117 struct priority_protection_data
120 unsigned int priomap
[];
124 /* Thread descriptor data structure. */
130 /* This overlaps the TCB as used for TLS without threads (see tls.h). */
135 int multiple_threads
;
137 # ifndef __ASSUME_PRIVATE_FUTEX
143 /* This extra padding has no special purpose, and this structure layout
144 is private and subject to change without affecting the official ABI.
145 We just have it here in case it might be convenient for some
146 implementation-specific instrumentation hack or suchlike. */
150 /* This descriptor's link on the `stack_used' or `__stack_user' list. */
153 /* Thread ID - which is also a 'is this thread descriptor (and
154 therefore stack) used' flag. */
157 /* Process ID - thread group ID in kernel speak. */
160 /* List of robust mutexes the thread is holding. */
161 #ifdef __PTHREAD_MUTEX_HAVE_PREV
163 struct robust_list_head robust_head
;
165 /* The list above is strange. It is basically a double linked list
166 but the pointer to the next/previous element of the list points
167 in the middle of the object, the __next element. Whenever
168 casting to __pthread_list_t we need to adjust the pointer
170 # define QUEUE_PTR_ADJUST (offsetof (__pthread_list_t, __next))
172 # define ENQUEUE_MUTEX_BOTH(mutex, val) \
174 __pthread_list_t *next = (__pthread_list_t *) \
175 ((((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_head.list)) & ~1ul) \
176 - QUEUE_PTR_ADJUST); \
177 next->__prev = (void *) &mutex->__data.__list.__next; \
178 mutex->__data.__list.__next = THREAD_GETMEM (THREAD_SELF, \
180 mutex->__data.__list.__prev = (void *) &THREAD_SELF->robust_head; \
181 THREAD_SETMEM (THREAD_SELF, robust_head.list, \
182 (void *) (((uintptr_t) &mutex->__data.__list.__next) \
185 # define DEQUEUE_MUTEX(mutex) \
187 __pthread_list_t *next = (__pthread_list_t *) \
188 ((char *) (((uintptr_t) mutex->__data.__list.__next) & ~1ul) \
189 - QUEUE_PTR_ADJUST); \
190 next->__prev = mutex->__data.__list.__prev; \
191 __pthread_list_t *prev = (__pthread_list_t *) \
192 ((char *) (((uintptr_t) mutex->__data.__list.__prev) & ~1ul) \
193 - QUEUE_PTR_ADJUST); \
194 prev->__next = mutex->__data.__list.__next; \
195 mutex->__data.__list.__prev = NULL; \
196 mutex->__data.__list.__next = NULL; \
201 __pthread_slist_t robust_list
;
202 struct robust_list_head robust_head
;
205 # define ENQUEUE_MUTEX_BOTH(mutex, val) \
207 mutex->__data.__list.__next \
208 = THREAD_GETMEM (THREAD_SELF, robust_list.__next); \
209 THREAD_SETMEM (THREAD_SELF, robust_list.__next, \
210 (void *) (((uintptr_t) &mutex->__data.__list) | val)); \
212 # define DEQUEUE_MUTEX(mutex) \
214 __pthread_slist_t *runp = (__pthread_slist_t *) \
215 (((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_list.__next)) & ~1ul); \
216 if (runp == &mutex->__data.__list) \
217 THREAD_SETMEM (THREAD_SELF, robust_list.__next, runp->__next); \
220 __pthread_slist_t *next = (__pthread_slist_t *) \
221 (((uintptr_t) runp->__next) & ~1ul); \
222 while (next != &mutex->__data.__list) \
225 next = (__pthread_slist_t *) (((uintptr_t) runp->__next) & ~1ul); \
228 runp->__next = next->__next; \
229 mutex->__data.__list.__next = NULL; \
233 #define ENQUEUE_MUTEX(mutex) ENQUEUE_MUTEX_BOTH (mutex, 0)
234 #define ENQUEUE_MUTEX_PI(mutex) ENQUEUE_MUTEX_BOTH (mutex, 1)
236 /* List of cleanup buffers. */
237 struct _pthread_cleanup_buffer
*cleanup
;
239 /* Unwind information. */
240 struct pthread_unwind_buf
*cleanup_jmp_buf
;
241 #define HAVE_CLEANUP_JMP_BUF
243 /* Flags determining processing of cancellation. */
245 /* Bit set if cancellation is disabled. */
246 #define CANCELSTATE_BIT 0
247 #define CANCELSTATE_BITMASK (0x01 << CANCELSTATE_BIT)
248 /* Bit set if asynchronous cancellation mode is selected. */
249 #define CANCELTYPE_BIT 1
250 #define CANCELTYPE_BITMASK (0x01 << CANCELTYPE_BIT)
251 /* Bit set if canceling has been initiated. */
252 #define CANCELING_BIT 2
253 #define CANCELING_BITMASK (0x01 << CANCELING_BIT)
254 /* Bit set if canceled. */
255 #define CANCELED_BIT 3
256 #define CANCELED_BITMASK (0x01 << CANCELED_BIT)
257 /* Bit set if thread is exiting. */
258 #define EXITING_BIT 4
259 #define EXITING_BITMASK (0x01 << EXITING_BIT)
260 /* Bit set if thread terminated and TCB is freed. */
261 #define TERMINATED_BIT 5
262 #define TERMINATED_BITMASK (0x01 << TERMINATED_BIT)
263 /* Bit set if thread is supposed to change XID. */
265 #define SETXID_BITMASK (0x01 << SETXID_BIT)
266 /* Mask for the rest. Helps the compiler to optimize. */
267 #define CANCEL_RESTMASK 0xffffff80
269 #define CANCEL_ENABLED_AND_CANCELED(value) \
270 (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \
271 | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
272 #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
273 (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \
274 | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \
275 == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
277 /* Flags. Including those copied from the thread attribute. */
280 /* We allocate one block of references here. This should be enough
281 to avoid allocating any memory dynamically for most applications. */
282 struct pthread_key_data
284 /* Sequence number. We use uintptr_t to not require padding on
285 32- and 64-bit machines. On 64-bit machines it helps to avoid
291 } specific_1stblock
[PTHREAD_KEY_2NDLEVEL_SIZE
];
293 /* Two-level array for the thread-specific data. */
294 struct pthread_key_data
*specific
[PTHREAD_KEY_1STLEVEL_SIZE
];
296 /* Flag which is set when specific data is set. */
299 /* True if events must be reported. */
302 /* True if the user provided the stack. */
305 /* True if thread must stop at startup time. */
308 /* The parent's cancel handling at the time of the pthread_create
309 call. This might be needed to undo the effects of a cancellation. */
310 int parent_cancelhandling
;
312 /* Lock to synchronize access to the descriptor. */
315 /* Lock for synchronizing setxid calls. */
319 /* Offset of the CPU clock at start thread start time. */
320 hp_timing_t cpuclock_offset
;
323 /* If the thread waits to join another one the ID of the latter is
326 In case a thread is detached this field contains a pointer of the
327 TCB if the thread itself. This is something which cannot happen
328 in normal operation. */
329 struct pthread
*joinid
;
330 /* Check whether a thread is detached. */
331 #define IS_DETACHED(pd) ((pd)->joinid == (pd))
333 /* The result of the thread function. */
336 /* Scheduling parameters for the new thread. */
337 struct sched_param schedparam
;
340 /* Start position of the code to be executed and the argument passed
342 void *(*start_routine
) (void *);
346 td_eventbuf_t eventbuf
;
347 /* Next descriptor with a pending event. */
348 struct pthread
*nextevent
;
350 #ifdef HAVE_FORCED_UNWIND
351 /* Machine-specific unwind info. */
352 struct _Unwind_Exception exc
;
355 /* If nonzero pointer to area allocated for the stack and its
358 size_t stackblock_size
;
359 /* Size of the included guard area. */
361 /* This is what the user specified and what we will report. */
362 size_t reported_guardsize
;
364 /* Thread Priority Protection data. */
365 struct priority_protection_data
*tpp
;
367 /* Resolver state. */
368 struct __res_state res
;
370 /* This member must be last. */
373 #define PTHREAD_STRUCT_END_PADDING \
374 (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
375 } __attribute ((aligned (TCB_ALIGNMENT
)));