Add C2x BOOL_MAX and BOOL_WIDTH to limits.h.
[glibc.git] / htl / pt-internal.h
blobe0baa6bcdadb0b38c2483d47e2761cbac36320a1
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
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>
29 #include <atomic.h>
31 #include <pt-key.h>
33 #include <pt-sysdep.h>
34 #include <pt-machdep.h>
36 #if IS_IN (libpthread)
37 # include <ldsodefs.h>
38 #endif
40 #include <tls.h>
42 /* Thread state. */
43 enum pthread_state
45 /* The thread is running and joinable. */
46 PTHREAD_JOINABLE = 0,
47 /* The thread is running and detached. */
48 PTHREAD_DETACHED,
49 /* A joinable thread exited and its return code is available. */
50 PTHREAD_EXITED,
51 /* The thread structure is unallocated and available for reuse. */
52 PTHREAD_TERMINATED
55 #ifndef PTHREAD_KEY_MEMBERS
56 # define PTHREAD_KEY_MEMBERS
57 #endif
59 #ifndef PTHREAD_SYSDEP_MEMBERS
60 # define PTHREAD_SYSDEP_MEMBERS
61 #endif
63 /* This structure describes a POSIX thread. */
64 struct __pthread
66 /* Thread ID. */
67 pthread_t 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
73 drops a reference. */
75 /* Cancellation. */
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;
81 int cancel_state;
82 int cancel_type;
83 int cancel_pending;
85 /* Thread stack. */
86 void *stackaddr;
87 size_t stacksize;
88 size_t guardsize;
89 int stack; /* Nonzero if the stack was allocated. */
91 /* Exit status. */
92 void *status;
94 /* Thread state. */
95 enum pthread_state state;
96 pthread_mutex_t state_lock; /* Locks the state. */
97 pthread_cond_t state_cond; /* Signalled when the state changes. */
99 /* Resolver state. */
100 struct __res_state res_state;
102 /* Indicates whether is a C11 thread created by thrd_creat. */
103 bool c11;
105 /* Thread context. */
106 struct pthread_mcontext mcontext;
108 PTHREAD_KEY_MEMBERS
110 PTHREAD_SYSDEP_MEMBERS
112 tcbhead_t *tcb;
114 /* Queue links. Since PREVP is used to determine if a thread has been
115 awaken, it must be protected by the queue lock. */
116 struct __pthread *next, **prevp;
119 /* Enqueue an element THREAD on the queue *HEAD. */
120 static inline void
121 __pthread_enqueue (struct __pthread **head, struct __pthread *thread)
123 assert (thread->prevp == 0);
125 thread->next = *head;
126 thread->prevp = head;
127 if (*head)
128 (*head)->prevp = &thread->next;
129 *head = thread;
132 /* Dequeue the element THREAD from the queue it is connected to. */
133 static inline void
134 __pthread_dequeue (struct __pthread *thread)
136 assert (thread);
137 assert (thread->prevp);
139 if (thread->next)
140 thread->next->prevp = thread->prevp;
141 *thread->prevp = thread->next;
142 thread->prevp = 0;
145 /* Iterate over QUEUE storing each element in ELEMENT. */
146 #define __pthread_queue_iterate(queue, element) \
147 for (struct __pthread *__pdi_next = (queue); \
148 ((element) = __pdi_next) \
149 && ((__pdi_next = __pdi_next->next), \
150 1); \
153 /* Iterate over QUEUE dequeuing each element, storing it in
154 ELEMENT. */
155 #define __pthread_dequeuing_iterate(queue, element) \
156 for (struct __pthread *__pdi_next = (queue); \
157 ((element) = __pdi_next) \
158 && ((__pdi_next = __pdi_next->next), \
159 ((element)->prevp = 0), \
160 1); \
163 /* The total number of threads currently active. */
164 extern unsigned int __pthread_total;
166 /* The total number of thread IDs currently in use, or on the list of
167 available thread IDs. */
168 extern int __pthread_num_threads;
170 /* Concurrency hint. */
171 extern int __pthread_concurrency;
173 /* Array of __pthread structures and its lock. Indexed by the pthread
174 id minus one. (Why not just use the pthread id? Because some
175 brain-dead users of the pthread interface incorrectly assume that 0
176 is an invalid pthread id.) */
177 extern struct __pthread **__pthread_threads;
178 extern int __pthread_max_threads;
179 extern pthread_rwlock_t __pthread_threads_lock;
181 #define __pthread_getid(thread) \
182 ({ struct __pthread *__t = NULL; \
183 __pthread_rwlock_rdlock (&__pthread_threads_lock); \
184 if (thread <= __pthread_max_threads) \
185 __t = __pthread_threads[thread - 1]; \
186 __pthread_rwlock_unlock (&__pthread_threads_lock); \
187 __t; })
189 #define __pthread_setid(thread, pthread) \
190 __pthread_rwlock_wrlock (&__pthread_threads_lock); \
191 __pthread_threads[thread - 1] = pthread; \
192 __pthread_rwlock_unlock (&__pthread_threads_lock);
194 /* Similar to pthread_self, but returns the thread descriptor instead
195 of the thread ID. */
196 #ifndef _pthread_self
197 extern struct __pthread *_pthread_self (void);
198 #endif
200 /* Stores the stack of cleanup handlers for the thread. */
201 extern __thread struct __pthread_cancelation_handler *__pthread_cleanup_stack;
204 /* Initialize the pthreads library. */
205 extern void ___pthread_init (void);
207 /* Internal version of pthread_create. Rather than return the new
208 tid, we return the whole __pthread structure in *PTHREAD. */
209 extern int __pthread_create_internal (struct __pthread **__restrict pthread,
210 const pthread_attr_t *__restrict attr,
211 void *(*start_routine) (void *),
212 void *__restrict arg);
214 /* Allocate a new thread structure and a pthread thread ID (but not a
215 kernel thread or a stack). THREAD has one reference. */
216 extern int __pthread_alloc (struct __pthread **thread);
218 /* Deallocate the thread structure. This is the dual of
219 __pthread_alloc (N.B. it does not call __pthread_stack_dealloc nor
220 __pthread_thread_terminate). THREAD loses one reference and is
221 released if the reference counter drops to 0. */
222 extern void __pthread_dealloc (struct __pthread *thread);
225 /* Allocate a stack of size STACKSIZE. The stack base shall be
226 returned in *STACKADDR. */
227 extern int __pthread_stack_alloc (void **stackaddr, size_t stacksize);
229 /* Deallocate the stack STACKADDR of size STACKSIZE. */
230 extern void __pthread_stack_dealloc (void *stackaddr, size_t stacksize);
233 /* Setup thread THREAD's context. */
234 extern int __pthread_setup (struct __pthread *__restrict thread,
235 void (*entry_point) (struct __pthread *,
236 void *(*)(void *),
237 void *),
238 void *(*start_routine) (void *),
239 void *__restrict arg);
242 /* Allocate a kernel thread (and any miscellaneous system dependent
243 resources) for THREAD; it must not be placed on the run queue. */
244 extern int __pthread_thread_alloc (struct __pthread *thread);
246 /* Start THREAD making it eligible to run. */
247 extern int __pthread_thread_start (struct __pthread *thread);
249 /* Terminate the kernel thread associated with THREAD, and deallocate its
250 stack as well as any other kernel resource associated with it.
251 In addition, THREAD looses one reference.
253 This function can be called by any thread, including the target thread.
254 Since some resources that are destroyed along the kernel thread are
255 stored in thread-local variables, the conditions required for this
256 function to behave correctly are a bit unusual : as long as the target
257 thread hasn't been started, any thread can terminate it, but once it
258 has started, no other thread can terminate it, so that thread-local
259 variables created by that thread are correctly released. */
260 extern void __pthread_thread_terminate (struct __pthread *thread);
263 /* Called by a thread just before it calls the provided start
264 routine. */
265 extern void __pthread_startup (void);
267 /* Block THREAD. */
268 extern void __pthread_block (struct __pthread *thread);
270 /* Block THREAD until *ABSTIME is reached. */
271 extern error_t __pthread_timedblock (struct __pthread *__restrict thread,
272 const struct timespec *__restrict abstime,
273 clockid_t clock_id);
275 /* Block THREAD with interrupts. */
276 extern error_t __pthread_block_intr (struct __pthread *thread);
278 /* Block THREAD until *ABSTIME is reached, with interrupts. */
279 extern error_t __pthread_timedblock_intr (struct __pthread *__restrict thread,
280 const struct timespec *__restrict abstime,
281 clockid_t clock_id);
283 /* Wakeup THREAD. */
284 extern void __pthread_wakeup (struct __pthread *thread);
287 /* Perform a cancelation. The CANCEL_LOCK member of the given thread must
288 be locked before calling this function, which must unlock it. */
289 extern int __pthread_do_cancel (struct __pthread *thread);
292 /* Initialize the thread specific data structures. THREAD must be the
293 calling thread. */
294 extern error_t __pthread_init_specific (struct __pthread *thread);
296 /* Call the destructors on all of the thread specific data in THREAD.
297 THREAD must be the calling thread. */
298 extern void __pthread_destroy_specific (struct __pthread *thread);
301 /* Initialize newly create thread *THREAD's signal state data
302 structures. */
303 extern error_t __pthread_sigstate_init (struct __pthread *thread);
305 /* Destroy the signal state data structures associcated with thread
306 *THREAD. */
307 extern void __pthread_sigstate_destroy (struct __pthread *thread);
309 /* Modify thread *THREAD's signal state. */
310 extern error_t __pthread_sigstate (struct __pthread *__restrict thread, int how,
311 const sigset_t *__restrict set,
312 sigset_t *__restrict oset,
313 int clear_pending);
315 /* If supported, check that MUTEX is locked by the caller. */
316 extern int __pthread_mutex_checklocked (pthread_mutex_t *mtx);
319 /* Default thread attributes. */
320 extern struct __pthread_attr __pthread_default_attr;
322 /* Default barrier attributes. */
323 extern const struct __pthread_barrierattr __pthread_default_barrierattr;
325 /* Default rdlock attributes. */
326 extern const struct __pthread_rwlockattr __pthread_default_rwlockattr;
328 /* Default condition attributes. */
329 extern const struct __pthread_condattr __pthread_default_condattr;
331 #endif /* pt-internal.h */