1/* $NetBSD: pthread.h,v 1.41.18.1 2025/11/20 18:46:42 martin Exp $ */
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _LIB_PTHREAD_H
33#define _LIB_PTHREAD_H
34
35#include <sys/cdefs.h>
36
37#include <time.h> /* For timespec */
38#include <sched.h>
39#include <sys/featuretest.h>
40
41#include <pthread_types.h>
42
43__BEGIN_DECLS
44#ifndef __PTHREAD_ATFORK_DECLARED
45#define __PTHREAD_ATFORK_DECLARED
46int pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
47#endif
48int pthread_create(pthread_t * __restrict,
49 const pthread_attr_t * __restrict, void *(*)(void *),
50 void * __restrict);
51void pthread_exit(void *) __attribute__((__noreturn__));
52int pthread_join(pthread_t, void **);
53int pthread_equal(pthread_t, pthread_t);
54pthread_t pthread_self(void);
55int pthread_detach(pthread_t);
56
57int pthread_getrrtimer_np(void);
58int pthread_setrrtimer_np(int);
59
60int pthread_attr_init(pthread_attr_t *);
61int pthread_attr_destroy(pthread_attr_t *);
62int pthread_attr_get_np(pthread_t, pthread_attr_t *);
63int pthread_attr_getguardsize(const pthread_attr_t * __restrict,
64 size_t * __restrict);
65int pthread_attr_setguardsize(pthread_attr_t *, size_t);
66int pthread_attr_getinheritsched(const pthread_attr_t * __restrict,
67 int * __restrict);
68int pthread_attr_setinheritsched(pthread_attr_t *, int);
69int pthread_attr_getschedparam(const pthread_attr_t * __restrict,
70 struct sched_param * __restrict);
71int pthread_attr_setschedparam(pthread_attr_t * __restrict,
72 const struct sched_param * __restrict);
73int pthread_attr_getschedpolicy(const pthread_attr_t * __restrict,
74 int * __restrict);
75int pthread_attr_setschedpolicy(pthread_attr_t *, int);
76int pthread_attr_getscope(const pthread_attr_t * __restrict,
77 int * __restrict);
78int pthread_attr_setscope(pthread_attr_t *, int);
79int pthread_attr_getstack(const pthread_attr_t * __restrict,
80 void ** __restrict, size_t * __restrict);
81int pthread_attr_setstack(pthread_attr_t *, void *, size_t);
82int pthread_attr_getstacksize(const pthread_attr_t * __restrict,
83 size_t * __restrict);
84int pthread_attr_setstacksize(pthread_attr_t *, size_t);
85int pthread_attr_getstackaddr(const pthread_attr_t * __restrict,
86 void ** __restrict);
87int pthread_attr_setstackaddr(pthread_attr_t *, void *);
88int pthread_attr_getdetachstate(const pthread_attr_t *, int *);
89int pthread_attr_setdetachstate(pthread_attr_t *, int);
90int pthread_attr_getname_np(const pthread_attr_t *, char *,
91 size_t, void **);
92int pthread_attr_setname_np(pthread_attr_t *, const char *, void *);
93
94int pthread_mutex_init(pthread_mutex_t * __restrict,
95 const pthread_mutexattr_t * __restrict);
96int pthread_mutex_destroy(pthread_mutex_t *);
97int pthread_mutex_lock(pthread_mutex_t *);
98int pthread_mutex_trylock(pthread_mutex_t *);
99int pthread_mutex_unlock(pthread_mutex_t *);
100#ifndef __LIBC12_SOURCE__
101int pthread_mutex_timedlock(pthread_mutex_t * __restrict,
102 const struct timespec * __restrict);
103#endif
104int pthread_mutex_getprioceiling(const pthread_mutex_t * __restrict,
105 int * __restrict);
106int pthread_mutex_setprioceiling(pthread_mutex_t * __restrict, int,
107 int * __restrict);
108int pthread_mutexattr_init(pthread_mutexattr_t *);
109int pthread_mutexattr_destroy(pthread_mutexattr_t *);
110#ifdef _PTHREAD_PSHARED
111int pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict,
112 int * __restrict);
113int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
114#endif
115int pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict,
116 int * __restrict);
117int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int);
118int pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict,
119 int * __restrict);
120int pthread_mutexattr_setprotocol(pthread_mutexattr_t*,
121 int);
122int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict,
123 int * __restrict);
124int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *,
125 int);
126int pthread_cond_init(pthread_cond_t * __restrict,
127 const pthread_condattr_t * __restrict);
128int pthread_cond_destroy(pthread_cond_t *);
129int pthread_cond_wait(pthread_cond_t * __restrict,
130 pthread_mutex_t * __restrict);
131#ifndef __LIBC12_SOURCE__
132int pthread_cond_timedwait(pthread_cond_t * __restrict,
133 pthread_mutex_t * __restrict, const struct timespec * __restrict);
134#endif
135int pthread_cond_signal(pthread_cond_t *);
136int pthread_cond_broadcast(pthread_cond_t *);
137int pthread_condattr_init(pthread_condattr_t *);
138int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
139int pthread_condattr_getclock(const pthread_condattr_t * __restrict,
140 clockid_t * __restrict);
141int pthread_condattr_destroy(pthread_condattr_t *);
142#ifdef _PTHREAD_PSHARED
143int pthread_condattr_getpshared(const pthread_condattr_t * __restrict,
144 int * __restrict);
145int pthread_condattr_setpshared(pthread_condattr_t *, int);
146#endif
147int pthread_once(pthread_once_t *, void (*)(void));
148
149int pthread_key_create(pthread_key_t *, void (*)(void *));
150int pthread_key_delete(pthread_key_t);
151int pthread_setspecific(pthread_key_t, const void *);
152void* pthread_getspecific(pthread_key_t);
153
154int pthread_cancel(pthread_t);
155int pthread_setcancelstate(int, int *);
156int pthread_setcanceltype(int, int *);
157void pthread_testcancel(void);
158
159int pthread_getname_np(pthread_t, char *, size_t);
160int pthread_setname_np(pthread_t, const char *, void *);
161
162int pthread_attr_setcreatesuspend_np(pthread_attr_t *);
163int pthread_suspend_np(pthread_t);
164int pthread_resume_np(pthread_t);
165
166unsigned int pthread_curcpu_np(void);
167
168int pthread_getcpuclockid(pthread_t, clockid_t *);
169
170struct pthread_cleanup_store {
171 void *pad[4];
172};
173
174#define pthread_cleanup_push(routine, arg) \
175 { \
176 struct pthread_cleanup_store __store; \
177 pthread__cleanup_push((routine),(arg), &__store);
178
179#define pthread_cleanup_pop(execute) \
180 pthread__cleanup_pop((execute), &__store); \
181 }
182
183void pthread__cleanup_push(void (*)(void *), void *, void *);
184void pthread__cleanup_pop(int, void *);
185
186int pthread_spin_init(pthread_spinlock_t *, int);
187int pthread_spin_destroy(pthread_spinlock_t *);
188int pthread_spin_lock(pthread_spinlock_t *);
189int pthread_spin_trylock(pthread_spinlock_t *);
190int pthread_spin_unlock(pthread_spinlock_t *);
191
192int pthread_rwlock_init(pthread_rwlock_t * __restrict,
193 const pthread_rwlockattr_t * __restrict);
194int pthread_rwlock_destroy(pthread_rwlock_t *);
195int pthread_rwlock_rdlock(pthread_rwlock_t *);
196int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
197int pthread_rwlock_wrlock(pthread_rwlock_t *);
198int pthread_rwlock_trywrlock(pthread_rwlock_t *);
199#ifndef __LIBC12_SOURCE__
200int pthread_rwlock_timedrdlock(pthread_rwlock_t * __restrict,
201 const struct timespec * __restrict);
202int pthread_rwlock_timedwrlock(pthread_rwlock_t * __restrict,
203 const struct timespec * __restrict);
204#endif
205int pthread_rwlock_unlock(pthread_rwlock_t *);
206int pthread_rwlockattr_init(pthread_rwlockattr_t *);
207int pthread_rwlockattr_destroy(pthread_rwlockattr_t *);
208#ifdef _PTHREAD_PSHARED
209int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * __restrict,
210 int * __restrict);
211int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int);
212#endif
213int pthread_barrier_init(pthread_barrier_t * __restrict,
214 const pthread_barrierattr_t * __restrict, unsigned int);
215int pthread_barrier_wait(pthread_barrier_t *);
216int pthread_barrier_destroy(pthread_barrier_t *);
217int pthread_barrierattr_init(pthread_barrierattr_t *);
218int pthread_barrierattr_destroy(pthread_barrierattr_t *);
219#ifdef _PTHREAD_PSHARED
220int pthread_barrierattr_getpshared(const pthread_barrierattr_t * __restrict,
221 int * __restrict);
222int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
223#endif
224int pthread_getschedparam(pthread_t, int * __restrict,
225 struct sched_param * __restrict);
226int pthread_setschedparam(pthread_t, int, const struct sched_param *);
227int pthread_setschedprio(pthread_t, int);
228
229int *pthread__errno(void);
230
231#if defined(_NETBSD_SOURCE)
232int pthread_getaffinity_np(pthread_t, size_t, cpuset_t *);
233int pthread_setaffinity_np(pthread_t, size_t, cpuset_t *);
234int pthread_getattr_np(pthread_t, pthread_attr_t *);
235
236int pthread_mutex_held_np(pthread_mutex_t *);
237pthread_t pthread_mutex_owner_np(pthread_mutex_t *);
238
239int pthread_rwlock_held_np(pthread_rwlock_t *);
240int pthread_rwlock_wrheld_np(pthread_rwlock_t *);
241int pthread_rwlock_rdheld_np(pthread_rwlock_t *);
242
243int pthread_cond_has_waiters_np(pthread_cond_t *);
244#endif /* _NETBSD_SOURCE */
245
246__END_DECLS
247
248#define PTHREAD_CREATE_JOINABLE 0
249#define PTHREAD_CREATE_DETACHED 1
250
251#define PTHREAD_INHERIT_SCHED 0
252#define PTHREAD_EXPLICIT_SCHED 1
253
254#define PTHREAD_SCOPE_PROCESS 0
255#define PTHREAD_SCOPE_SYSTEM 1
256
257#define PTHREAD_PROCESS_PRIVATE 0
258#define PTHREAD_PROCESS_SHARED 1
259
260#define PTHREAD_CANCEL_DEFERRED 0
261#define PTHREAD_CANCEL_ASYNCHRONOUS 1
262
263#define PTHREAD_CANCEL_ENABLE 0
264#define PTHREAD_CANCEL_DISABLE 1
265
266#define PTHREAD_BARRIER_SERIAL_THREAD 1234567
267
268/*
269 * POSIX 1003.1-2001, section 2.5.9.3: "The symbolic constant
270 * PTHREAD_CANCELED expands to a constant expression of type (void *)
271 * whose value matches no pointer to an object in memory nor the value
272 * NULL."
273 */
274#define PTHREAD_CANCELED ((void *) 1)
275
276/*
277 * Maximum length of a thread's name, including the terminating NUL.
278 */
279#define PTHREAD_MAX_NAMELEN_NP 32
280
281/*
282 * Mutex attributes.
283 */
284#define PTHREAD_MUTEX_NORMAL 0
285#define PTHREAD_MUTEX_ERRORCHECK 1
286#define PTHREAD_MUTEX_RECURSIVE 2
287#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
288
289#define PTHREAD_PRIO_NONE 0
290#define PTHREAD_PRIO_INHERIT 1
291#define PTHREAD_PRIO_PROTECT 2
292
293#define PTHREAD_COND_INITIALIZER _PTHREAD_COND_INITIALIZER
294#define PTHREAD_MUTEX_INITIALIZER _PTHREAD_MUTEX_INITIALIZER
295#define PTHREAD_ONCE_INIT _PTHREAD_ONCE_INIT
296#define PTHREAD_RWLOCK_INITIALIZER _PTHREAD_RWLOCK_INITIALIZER
297#define PTHREAD_SPINLOCK_INITIALIZER _PTHREAD_SPINLOCK_INITIALIZER
298
299/*
300 * Use macros to rename many pthread functions to the corresponding
301 * libc symbols which are either trivial/no-op stubs or the real
302 * thing, depending on whether libpthread is linked in to the
303 * program. This permits code, particularly libraries that do not
304 * directly use threads but want to be thread-safe in the presence of
305 * threaded callers, to use pthread mutexes and the like without
306 * unnecessarily including libpthread in their linkage.
307 *
308 * A common mistake is to include pthread.h but not link against
309 * libpthread in applications that create threads. Since threading
310 * adds substantial overhead to basic libc functionality like stdio, we
311 * don't want to make libpthread default, but we do want to catch this
312 * mistake. We catch it by not defining pthread_create in libc or
313 * renaming it to a stub that is defined in libc by default.
314 *
315 * However, some libraries (like openssl libcrypto) will _optionally_
316 * create threads in threaded applications. These libraries can
317 * request the stub by defining _PTHREAD_CREATE_WEAK, so they can be
318 * used by threaded applications -- which need to link against
319 * libpthread themselves to avoid runtime errors -- and non-threaded
320 * applications which don't link against libpthread at all.
321 *
322 * The rename is done as:
323 * #define pthread_foo __libc_foo
324 * instead of
325 * #define pthread_foo(x) __libc_foo((x))
326 * in order that taking the address of the function ("func =
327 * &pthread_foo;") continue to work.
328 *
329 * POSIX/SUSv3 requires that its functions exist as functions (even if
330 * macro versions exist) and specifically that "#undef pthread_foo" is
331 * legal and should not break anything. Code that does such will not
332 * successfully get the stub behavior implemented here and will
333 * require libpthread to be linked in.
334 */
335
336#ifndef __LIBPTHREAD_SOURCE__
337__BEGIN_DECLS
338int __libc_thr_create(pthread_t * __restrict,
339 const pthread_attr_t * __restrict, void *(*)(void *),
340 void * __restrict);
341int __libc_thr_detach(pthread_t);
342int __libc_thr_join(pthread_t, void **);
343
344int __libc_thr_attr_init(pthread_attr_t *);
345int __libc_thr_attr_setdetachstate(pthread_attr_t *, int);
346int __libc_thr_attr_destroy(pthread_attr_t *);
347__END_DECLS
348
349// zig patch: these aliases were added in NetBSD 11.0
350#if __NetBSD_Version__ >= 1100000000
351/*
352 * If _NETBSD_PTHREAD_CREATE_WEAK is defined, make pthread_create
353 * expand to a symbol which is defined as a weak alias by libc, so
354 * libraries can opt into using it for threaded applications without
355 * requiring non-threaded applications to be linked against libpthread.
356 * Otherwise, if you include pthread.h _without_ defining
357 * _NETBSD_PTHREAD_CREATE_WEAK and try to call pthread_create without
358 * linking against libpthread, the linker will detect this as an error.
359 */
360#ifdef _NETBSD_PTHREAD_CREATE_WEAK
361#define pthread_create __libc_thr_create
362#endif
363
364#define pthread_detach __libc_thr_detach
365#define pthread_join __libc_thr_join
366
367#define pthread_attr_init __libc_thr_attr_init
368#define pthread_attr_setdetachstate __libc_thr_attr_setdetachstate
369#define pthread_attr_destroy __libc_thr_attr_destroy
370#endif
371
372__BEGIN_DECLS
373int __libc_mutex_init(pthread_mutex_t * __restrict, const pthread_mutexattr_t * __restrict);
374int __libc_mutex_lock(pthread_mutex_t *);
375int __libc_mutex_trylock(pthread_mutex_t *);
376int __libc_mutex_unlock(pthread_mutex_t *);
377int __libc_mutex_destroy(pthread_mutex_t *);
378
379int __libc_mutexattr_init(pthread_mutexattr_t *);
380int __libc_mutexattr_settype(pthread_mutexattr_t *, int);
381int __libc_mutexattr_destroy(pthread_mutexattr_t *);
382__END_DECLS
383
384#define pthread_mutex_init __libc_mutex_init
385#define pthread_mutex_lock __libc_mutex_lock
386#define pthread_mutex_trylock __libc_mutex_trylock
387#define pthread_mutex_unlock __libc_mutex_unlock
388#define pthread_mutex_destroy __libc_mutex_destroy
389
390#define pthread_mutexattr_init __libc_mutexattr_init
391#define pthread_mutexattr_settype __libc_mutexattr_settype
392#define pthread_mutexattr_destroy __libc_mutexattr_destroy
393
394__BEGIN_DECLS
395int __libc_cond_init(pthread_cond_t * __restrict,
396 const pthread_condattr_t * __restrict);
397int __libc_cond_signal(pthread_cond_t *);
398int __libc_cond_broadcast(pthread_cond_t *);
399int __libc_cond_wait(pthread_cond_t * __restrict,
400 pthread_mutex_t * __restrict);
401#ifndef __LIBC12_SOURCE__
402int __libc_cond_timedwait(pthread_cond_t * __restrict,
403 pthread_mutex_t * __restrict, const struct timespec * __restrict);
404#endif
405int __libc_cond_destroy(pthread_cond_t *);
406__END_DECLS
407
408#define pthread_cond_init __libc_cond_init
409#define pthread_cond_signal __libc_cond_signal
410#define pthread_cond_broadcast __libc_cond_broadcast
411#define pthread_cond_wait __libc_cond_wait
412#define pthread_cond_timedwait __libc_cond_timedwait
413#define pthread_cond_destroy __libc_cond_destroy
414
415__BEGIN_DECLS
416int __libc_rwlock_init(pthread_rwlock_t * __restrict,
417 const pthread_rwlockattr_t * __restrict);
418int __libc_rwlock_rdlock(pthread_rwlock_t *);
419int __libc_rwlock_wrlock(pthread_rwlock_t *);
420int __libc_rwlock_tryrdlock(pthread_rwlock_t *);
421int __libc_rwlock_trywrlock(pthread_rwlock_t *);
422int __libc_rwlock_unlock(pthread_rwlock_t *);
423int __libc_rwlock_destroy(pthread_rwlock_t *);
424__END_DECLS
425
426#define pthread_rwlock_init __libc_rwlock_init
427#define pthread_rwlock_rdlock __libc_rwlock_rdlock
428#define pthread_rwlock_wrlock __libc_rwlock_wrlock
429#define pthread_rwlock_tryrdlock __libc_rwlock_tryrdlock
430#define pthread_rwlock_trywrlock __libc_rwlock_trywrlock
431#define pthread_rwlock_unlock __libc_rwlock_unlock
432#define pthread_rwlock_destroy __libc_rwlock_destroy
433
434__BEGIN_DECLS
435int __libc_thr_keycreate(pthread_key_t *, void (*)(void *));
436int __libc_thr_setspecific(pthread_key_t, const void *);
437void *__libc_thr_getspecific(pthread_key_t);
438int __libc_thr_keydelete(pthread_key_t);
439__END_DECLS
440
441#define pthread_key_create __libc_thr_keycreate
442#define pthread_setspecific __libc_thr_setspecific
443#define pthread_getspecific __libc_thr_getspecific
444#define pthread_key_delete __libc_thr_keydelete
445
446__BEGIN_DECLS
447int __libc_thr_once(pthread_once_t *, void (*)(void));
448pthread_t __libc_thr_self(void);
449void __libc_thr_exit(void *) __attribute__((__noreturn__));
450int __libc_thr_setcancelstate(int, int *);
451int __libc_thr_equal(pthread_t, pthread_t);
452unsigned int __libc_thr_curcpu(void);
453__END_DECLS
454
455#define pthread_once __libc_thr_once
456#define pthread_self __libc_thr_self
457#define pthread_exit __libc_thr_exit
458#define pthread_setcancelstate __libc_thr_setcancelstate
459#define pthread_equal __libc_thr_equal
460#define pthread_curcpu_np __libc_thr_curcpu
461
462#endif /* __LIBPTHREAD_SOURCE__ */
463
464#endif /* _LIB_PTHREAD_H */