]>
Commit | Line | Data |
---|---|---|
33574c17 ST |
1 | /* Internal defenitions for pthreads library. |
2 | Copyright (C) 2000-2018 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | 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 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If | |
17 | not, see <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #ifndef _PT_INTERNAL_H | |
20 | #define _PT_INTERNAL_H 1 | |
21 | ||
22 | #include <pthread.h> | |
23 | #include <stddef.h> | |
24 | #include <sched.h> | |
25 | #include <signal.h> | |
26 | #include <assert.h> | |
27 | #include <bits/types/res_state.h> | |
28 | ||
29 | #include <atomic.h> | |
30 | ||
31 | #include <pt-key.h> | |
32 | ||
33 | #include <pt-sysdep.h> | |
34 | #include <pt-machdep.h> | |
35 | ||
36 | #if IS_IN (libpthread) | |
37 | # include <ldsodefs.h> | |
38 | #endif | |
39 | ||
40 | /* Thread state. */ | |
41 | enum pthread_state | |
42 | { | |
43 | /* The thread is running and joinable. */ | |
44 | PTHREAD_JOINABLE = 0, | |
45 | /* The thread is running and detached. */ | |
46 | PTHREAD_DETACHED, | |
47 | /* A joinable thread exited and its return code is available. */ | |
48 | PTHREAD_EXITED, | |
49 | /* The thread structure is unallocated and available for reuse. */ | |
50 | PTHREAD_TERMINATED | |
51 | }; | |
52 | ||
53 | #ifndef PTHREAD_KEY_MEMBERS | |
54 | # define PTHREAD_KEY_MEMBERS | |
55 | #endif | |
56 | ||
57 | #ifndef PTHREAD_SYSDEP_MEMBERS | |
58 | # define PTHREAD_SYSDEP_MEMBERS | |
59 | #endif | |
60 | ||
61 | #if !(IS_IN (libpthread)) | |
62 | /* Type of the TCB. */ | |
63 | typedef struct | |
64 | { | |
65 | void *tcb; /* Points to this structure. */ | |
66 | void *dtv; /* Vector of pointers to TLS data. */ | |
67 | thread_t self; /* This thread's control port. */ | |
68 | } tcbhead_t; | |
69 | #endif /* ! IS_IN (libpthread) */ | |
70 | ||
71 | /* This structure describes a POSIX thread. */ | |
72 | struct __pthread | |
73 | { | |
74 | /* Thread ID. */ | |
75 | pthread_t thread; | |
76 | ||
77 | unsigned int nr_refs; /* Detached threads have a self reference only, | |
78 | while joinable threads have two references. | |
79 | These are used to keep the structure valid at | |
80 | thread destruction. Detaching/joining a thread | |
81 | drops a reference. */ | |
82 | ||
83 | /* Cancellation. */ | |
84 | pthread_mutex_t cancel_lock; /* Protect cancel_xxx members. */ | |
85 | void (*cancel_hook) (void *); /* Called to unblock a thread blocking | |
86 | in a cancellation point (namely, | |
87 | __pthread_cond_timedwait_internal). */ | |
88 | void *cancel_hook_arg; | |
89 | int cancel_state; | |
90 | int cancel_type; | |
91 | int cancel_pending; | |
92 | struct __pthread_cancelation_handler *cancelation_handlers; | |
93 | ||
94 | /* Thread stack. */ | |
95 | void *stackaddr; | |
96 | size_t stacksize; | |
97 | size_t guardsize; | |
98 | int stack; /* Nonzero if the stack was allocated. */ | |
99 | ||
100 | /* Exit status. */ | |
101 | void *status; | |
102 | ||
103 | /* Thread state. */ | |
104 | enum pthread_state state; | |
105 | pthread_mutex_t state_lock; /* Locks the state. */ | |
106 | pthread_cond_t state_cond; /* Signalled when the state changes. */ | |
107 | ||
108 | /* Resolver state. */ | |
109 | struct __res_state res_state; | |
110 | ||
111 | /* Thread context. */ | |
112 | struct pthread_mcontext mcontext; | |
113 | ||
114 | PTHREAD_KEY_MEMBERS | |
115 | ||
116 | PTHREAD_SYSDEP_MEMBERS | |
117 | ||
118 | tcbhead_t *tcb; | |
119 | ||
120 | /* Queue links. Since PREVP is used to determine if a thread has been | |
121 | awaken, it must be protected by the queue lock. */ | |
122 | struct __pthread *next, **prevp; | |
123 | }; | |
124 | ||
125 | /* Enqueue an element THREAD on the queue *HEAD. */ | |
126 | static inline void | |
127 | __pthread_enqueue (struct __pthread **head, struct __pthread *thread) | |
128 | { | |
129 | assert (thread->prevp == 0); | |
130 | ||
131 | thread->next = *head; | |
132 | thread->prevp = head; | |
133 | if (*head) | |
134 | (*head)->prevp = &thread->next; | |
135 | *head = thread; | |
136 | } | |
137 | ||
138 | /* Dequeue the element THREAD from the queue it is connected to. */ | |
139 | static inline void | |
140 | __pthread_dequeue (struct __pthread *thread) | |
141 | { | |
142 | assert (thread); | |
143 | assert (thread->prevp); | |
144 | ||
145 | if (thread->next) | |
146 | thread->next->prevp = thread->prevp; | |
147 | *thread->prevp = thread->next; | |
148 | thread->prevp = 0; | |
149 | } | |
150 | ||
151 | /* Iterate over QUEUE storing each element in ELEMENT. */ | |
152 | #define __pthread_queue_iterate(queue, element) \ | |
153 | for (struct __pthread *__pdi_next = (queue); \ | |
154 | ((element) = __pdi_next) \ | |
155 | && ((__pdi_next = __pdi_next->next), \ | |
156 | 1); \ | |
157 | ) | |
158 | ||
159 | /* Iterate over QUEUE dequeuing each element, storing it in | |
160 | ELEMENT. */ | |
161 | #define __pthread_dequeuing_iterate(queue, element) \ | |
162 | for (struct __pthread *__pdi_next = (queue); \ | |
163 | ((element) = __pdi_next) \ | |
164 | && ((__pdi_next = __pdi_next->next), \ | |
165 | ((element)->prevp = 0), \ | |
166 | 1); \ | |
167 | ) | |
168 | ||
169 | /* The total number of threads currently active. */ | |
170 | extern unsigned int __pthread_total; | |
171 | ||
172 | /* The total number of thread IDs currently in use, or on the list of | |
173 | available thread IDs. */ | |
174 | extern int __pthread_num_threads; | |
175 | ||
176 | /* Concurrency hint. */ | |
177 | extern int __pthread_concurrency; | |
178 | ||
179 | /* Array of __pthread structures and its lock. Indexed by the pthread | |
180 | id minus one. (Why not just use the pthread id? Because some | |
181 | brain-dead users of the pthread interface incorrectly assume that 0 | |
182 | is an invalid pthread id.) */ | |
183 | extern struct __pthread **__pthread_threads; | |
184 | extern pthread_rwlock_t __pthread_threads_lock; | |
185 | ||
186 | #define __pthread_getid(thread) \ | |
187 | ({ struct __pthread *__t; \ | |
188 | __pthread_rwlock_rdlock (&__pthread_threads_lock); \ | |
189 | __t = __pthread_threads[thread - 1]; \ | |
190 | __pthread_rwlock_unlock (&__pthread_threads_lock); \ | |
191 | __t; }) | |
192 | ||
193 | #define __pthread_setid(thread, pthread) \ | |
194 | __pthread_rwlock_wrlock (&__pthread_threads_lock); \ | |
195 | __pthread_threads[thread - 1] = pthread; \ | |
196 | __pthread_rwlock_unlock (&__pthread_threads_lock); | |
197 | ||
198 | /* Similar to pthread_self, but returns the thread descriptor instead | |
199 | of the thread ID. */ | |
200 | #ifndef _pthread_self | |
201 | extern struct __pthread *_pthread_self (void); | |
202 | #endif | |
203 | \f | |
204 | ||
205 | /* Initialize the pthreads library. */ | |
206 | extern void ___pthread_init (void); | |
207 | ||
208 | /* Internal version of pthread_create. Rather than return the new | |
209 | tid, we return the whole __pthread structure in *PTHREAD. */ | |
210 | extern int __pthread_create_internal (struct __pthread **__restrict pthread, | |
211 | const pthread_attr_t *__restrict attr, | |
212 | void *(*start_routine) (void *), | |
213 | void *__restrict arg); | |
214 | ||
215 | /* Allocate a new thread structure and a pthread thread ID (but not a | |
216 | kernel thread or a stack). THREAD has one reference. */ | |
217 | extern int __pthread_alloc (struct __pthread **thread); | |
218 | ||
219 | /* Deallocate the thread structure. This is the dual of | |
220 | __pthread_alloc (N.B. it does not call __pthread_stack_dealloc nor | |
221 | __pthread_thread_terminate). THREAD loses one reference and is | |
222 | released if the reference counter drops to 0. */ | |
223 | extern void __pthread_dealloc (struct __pthread *thread); | |
224 | ||
225 | ||
226 | /* Allocate a stack of size STACKSIZE. The stack base shall be | |
227 | returned in *STACKADDR. */ | |
228 | extern int __pthread_stack_alloc (void **stackaddr, size_t stacksize); | |
229 | ||
230 | /* Deallocate the stack STACKADDR of size STACKSIZE. */ | |
231 | extern void __pthread_stack_dealloc (void *stackaddr, size_t stacksize); | |
232 | ||
233 | ||
234 | /* Setup thread THREAD's context. */ | |
235 | extern int __pthread_setup (struct __pthread *__restrict thread, | |
236 | void (*entry_point) (struct __pthread *, | |
237 | void *(*)(void *), | |
238 | void *), | |
239 | void *(*start_routine) (void *), | |
240 | void *__restrict arg); | |
241 | ||
242 | ||
243 | /* Allocate a kernel thread (and any miscellaneous system dependent | |
244 | resources) for THREAD; it must not be placed on the run queue. */ | |
245 | extern int __pthread_thread_alloc (struct __pthread *thread); | |
246 | ||
247 | /* Start THREAD making it eligible to run. */ | |
248 | extern int __pthread_thread_start (struct __pthread *thread); | |
249 | ||
250 | /* Terminate the kernel thread associated with THREAD, and deallocate its | |
251 | stack as well as any other kernel resource associated with it. | |
252 | In addition, THREAD looses one reference. | |
253 | ||
254 | This function can be called by any thread, including the target thread. | |
255 | Since some resources that are destroyed along the kernel thread are | |
256 | stored in thread-local variables, the conditions required for this | |
257 | function to behave correctly are a bit unusual : as long as the target | |
258 | thread hasn't been started, any thread can terminate it, but once it | |
259 | has started, no other thread can terminate it, so that thread-local | |
260 | variables created by that thread are correctly released. */ | |
261 | extern void __pthread_thread_terminate (struct __pthread *thread); | |
262 | ||
263 | ||
264 | /* Called by a thread just before it calls the provided start | |
265 | routine. */ | |
266 | extern void __pthread_startup (void); | |
267 | ||
268 | /* Block THREAD. */ | |
269 | extern void __pthread_block (struct __pthread *thread); | |
270 | ||
271 | /* Block THREAD until *ABSTIME is reached. */ | |
272 | extern error_t __pthread_timedblock (struct __pthread *__restrict thread, | |
273 | const struct timespec *__restrict abstime, | |
274 | clockid_t clock_id); | |
275 | ||
276 | /* Wakeup THREAD. */ | |
277 | extern void __pthread_wakeup (struct __pthread *thread); | |
278 | ||
279 | ||
280 | /* Perform a cancelation. The CANCEL_LOCK member of the given thread must | |
281 | be locked before calling this function, which must unlock it. */ | |
282 | extern int __pthread_do_cancel (struct __pthread *thread); | |
283 | ||
284 | ||
285 | /* Initialize the thread specific data structures. THREAD must be the | |
286 | calling thread. */ | |
287 | extern error_t __pthread_init_specific (struct __pthread *thread); | |
288 | ||
289 | /* Call the destructors on all of the thread specific data in THREAD. | |
290 | THREAD must be the calling thread. */ | |
291 | extern void __pthread_destroy_specific (struct __pthread *thread); | |
292 | ||
293 | ||
294 | /* Initialize newly create thread *THREAD's signal state data | |
295 | structures. */ | |
296 | extern error_t __pthread_sigstate_init (struct __pthread *thread); | |
297 | ||
298 | /* Destroy the signal state data structures associcated with thread | |
299 | *THREAD. */ | |
300 | extern void __pthread_sigstate_destroy (struct __pthread *thread); | |
301 | ||
302 | /* Modify thread *THREAD's signal state. */ | |
303 | extern error_t __pthread_sigstate (struct __pthread *__restrict thread, int how, | |
304 | const sigset_t *__restrict set, | |
305 | sigset_t *__restrict oset, | |
306 | int clear_pending); | |
307 | \f | |
308 | ||
309 | /* Default thread attributes. */ | |
310 | extern const struct __pthread_attr __pthread_default_attr; | |
311 | ||
312 | /* Default barrier attributes. */ | |
313 | extern const struct __pthread_barrierattr __pthread_default_barrierattr; | |
314 | ||
315 | /* Default mutex attributes. */ | |
316 | extern const struct __pthread_mutexattr __pthread_default_mutexattr; | |
317 | ||
318 | /* Default rdlock attributes. */ | |
319 | extern const struct __pthread_rwlockattr __pthread_default_rwlockattr; | |
320 | ||
321 | /* Default condition attributes. */ | |
322 | extern const struct __pthread_condattr __pthread_default_condattr; | |
323 | ||
324 | #endif /* pt-internal.h */ |