Initial revision
[glibc.git] / nptl / descr.h
blobf3a9620a7bd5f3cd4c909818b1274c05f1413b8e
1 /* Copyright (C) 2002 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
18 02111-1307 USA. */
20 #ifndef _DESCR_H
21 #define _DESCR_H 1
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 "../nptl_db/thread_db.h"
35 #ifndef TCB_ALIGNMENT
36 # define TCB_ALIGNMENT sizeof (double)
37 #endif
40 /* We keep thread specific data in a special data structure, a two-level
41 array. The top-level array contains pointers to dynamically allocated
42 arrays of a certain number of data pointers. So we can implement a
43 sparse array. Each dynamic second-level array has
44 PTHREAD_KEY_2NDLEVEL_SIZE
45 entries. This value shouldn't be too large. */
46 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
48 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
49 keys in each subarray. */
50 #define PTHREAD_KEY_1STLEVEL_SIZE \
51 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
52 / PTHREAD_KEY_2NDLEVEL_SIZE)
55 /* Thread descriptor data structure. */
56 struct pthread
58 /* XXX Remove this union for IA-64 style TLS module */
59 union
61 struct
63 void *tcb; /* Pointer to the TCB. This is not always
64 the address of this thread descriptor. */
65 union dtv *dtvp;
66 struct pthread *self; /* Pointer to this structure */
67 list_t list;
68 } data;
69 void *__padding[16];
70 } header;
72 /* Two-level array for the thread-specific data. */
73 struct pthread_key_data
75 /* Sequence number. We use uintptr_t to not require padding on
76 32- and 64-bit machines. On 64-bit machines it helps to avoid
77 wrapping, too. */
78 uintptr_t seq;
80 /* Data pointer. */
81 void *data;
82 } *specific[PTHREAD_KEY_1STLEVEL_SIZE];
83 /* We allocate one block of references here. This should be enough
84 to avoid allocating any memory dynamically for most applications. */
85 struct pthread_key_data specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE];
86 /* Flag which is set when specific data is set. */
87 bool specific_used;
89 /* True if the user provided the stack. */
90 bool user_stack;
92 /* True if events must be reported. */
93 bool report_events;
95 /* Lock to syncronize access to the descriptor. */
96 lll_lock_t lock;
98 #if HP_TIMING_AVAIL
99 /* Offset of the CPU clock at start thread start time. */
100 hp_timing_t cpuclock_offset;
101 #endif
103 /* If the thread waits to join another one the ID of the latter is
104 stored here.
106 In case a thread is detached this field contains a pointer of the
107 TCB if the thread itself. This is something which cannot happen
108 in normal operation. */
109 struct pthread *joinid;
110 /* Check whether a thread is detached. */
111 #define IS_DETACHED(pd) ((pd)->joinid == (pd))
113 /* List of cleanup buffers. */
114 struct _pthread_cleanup_buffer *cleanup;
115 /* Flags determining processing of cancellation. */
116 int cancelhandling;
117 /* Bit set if cancellation is disabled. */
118 #define CANCELSTATE_BIT 0
119 #define CANCELSTATE_BITMASK 0x01
120 /* Bit set if asynchronous cancellation mode is selected. */
121 #define CANCELTYPE_BIT 1
122 #define CANCELTYPE_BITMASK 0x02
123 /* Bit set if canceled. */
124 #define CANCELED_BIT 2
125 #define CANCELED_BITMASK 0x04
126 /* Bit set if thread is exiting. */
127 #define EXITING_BIT 3
128 #define EXITING_BITMASK 0x08
129 /* Bit set if thread terminated and TCB is freed. */
130 #define TERMINATED_BIT 4
131 #define TERMINATED_BITMASK 0x10
132 /* Mask for the rest. Helps the compiler to optimize. */
133 #define CANCEL_RESTMASK 0xffffffe0
135 #define CANCEL_ENABLED_AND_CANCELED(value) \
136 (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \
137 | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
138 #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
139 (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \
140 | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \
141 == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
142 /* Setjmp buffer to be used if try/finally is not available. */
143 sigjmp_buf cancelbuf;
144 #define HAVE_CANCELBUF 1
146 /* Thread ID - which is also a 'is this thread descriptor (and
147 therefore stack) used' flag. */
148 pid_t tid;
150 /* Flags. Including those copied from the thread attribute. */
151 int flags;
153 /* The result of the thread function. */
154 void *result;
156 /* Scheduling parameters for the new thread. */
157 struct sched_param schedparam;
158 int schedpolicy;
160 /* Start position of the code to be executed and the argument passed
161 to the function. */
162 void *(*start_routine) (void *);
163 void *arg;
165 /* Debug state. */
166 td_eventbuf_t eventbuf;
167 /* Next descriptor with a pending event. */
168 struct pthread *nextevent;
170 /* If nonzero pointer to area allocated for the stack and its
171 size. */
172 void *stackblock;
173 size_t stackblock_size;
174 /* Size of the included guard area. */
175 size_t guardsize;
176 } __attribute ((aligned (TCB_ALIGNMENT)));
179 #endif /* descr.h */