Split timed-wait functions out of nptl/lowlevellock.c.
[glibc.git] / nptl / descr.h
blob5bd12826494910d59f5ea47b2813857d91d87554
1 /* Copyright (C) 2002-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _DESCR_H
20 #define _DESCR_H 1
22 #include <limits.h>
23 #include <sched.h>
24 #include <setjmp.h>
25 #include <stdbool.h>
26 #include <sys/types.h>
27 #include <hp-timing.h>
28 #define __need_list_t
29 #include <list.h>
30 #include <lowlevellock.h>
31 #include <pthreaddef.h>
32 #include <dl-sysdep.h>
33 #include "../nptl_db/thread_db.h"
34 #include <tls.h>
35 #ifdef HAVE_FORCED_UNWIND
36 # include <unwind.h>
37 #endif
38 #define __need_res_state
39 #include <resolv.h>
40 #include <kernel-features.h>
42 #ifndef TCB_ALIGNMENT
43 # define TCB_ALIGNMENT sizeof (double)
44 #endif
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
65 information. */
66 struct pthread_unwind_buf
68 struct
70 __jmp_buf jmp_buf;
71 int mask_was_saved;
72 } cancel_jmp_buf[1];
74 union
76 /* This is the placeholder of the public version. */
77 void *pad[4];
79 struct
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
86 installment. */
87 struct _pthread_cleanup_buffer *cleanup;
89 /* Cancellation type before the push call. */
90 int canceltype;
91 } data;
92 } priv;
96 /* Opcodes and data types for communication with the signal handler to
97 change user/group IDs. */
98 struct xid_command
100 int syscall_no;
101 long int id[3];
102 volatile int cntr;
103 volatile int error; /* -1: no call yet, 0: success seen, >0: error seen. */
107 /* Data structure used by the kernel to find robust futexes. */
108 struct robust_list_head
110 void *list;
111 long int futex_offset;
112 void *list_op_pending;
116 /* Data strcture used to handle thread priority protection. */
117 struct priority_protection_data
119 int priomax;
120 unsigned int priomap[];
124 /* Thread descriptor data structure. */
125 struct pthread
127 union
129 #if !TLS_DTV_AT_TP
130 /* This overlaps the TCB as used for TLS without threads (see tls.h). */
131 tcbhead_t header;
132 #else
133 struct
135 /* multiple_threads is enabled either when the process has spawned at
136 least one thread or when a single-threaded process cancels itself.
137 This enables additional code to introduce locking before doing some
138 compare_and_exchange operations and also enable cancellation points.
139 The concepts of multiple threads and cancellation points ideally
140 should be separate, since it is not necessary for multiple threads to
141 have been created for cancellation points to be enabled, as is the
142 case is when single-threaded process cancels itself.
144 Since enabling multiple_threads enables additional code in
145 cancellation points and compare_and_exchange operations, there is a
146 potential for an unneeded performance hit when it is enabled in a
147 single-threaded, self-canceling process. This is OK though, since a
148 single-threaded process will enable async cancellation only when it
149 looks to cancel itself and is hence going to end anyway. */
150 int multiple_threads;
151 int gscope_flag;
152 # ifndef __ASSUME_PRIVATE_FUTEX
153 int private_futex;
154 # endif
155 } header;
156 #endif
158 /* This extra padding has no special purpose, and this structure layout
159 is private and subject to change without affecting the official ABI.
160 We just have it here in case it might be convenient for some
161 implementation-specific instrumentation hack or suchlike. */
162 void *__padding[24];
165 /* This descriptor's link on the `stack_used' or `__stack_user' list. */
166 list_t list;
168 /* Thread ID - which is also a 'is this thread descriptor (and
169 therefore stack) used' flag. */
170 pid_t tid;
172 /* Process ID - thread group ID in kernel speak. */
173 pid_t pid;
175 /* List of robust mutexes the thread is holding. */
176 #ifdef __PTHREAD_MUTEX_HAVE_PREV
177 void *robust_prev;
178 struct robust_list_head robust_head;
180 /* The list above is strange. It is basically a double linked list
181 but the pointer to the next/previous element of the list points
182 in the middle of the object, the __next element. Whenever
183 casting to __pthread_list_t we need to adjust the pointer
184 first. */
185 # define QUEUE_PTR_ADJUST (offsetof (__pthread_list_t, __next))
187 # define ENQUEUE_MUTEX_BOTH(mutex, val) \
188 do { \
189 __pthread_list_t *next = (__pthread_list_t *) \
190 ((((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_head.list)) & ~1ul) \
191 - QUEUE_PTR_ADJUST); \
192 next->__prev = (void *) &mutex->__data.__list.__next; \
193 mutex->__data.__list.__next = THREAD_GETMEM (THREAD_SELF, \
194 robust_head.list); \
195 mutex->__data.__list.__prev = (void *) &THREAD_SELF->robust_head; \
196 THREAD_SETMEM (THREAD_SELF, robust_head.list, \
197 (void *) (((uintptr_t) &mutex->__data.__list.__next) \
198 | val)); \
199 } while (0)
200 # define DEQUEUE_MUTEX(mutex) \
201 do { \
202 __pthread_list_t *next = (__pthread_list_t *) \
203 ((char *) (((uintptr_t) mutex->__data.__list.__next) & ~1ul) \
204 - QUEUE_PTR_ADJUST); \
205 next->__prev = mutex->__data.__list.__prev; \
206 __pthread_list_t *prev = (__pthread_list_t *) \
207 ((char *) (((uintptr_t) mutex->__data.__list.__prev) & ~1ul) \
208 - QUEUE_PTR_ADJUST); \
209 prev->__next = mutex->__data.__list.__next; \
210 mutex->__data.__list.__prev = NULL; \
211 mutex->__data.__list.__next = NULL; \
212 } while (0)
213 #else
214 union
216 __pthread_slist_t robust_list;
217 struct robust_list_head robust_head;
220 # define ENQUEUE_MUTEX_BOTH(mutex, val) \
221 do { \
222 mutex->__data.__list.__next \
223 = THREAD_GETMEM (THREAD_SELF, robust_list.__next); \
224 THREAD_SETMEM (THREAD_SELF, robust_list.__next, \
225 (void *) (((uintptr_t) &mutex->__data.__list) | val)); \
226 } while (0)
227 # define DEQUEUE_MUTEX(mutex) \
228 do { \
229 __pthread_slist_t *runp = (__pthread_slist_t *) \
230 (((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_list.__next)) & ~1ul); \
231 if (runp == &mutex->__data.__list) \
232 THREAD_SETMEM (THREAD_SELF, robust_list.__next, runp->__next); \
233 else \
235 __pthread_slist_t *next = (__pthread_slist_t *) \
236 (((uintptr_t) runp->__next) & ~1ul); \
237 while (next != &mutex->__data.__list) \
239 runp = next; \
240 next = (__pthread_slist_t *) (((uintptr_t) runp->__next) & ~1ul); \
243 runp->__next = next->__next; \
244 mutex->__data.__list.__next = NULL; \
246 } while (0)
247 #endif
248 #define ENQUEUE_MUTEX(mutex) ENQUEUE_MUTEX_BOTH (mutex, 0)
249 #define ENQUEUE_MUTEX_PI(mutex) ENQUEUE_MUTEX_BOTH (mutex, 1)
251 /* List of cleanup buffers. */
252 struct _pthread_cleanup_buffer *cleanup;
254 /* Unwind information. */
255 struct pthread_unwind_buf *cleanup_jmp_buf;
256 #define HAVE_CLEANUP_JMP_BUF
258 /* Flags determining processing of cancellation. */
259 int cancelhandling;
260 /* Bit set if cancellation is disabled. */
261 #define CANCELSTATE_BIT 0
262 #define CANCELSTATE_BITMASK (0x01 << CANCELSTATE_BIT)
263 /* Bit set if asynchronous cancellation mode is selected. */
264 #define CANCELTYPE_BIT 1
265 #define CANCELTYPE_BITMASK (0x01 << CANCELTYPE_BIT)
266 /* Bit set if canceling has been initiated. */
267 #define CANCELING_BIT 2
268 #define CANCELING_BITMASK (0x01 << CANCELING_BIT)
269 /* Bit set if canceled. */
270 #define CANCELED_BIT 3
271 #define CANCELED_BITMASK (0x01 << CANCELED_BIT)
272 /* Bit set if thread is exiting. */
273 #define EXITING_BIT 4
274 #define EXITING_BITMASK (0x01 << EXITING_BIT)
275 /* Bit set if thread terminated and TCB is freed. */
276 #define TERMINATED_BIT 5
277 #define TERMINATED_BITMASK (0x01 << TERMINATED_BIT)
278 /* Bit set if thread is supposed to change XID. */
279 #define SETXID_BIT 6
280 #define SETXID_BITMASK (0x01 << SETXID_BIT)
281 /* Mask for the rest. Helps the compiler to optimize. */
282 #define CANCEL_RESTMASK 0xffffff80
284 #define CANCEL_ENABLED_AND_CANCELED(value) \
285 (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \
286 | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
287 #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
288 (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \
289 | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \
290 == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
292 /* Flags. Including those copied from the thread attribute. */
293 int flags;
295 /* We allocate one block of references here. This should be enough
296 to avoid allocating any memory dynamically for most applications. */
297 struct pthread_key_data
299 /* Sequence number. We use uintptr_t to not require padding on
300 32- and 64-bit machines. On 64-bit machines it helps to avoid
301 wrapping, too. */
302 uintptr_t seq;
304 /* Data pointer. */
305 void *data;
306 } specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE];
308 /* Two-level array for the thread-specific data. */
309 struct pthread_key_data *specific[PTHREAD_KEY_1STLEVEL_SIZE];
311 /* Flag which is set when specific data is set. */
312 bool specific_used;
314 /* True if events must be reported. */
315 bool report_events;
317 /* True if the user provided the stack. */
318 bool user_stack;
320 /* True if thread must stop at startup time. */
321 bool stopped_start;
323 /* The parent's cancel handling at the time of the pthread_create
324 call. This might be needed to undo the effects of a cancellation. */
325 int parent_cancelhandling;
327 /* Lock to synchronize access to the descriptor. */
328 int lock;
330 /* Lock for synchronizing setxid calls. */
331 int setxid_futex;
333 #if HP_TIMING_AVAIL
334 /* Offset of the CPU clock at start thread start time. */
335 hp_timing_t cpuclock_offset;
336 #endif
338 /* If the thread waits to join another one the ID of the latter is
339 stored here.
341 In case a thread is detached this field contains a pointer of the
342 TCB if the thread itself. This is something which cannot happen
343 in normal operation. */
344 struct pthread *joinid;
345 /* Check whether a thread is detached. */
346 #define IS_DETACHED(pd) ((pd)->joinid == (pd))
348 /* The result of the thread function. */
349 void *result;
351 /* Scheduling parameters for the new thread. */
352 struct sched_param schedparam;
353 int schedpolicy;
355 /* Start position of the code to be executed and the argument passed
356 to the function. */
357 void *(*start_routine) (void *);
358 void *arg;
360 /* Debug state. */
361 td_eventbuf_t eventbuf;
362 /* Next descriptor with a pending event. */
363 struct pthread *nextevent;
365 #ifdef HAVE_FORCED_UNWIND
366 /* Machine-specific unwind info. */
367 struct _Unwind_Exception exc;
368 #endif
370 /* If nonzero pointer to area allocated for the stack and its
371 size. */
372 void *stackblock;
373 size_t stackblock_size;
374 /* Size of the included guard area. */
375 size_t guardsize;
376 /* This is what the user specified and what we will report. */
377 size_t reported_guardsize;
379 /* Thread Priority Protection data. */
380 struct priority_protection_data *tpp;
382 /* Resolver state. */
383 struct __res_state res;
385 /* This member must be last. */
386 char end_padding[];
388 #define PTHREAD_STRUCT_END_PADDING \
389 (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
390 } __attribute ((aligned (TCB_ALIGNMENT)));
393 #endif /* descr.h */