]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/descr.h
20e44af3beee9bcd9a2021c2c97fbe7d8fc1357a
[thirdparty/glibc.git] / nptl / descr.h
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
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
18 02111-1307 USA. */
19
20 #ifndef _DESCR_H
21 #define _DESCR_H 1
22
23 #include <limits.h>
24 #include <sched.h>
25 #include <setjmp.h>
26 #include <stdbool.h>
27 #include <sys/types.h>
28 #include <hp-timing.h>
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
36
37 #ifndef TCB_ALIGNMENT
38 # define TCB_ALIGNMENT sizeof (double)
39 #endif
40
41
42 /* We keep thread specific data in a special data structure, a two-level
43 array. The top-level array contains pointers to dynamically allocated
44 arrays of a certain number of data pointers. So we can implement a
45 sparse array. Each dynamic second-level array has
46 PTHREAD_KEY_2NDLEVEL_SIZE
47 entries. This value shouldn't be too large. */
48 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
49
50 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
51 keys in each subarray. */
52 #define PTHREAD_KEY_1STLEVEL_SIZE \
53 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
54 / PTHREAD_KEY_2NDLEVEL_SIZE)
55
56
57 /* Thread descriptor data structure. */
58 struct pthread
59 {
60 union
61 {
62 #if !TLS_DTV_AT_TP
63 /* This overlaps the TCB as used for TLS without threads (see tls.h). */
64 tcbhead_t header;
65 #elif TLS_MULTIPLE_THREADS_IN_TCB
66 struct
67 {
68 int multiple_threads;
69 } header;
70 #endif
71
72 /* This extra padding has no special purpose, and this structure layout
73 is private and subject to change without affecting the official ABI.
74 We just have it here in case it might be convenient for some
75 implementation-specific instrumentation hack or suchlike. */
76 void *__padding[16];
77 };
78
79 /* This descriptor's link on the `stack_used' or `__stack_user' list. */
80 list_t list;
81
82 /* Thread ID - which is also a 'is this thread descriptor (and
83 therefore stack) used' flag. */
84 pid_t tid;
85
86 /* List of cleanup buffers. */
87 struct _pthread_cleanup_buffer *cleanup;
88
89 /* True if events must be reported. */
90 bool report_events;
91
92 /* We allocate one block of references here. This should be enough
93 to avoid allocating any memory dynamically for most applications. */
94 struct pthread_key_data
95 {
96 /* Sequence number. We use uintptr_t to not require padding on
97 32- and 64-bit machines. On 64-bit machines it helps to avoid
98 wrapping, too. */
99 uintptr_t seq;
100
101 /* Data pointer. */
102 void *data;
103 } specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE];
104
105 /* Flag which is set when specific data is set. */
106 bool specific_used;
107
108 /* Two-level array for the thread-specific data. */
109 struct pthread_key_data *specific[PTHREAD_KEY_1STLEVEL_SIZE];
110
111 /* True if the user provided the stack. */
112 bool user_stack;
113
114 /* Lock to synchronize access to the descriptor. */
115 lll_lock_t lock;
116
117 #if HP_TIMING_AVAIL
118 /* Offset of the CPU clock at start thread start time. */
119 hp_timing_t cpuclock_offset;
120 #endif
121
122 /* If the thread waits to join another one the ID of the latter is
123 stored here.
124
125 In case a thread is detached this field contains a pointer of the
126 TCB if the thread itself. This is something which cannot happen
127 in normal operation. */
128 struct pthread *joinid;
129 /* Check whether a thread is detached. */
130 #define IS_DETACHED(pd) ((pd)->joinid == (pd))
131
132 /* Flags determining processing of cancellation. */
133 int cancelhandling;
134 /* Bit set if cancellation is disabled. */
135 #define CANCELSTATE_BIT 0
136 #define CANCELSTATE_BITMASK 0x01
137 /* Bit set if asynchronous cancellation mode is selected. */
138 #define CANCELTYPE_BIT 1
139 #define CANCELTYPE_BITMASK 0x02
140 /* Bit set if canceling has been initiated. */
141 #define CANCELING_BIT 2
142 #define CANCELING_BITMASK 0x04
143 /* Bit set if canceled. */
144 #define CANCELED_BIT 3
145 #define CANCELED_BITMASK 0x08
146 /* Bit set if thread is exiting. */
147 #define EXITING_BIT 4
148 #define EXITING_BITMASK 0x10
149 /* Bit set if thread terminated and TCB is freed. */
150 #define TERMINATED_BIT 5
151 #define TERMINATED_BITMASK 0x20
152 /* Mask for the rest. Helps the compiler to optimize. */
153 #define CANCEL_RESTMASK 0xffffffc0
154
155 #define CANCEL_ENABLED_AND_CANCELED(value) \
156 (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \
157 | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
158 #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
159 (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \
160 | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \
161 == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
162 /* Setjmp buffer to be used if try/finally is not available. */
163 sigjmp_buf cancelbuf;
164 #define HAVE_CANCELBUF 1
165
166 /* Flags. Including those copied from the thread attribute. */
167 int flags;
168
169 /* The result of the thread function. */
170 void *result;
171
172 /* Scheduling parameters for the new thread. */
173 struct sched_param schedparam;
174 int schedpolicy;
175
176 /* Start position of the code to be executed and the argument passed
177 to the function. */
178 void *(*start_routine) (void *);
179 void *arg;
180
181 /* Debug state. */
182 td_eventbuf_t eventbuf;
183 /* Next descriptor with a pending event. */
184 struct pthread *nextevent;
185
186 /* If nonzero pointer to area allocated for the stack and its
187 size. */
188 void *stackblock;
189 size_t stackblock_size;
190 /* Size of the included guard area. */
191 size_t guardsize;
192 } __attribute ((aligned (TCB_ALIGNMENT)));
193
194
195 #endif /* descr.h */