1 /* Internal defenitions for pthreads library.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <https://www.gnu.org/licenses/>. */
19 #ifndef _PT_INTERNAL_H
20 #define _PT_INTERNAL_H 1
27 #include <bits/types/res_state.h>
33 #include <pt-sysdep.h>
34 #include <pt-machdep.h>
36 #if IS_IN (libpthread)
37 # include <ldsodefs.h>
45 /* The thread is running and joinable. */
47 /* The thread is running and detached. */
49 /* A joinable thread exited and its return code is available. */
51 /* The thread structure is unallocated and available for reuse. */
55 #ifndef PTHREAD_KEY_MEMBERS
56 # define PTHREAD_KEY_MEMBERS
59 #ifndef PTHREAD_SYSDEP_MEMBERS
60 # define PTHREAD_SYSDEP_MEMBERS
63 /* This structure describes a POSIX thread. */
69 unsigned int nr_refs
; /* Detached threads have a self reference only,
70 while joinable threads have two references.
71 These are used to keep the structure valid at
72 thread destruction. Detaching/joining a thread
76 pthread_mutex_t cancel_lock
; /* Protect cancel_xxx members. */
77 void (*cancel_hook
) (void *); /* Called to unblock a thread blocking
78 in a cancellation point (namely,
79 __pthread_cond_timedwait_internal). */
80 void *cancel_hook_arg
;
84 struct __pthread_cancelation_handler
*cancelation_handlers
;
90 int stack
; /* Nonzero if the stack was allocated. */
96 enum pthread_state state
;
97 pthread_mutex_t state_lock
; /* Locks the state. */
98 pthread_cond_t state_cond
; /* Signalled when the state changes. */
100 /* Resolver state. */
101 struct __res_state res_state
;
103 /* Thread context. */
104 struct pthread_mcontext mcontext
;
108 PTHREAD_SYSDEP_MEMBERS
112 /* Queue links. Since PREVP is used to determine if a thread has been
113 awaken, it must be protected by the queue lock. */
114 struct __pthread
*next
, **prevp
;
117 /* Enqueue an element THREAD on the queue *HEAD. */
119 __pthread_enqueue (struct __pthread
**head
, struct __pthread
*thread
)
121 assert (thread
->prevp
== 0);
123 thread
->next
= *head
;
124 thread
->prevp
= head
;
126 (*head
)->prevp
= &thread
->next
;
130 /* Dequeue the element THREAD from the queue it is connected to. */
132 __pthread_dequeue (struct __pthread
*thread
)
135 assert (thread
->prevp
);
138 thread
->next
->prevp
= thread
->prevp
;
139 *thread
->prevp
= thread
->next
;
143 /* Iterate over QUEUE storing each element in ELEMENT. */
144 #define __pthread_queue_iterate(queue, element) \
145 for (struct __pthread *__pdi_next = (queue); \
146 ((element) = __pdi_next) \
147 && ((__pdi_next = __pdi_next->next), \
151 /* Iterate over QUEUE dequeuing each element, storing it in
153 #define __pthread_dequeuing_iterate(queue, element) \
154 for (struct __pthread *__pdi_next = (queue); \
155 ((element) = __pdi_next) \
156 && ((__pdi_next = __pdi_next->next), \
157 ((element)->prevp = 0), \
161 /* The total number of threads currently active. */
162 extern unsigned int __pthread_total
;
164 /* The total number of thread IDs currently in use, or on the list of
165 available thread IDs. */
166 extern int __pthread_num_threads
;
168 /* Concurrency hint. */
169 extern int __pthread_concurrency
;
171 /* Array of __pthread structures and its lock. Indexed by the pthread
172 id minus one. (Why not just use the pthread id? Because some
173 brain-dead users of the pthread interface incorrectly assume that 0
174 is an invalid pthread id.) */
175 extern struct __pthread
**__pthread_threads
;
176 extern pthread_rwlock_t __pthread_threads_lock
;
178 #define __pthread_getid(thread) \
179 ({ struct __pthread *__t; \
180 __pthread_rwlock_rdlock (&__pthread_threads_lock); \
181 __t = __pthread_threads[thread - 1]; \
182 __pthread_rwlock_unlock (&__pthread_threads_lock); \
185 #define __pthread_setid(thread, pthread) \
186 __pthread_rwlock_wrlock (&__pthread_threads_lock); \
187 __pthread_threads[thread - 1] = pthread; \
188 __pthread_rwlock_unlock (&__pthread_threads_lock);
190 /* Similar to pthread_self, but returns the thread descriptor instead
192 #ifndef _pthread_self
193 extern struct __pthread
*_pthread_self (void);
197 /* Initialize the pthreads library. */
198 extern void ___pthread_init (void);
200 /* Internal version of pthread_create. Rather than return the new
201 tid, we return the whole __pthread structure in *PTHREAD. */
202 extern int __pthread_create_internal (struct __pthread
**__restrict pthread
,
203 const pthread_attr_t
*__restrict attr
,
204 void *(*start_routine
) (void *),
205 void *__restrict arg
);
207 /* Allocate a new thread structure and a pthread thread ID (but not a
208 kernel thread or a stack). THREAD has one reference. */
209 extern int __pthread_alloc (struct __pthread
**thread
);
211 /* Deallocate the thread structure. This is the dual of
212 __pthread_alloc (N.B. it does not call __pthread_stack_dealloc nor
213 __pthread_thread_terminate). THREAD loses one reference and is
214 released if the reference counter drops to 0. */
215 extern void __pthread_dealloc (struct __pthread
*thread
);
218 /* Allocate a stack of size STACKSIZE. The stack base shall be
219 returned in *STACKADDR. */
220 extern int __pthread_stack_alloc (void **stackaddr
, size_t stacksize
);
222 /* Deallocate the stack STACKADDR of size STACKSIZE. */
223 extern void __pthread_stack_dealloc (void *stackaddr
, size_t stacksize
);
226 /* Setup thread THREAD's context. */
227 extern int __pthread_setup (struct __pthread
*__restrict thread
,
228 void (*entry_point
) (struct __pthread
*,
231 void *(*start_routine
) (void *),
232 void *__restrict arg
);
235 /* Allocate a kernel thread (and any miscellaneous system dependent
236 resources) for THREAD; it must not be placed on the run queue. */
237 extern int __pthread_thread_alloc (struct __pthread
*thread
);
239 /* Start THREAD making it eligible to run. */
240 extern int __pthread_thread_start (struct __pthread
*thread
);
242 /* Terminate the kernel thread associated with THREAD, and deallocate its
243 stack as well as any other kernel resource associated with it.
244 In addition, THREAD looses one reference.
246 This function can be called by any thread, including the target thread.
247 Since some resources that are destroyed along the kernel thread are
248 stored in thread-local variables, the conditions required for this
249 function to behave correctly are a bit unusual : as long as the target
250 thread hasn't been started, any thread can terminate it, but once it
251 has started, no other thread can terminate it, so that thread-local
252 variables created by that thread are correctly released. */
253 extern void __pthread_thread_terminate (struct __pthread
*thread
);
256 /* Called by a thread just before it calls the provided start
258 extern void __pthread_startup (void);
261 extern void __pthread_block (struct __pthread
*thread
);
263 /* Block THREAD until *ABSTIME is reached. */
264 extern error_t
__pthread_timedblock (struct __pthread
*__restrict thread
,
265 const struct timespec
*__restrict abstime
,
269 extern void __pthread_wakeup (struct __pthread
*thread
);
272 /* Perform a cancelation. The CANCEL_LOCK member of the given thread must
273 be locked before calling this function, which must unlock it. */
274 extern int __pthread_do_cancel (struct __pthread
*thread
);
277 /* Initialize the thread specific data structures. THREAD must be the
279 extern error_t
__pthread_init_specific (struct __pthread
*thread
);
281 /* Call the destructors on all of the thread specific data in THREAD.
282 THREAD must be the calling thread. */
283 extern void __pthread_destroy_specific (struct __pthread
*thread
);
286 /* Initialize newly create thread *THREAD's signal state data
288 extern error_t
__pthread_sigstate_init (struct __pthread
*thread
);
290 /* Destroy the signal state data structures associcated with thread
292 extern void __pthread_sigstate_destroy (struct __pthread
*thread
);
294 /* Modify thread *THREAD's signal state. */
295 extern error_t
__pthread_sigstate (struct __pthread
*__restrict thread
, int how
,
296 const sigset_t
*__restrict set
,
297 sigset_t
*__restrict oset
,
301 /* Default thread attributes. */
302 extern const struct __pthread_attr __pthread_default_attr
;
304 /* Default barrier attributes. */
305 extern const struct __pthread_barrierattr __pthread_default_barrierattr
;
307 /* Default mutex attributes. */
308 extern const struct __pthread_mutexattr __pthread_default_mutexattr
;
310 /* Default rdlock attributes. */
311 extern const struct __pthread_rwlockattr __pthread_default_rwlockattr
;
313 /* Default condition attributes. */
314 extern const struct __pthread_condattr __pthread_default_condattr
;
316 #endif /* pt-internal.h */