pthread.h revision 331722
1331722Seadler/*
213547Sjulian * Copyright (c) 1993, 1994 by Chris Provenzano, proven@mit.edu
335025Sjb * Copyright (c) 1995-1998 by John Birrell <jb@cimlogic.com.au>
413547Sjulian * All rights reserved.
513547Sjulian *
613547Sjulian * Redistribution and use in source and binary forms, with or without
713547Sjulian * modification, are permitted provided that the following conditions
813547Sjulian * are met:
913547Sjulian * 1. Redistributions of source code must retain the above copyright
1013547Sjulian *    notice, this list of conditions and the following disclaimer.
1113547Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1213547Sjulian *    notice, this list of conditions and the following disclaimer in the
1313547Sjulian *    documentation and/or other materials provided with the distribution.
1413547Sjulian * 3. All advertising materials mentioning features or use of this software
1513547Sjulian *    must display the following acknowledgement:
1613547Sjulian *  This product includes software developed by Chris Provenzano.
1713547Sjulian * 4. The name of Chris Provenzano may not be used to endorse or promote
1813547Sjulian *	  products derived from this software without specific prior written
1913547Sjulian *	  permission.
2013547Sjulian *
2113547Sjulian * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
2213547Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2313547Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2413547Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY
2513547Sjulian * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2613547Sjulian * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2713547Sjulian * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2813547Sjulian * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2913547Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3013547Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3113547Sjulian * SUCH DAMAGE.
3213547Sjulian *
3350473Speter * $FreeBSD: stable/11/include/pthread.h 331722 2018-03-29 02:50:57Z eadler $
3413547Sjulian */
3513547Sjulian#ifndef _PTHREAD_H_
36280820Spfg#define	_PTHREAD_H_
3713547Sjulian
3813547Sjulian/*
3913547Sjulian * Header files.
4013547Sjulian */
4113547Sjulian#include <sys/cdefs.h>
42146824Srodrigc#include <sys/_pthreadtypes.h>
43149692Sstefanf#include <machine/_limits.h>
44149692Sstefanf#include <machine/_types.h>
45149692Sstefanf#include <sys/_sigset.h>
4644965Sjb#include <sched.h>
47149692Sstefanf#include <time.h>
4813547Sjulian
49315282Spfg__NULLABILITY_PRAGMA_PUSH
50315282Spfg
5113547Sjulian/*
5217706Sjulian * Run-time invariant values:
5313547Sjulian */
54280820Spfg#define	PTHREAD_DESTRUCTOR_ITERATIONS		4
55280820Spfg#define	PTHREAD_KEYS_MAX			256
56280820Spfg#define	PTHREAD_STACK_MIN			__MINSIGSTKSZ
57280820Spfg#define	PTHREAD_THREADS_MAX			__ULONG_MAX
58280820Spfg#define	PTHREAD_BARRIER_SERIAL_THREAD		-1
5913547Sjulian
6013547Sjulian/*
6122315Sjulian * Flags for threads and thread attributes.
6222315Sjulian */
63280820Spfg#define	PTHREAD_DETACHED		0x1
64280820Spfg#define	PTHREAD_SCOPE_SYSTEM		0x2
65280820Spfg#define	PTHREAD_INHERIT_SCHED		0x4
66280820Spfg#define	PTHREAD_NOFLOAT			0x8
6722315Sjulian
68280820Spfg#define	PTHREAD_CREATE_DETACHED		PTHREAD_DETACHED
69280820Spfg#define	PTHREAD_CREATE_JOINABLE		0
70280820Spfg#define	PTHREAD_SCOPE_PROCESS		0
71280820Spfg#define	PTHREAD_EXPLICIT_SCHED		0
7222315Sjulian
7322315Sjulian/*
74296162Skib * Values for process shared/private attributes.
7538919Salex */
76280820Spfg#define	PTHREAD_PROCESS_PRIVATE		0
77280820Spfg#define	PTHREAD_PROCESS_SHARED		1
7838919Salex
7938919Salex/*
8053812Salfred * Flags for cancelling threads
8153812Salfred */
82280820Spfg#define	PTHREAD_CANCEL_ENABLE		0
83280820Spfg#define	PTHREAD_CANCEL_DISABLE		1
84280820Spfg#define	PTHREAD_CANCEL_DEFERRED		0
85280820Spfg#define	PTHREAD_CANCEL_ASYNCHRONOUS	2
86280820Spfg#define	PTHREAD_CANCELED		((void *) 1)
8753812Salfred
8853812Salfred/*
8913547Sjulian * Flags for once initialization.
9013547Sjulian */
91280820Spfg#define	PTHREAD_NEEDS_INIT	0
92280820Spfg#define	PTHREAD_DONE_INIT	1
9313547Sjulian
9413547Sjulian/*
95280820Spfg * Static once initialization values.
9613547Sjulian */
97280820Spfg#define	PTHREAD_ONCE_INIT	{ PTHREAD_NEEDS_INIT, NULL }
9813547Sjulian
9913547Sjulian/*
100280820Spfg * Static initialization values.
10113547Sjulian */
102280820Spfg#define	PTHREAD_MUTEX_INITIALIZER	NULL
103280820Spfg#define	PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP	((pthread_mutex_t)1)
104280820Spfg#define	PTHREAD_COND_INITIALIZER	NULL
105280820Spfg#define	PTHREAD_RWLOCK_INITIALIZER	NULL
10635025Sjb
10735025Sjb/*
10835025Sjb * Default attribute arguments (draft 4, deprecated).
10935025Sjb */
11022315Sjulian#ifndef PTHREAD_KERNEL
111280820Spfg#define	pthread_condattr_default	NULL
112280820Spfg#define	pthread_mutexattr_default	NULL
113280820Spfg#define	pthread_attr_default		NULL
11413547Sjulian#endif
11513547Sjulian
116280820Spfg#define	PTHREAD_PRIO_NONE	0
117280820Spfg#define	PTHREAD_PRIO_INHERIT	1
118280820Spfg#define	PTHREAD_PRIO_PROTECT	2
11944965Sjb
12044965Sjb/*
12144965Sjb * Mutex types (Single UNIX Specification, Version 2, 1997).
12244965Sjb *
12344965Sjb * Note that a mutex attribute with one of the following types:
12444965Sjb *
12544965Sjb *	PTHREAD_MUTEX_NORMAL
12644965Sjb *	PTHREAD_MUTEX_RECURSIVE
12744965Sjb *
12844965Sjb * will deviate from POSIX specified semantics.
12944965Sjb */
13019637Shsuenum pthread_mutextype {
13144965Sjb	PTHREAD_MUTEX_ERRORCHECK	= 1,	/* Default POSIX mutex */
13244965Sjb	PTHREAD_MUTEX_RECURSIVE		= 2,	/* Recursive mutex */
13344965Sjb	PTHREAD_MUTEX_NORMAL		= 3,	/* No error checking */
134173154Skris	PTHREAD_MUTEX_ADAPTIVE_NP	= 4,	/* Adaptive mutex, spins briefly before blocking on lock */
135149298Sstefanf	PTHREAD_MUTEX_TYPE_MAX
13619637Shsu};
13719637Shsu
138280820Spfg#define	PTHREAD_MUTEX_DEFAULT		PTHREAD_MUTEX_ERRORCHECK
13941390Seivind
140300043Skib#define	PTHREAD_MUTEX_STALLED		0
141300043Skib#define	PTHREAD_MUTEX_ROBUST		1
142300043Skib
143179662Sdavidxustruct _pthread_cleanup_info {
144179662Sdavidxu	__uintptr_t	pthread_cleanup_pad[8];
145179662Sdavidxu};
146179662Sdavidxu
14713547Sjulian/*
14813547Sjulian * Thread function prototype definitions:
14913547Sjulian */
15013547Sjulian__BEGIN_DECLS
151149273Sstefanfint		pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
152315282Spfgint		pthread_attr_destroy(pthread_attr_t * _Nonnull);
153315282Spfgint		pthread_attr_getstack(
154315282Spfg		    const pthread_attr_t * _Nonnull __restrict,
155315282Spfg		    void ** _Nonnull __restrict,
156315282Spfg		    size_t * _Nonnull __restrict);
157315282Spfgint		pthread_attr_getstacksize(const pthread_attr_t * _Nonnull,
158315282Spfg		    size_t * _Nonnull);
159315282Spfgint		pthread_attr_getguardsize(const pthread_attr_t * _Nonnull,
160315282Spfg		    size_t * _Nonnull);
16193032Simpint		pthread_attr_getstackaddr(const pthread_attr_t *, void **);
162315282Spfgint		pthread_attr_getdetachstate(const pthread_attr_t * _Nonnull,
163315282Spfg		    int * _Nonnull);
164315282Spfgint		pthread_attr_init(pthread_attr_t * _Nonnull);
165315282Spfgint		pthread_attr_setstacksize(pthread_attr_t * _Nonnull, size_t);
166315282Spfgint		pthread_attr_setguardsize(pthread_attr_t * _Nonnull, size_t);
167315282Spfgint		pthread_attr_setstack(pthread_attr_t * _Nonnull, void *,
168315282Spfg		    size_t);
16993032Simpint		pthread_attr_setstackaddr(pthread_attr_t *, void *);
170315282Spfgint		pthread_attr_setdetachstate(pthread_attr_t * _Nonnull, int);
171315282Spfgint		pthread_barrier_destroy(pthread_barrier_t * _Nonnull);
172315282Spfgint		pthread_barrier_init(pthread_barrier_t * _Nonnull,
173119736Sdavidxu			const pthread_barrierattr_t *, unsigned);
174315282Spfgint		pthread_barrier_wait(pthread_barrier_t * _Nonnull);
175315282Spfgint		pthread_barrierattr_destroy(pthread_barrierattr_t * _Nonnull);
176315282Spfgint		pthread_barrierattr_getpshared(
177315282Spfg		    const pthread_barrierattr_t * _Nonnull, int * _Nonnull);
178315282Spfgint		pthread_barrierattr_init(pthread_barrierattr_t * _Nonnull);
179315282Spfgint		pthread_barrierattr_setpshared(pthread_barrierattr_t * _Nonnull,
180315282Spfg		    int);
181179662Sdavidxu
182179662Sdavidxu#define		pthread_cleanup_push(cleanup_routine, cleanup_arg)		\
183179662Sdavidxu		{								\
184179662Sdavidxu			struct _pthread_cleanup_info __cleanup_info__;		\
185179662Sdavidxu			__pthread_cleanup_push_imp(cleanup_routine, cleanup_arg,\
186179662Sdavidxu				&__cleanup_info__);				\
187179662Sdavidxu			{
188179662Sdavidxu
189179662Sdavidxu#define		pthread_cleanup_pop(execute)					\
190256925Stijl				(void)0;					\
191179662Sdavidxu			}							\
192179662Sdavidxu			__pthread_cleanup_pop_imp(execute);			\
193179662Sdavidxu		}
194179662Sdavidxu
195315282Spfgint		pthread_condattr_destroy(pthread_condattr_t * _Nonnull);
196315282Spfgint		pthread_condattr_getclock(const pthread_condattr_t * _Nonnull,
197315282Spfg		    clockid_t * _Nonnull);
198315282Spfgint		pthread_condattr_getpshared(const pthread_condattr_t * _Nonnull,
199315282Spfg		    int * _Nonnull);
200315282Spfgint		pthread_condattr_init(pthread_condattr_t * _Nonnull);
201315282Spfgint		pthread_condattr_setclock(pthread_condattr_t * _Nonnull,
202315282Spfg		    clockid_t);
203315282Spfgint		pthread_condattr_setpshared(pthread_condattr_t * _Nonnull, int);
204315282Spfgint		pthread_cond_broadcast(pthread_cond_t * _Nonnull);
205315282Spfgint		pthread_cond_destroy(pthread_cond_t * _Nonnull);
206315282Spfgint		pthread_cond_init(pthread_cond_t * _Nonnull,
207311234Spfg			const pthread_condattr_t *);
208315282Spfgint		pthread_cond_signal(pthread_cond_t * _Nonnull);
209315282Spfgint		pthread_cond_timedwait(pthread_cond_t * _Nonnull,
210315282Spfg		    pthread_mutex_t * _Nonnull __mutex,
211315282Spfg		    const struct timespec * _Nonnull)
212311234Spfg		    __requires_exclusive(*__mutex);
213315282Spfgint		pthread_cond_wait(pthread_cond_t * _Nonnull,
214315282Spfg		    pthread_mutex_t * _Nonnull __mutex)
215311234Spfg		    __requires_exclusive(*__mutex);
216315282Spfgint		pthread_create(pthread_t * _Nonnull, const pthread_attr_t *,
217315282Spfg		    void *(* _Nonnull) (void *), void *);
21893032Simpint		pthread_detach(pthread_t);
21993032Simpint		pthread_equal(pthread_t, pthread_t);
22093032Simpvoid		pthread_exit(void *) __dead2;
22193032Simpvoid		*pthread_getspecific(pthread_key_t);
222315282Spfgint		pthread_getcpuclockid(pthread_t, clockid_t * _Nonnull);
22393032Simpint		pthread_join(pthread_t, void **);
224315282Spfgint		pthread_key_create(pthread_key_t * _Nonnull,
225315282Spfg		    void (*) (void *));
22693032Simpint		pthread_key_delete(pthread_key_t);
227315282Spfgint		pthread_mutexattr_init(pthread_mutexattr_t * _Nonnull);
228315282Spfgint		pthread_mutexattr_destroy(pthread_mutexattr_t * _Nonnull);
229315282Spfgint		pthread_mutexattr_getpshared(
230315282Spfg		    const pthread_mutexattr_t * _Nonnull, int * _Nonnull);
231315282Spfgint		pthread_mutexattr_gettype(pthread_mutexattr_t * _Nonnull,
232315282Spfg		    int * _Nonnull);
233315282Spfgint		pthread_mutexattr_settype(pthread_mutexattr_t * _Nonnull, int);
234315282Spfgint		pthread_mutexattr_setpshared(pthread_mutexattr_t * _Nonnull,
235315282Spfg		    int);
236315282Spfgint		pthread_mutex_consistent(pthread_mutex_t * _Nonnull __mutex)
237315282Spfg		    __requires_exclusive(*__mutex);
238315282Spfgint		pthread_mutex_destroy(pthread_mutex_t * _Nonnull __mutex)
239311234Spfg		    __requires_unlocked(*__mutex);
240315282Spfgint		pthread_mutex_init(pthread_mutex_t * _Nonnull __mutex,
241315282Spfg		    const pthread_mutexattr_t *)
242311234Spfg		    __requires_unlocked(*__mutex);
243315282Spfgint		pthread_mutex_lock(pthread_mutex_t * _Nonnull __mutex)
244315282Spfg		    __locks_exclusive(*__mutex);
245315282Spfgint		pthread_mutex_trylock(pthread_mutex_t * _Nonnull __mutex)
246315282Spfg		    __trylocks_exclusive(0, *__mutex);
247315282Spfgint		pthread_mutex_timedlock(pthread_mutex_t * _Nonnull __mutex,
248315282Spfg		    const struct timespec * _Nonnull)
249315282Spfg		    __trylocks_exclusive(0, *__mutex);
250315282Spfgint		pthread_mutex_unlock(pthread_mutex_t * _Nonnull __mutex)
251311234Spfg		    __unlocks(*__mutex);
252315282Spfgint		pthread_once(pthread_once_t * _Nonnull,
253315282Spfg		    void (* _Nonnull) (void));
254315282Spfgint		pthread_rwlock_destroy(pthread_rwlock_t * _Nonnull __rwlock)
255311234Spfg		    __requires_unlocked(*__rwlock);
256315282Spfgint		pthread_rwlock_init(pthread_rwlock_t * _Nonnull __rwlock,
257315282Spfg		    const pthread_rwlockattr_t *)
258311234Spfg		    __requires_unlocked(*__rwlock);
259315282Spfgint		pthread_rwlock_rdlock(pthread_rwlock_t * _Nonnull __rwlock)
260315282Spfg		    __locks_shared(*__rwlock);
261315282Spfgint		pthread_rwlock_timedrdlock(pthread_rwlock_t * _Nonnull __rwlock,
262315282Spfg		    const struct timespec * _Nonnull)
263315282Spfg		    __trylocks_shared(0, *__rwlock);
264315282Spfgint		pthread_rwlock_timedwrlock(pthread_rwlock_t * _Nonnull __rwlock,
265315282Spfg		    const struct timespec * _Nonnull)
266315282Spfg		    __trylocks_exclusive(0, *__rwlock);
267315282Spfgint		pthread_rwlock_tryrdlock(pthread_rwlock_t * _Nonnull __rwlock)
268315282Spfg		    __trylocks_shared(0, *__rwlock);
269315282Spfgint		pthread_rwlock_trywrlock(pthread_rwlock_t * _Nonnull __rwlock)
270315282Spfg		    __trylocks_exclusive(0, *__rwlock);
271315282Spfgint		pthread_rwlock_unlock(pthread_rwlock_t * _Nonnull __rwlock)
272311234Spfg		    __unlocks(*__rwlock);
273315282Spfgint		pthread_rwlock_wrlock(pthread_rwlock_t * _Nonnull __rwlock)
274315282Spfg		    __locks_exclusive(*__rwlock);
275315282Spfgint		pthread_rwlockattr_destroy(pthread_rwlockattr_t * _Nonnull);
276315282Spfgint		pthread_rwlockattr_getkind_np(
277315282Spfg		    const pthread_rwlockattr_t * _Nonnull, int *);
278315282Spfgint		pthread_rwlockattr_getpshared(
279315282Spfg		    const pthread_rwlockattr_t * _Nonnull, int * _Nonnull);
280315282Spfgint		pthread_rwlockattr_init(pthread_rwlockattr_t * _Nonnull);
281315282Spfgint		pthread_rwlockattr_setkind_np(pthread_rwlockattr_t * _Nonnull,
282315282Spfg		    int);
283315282Spfgint		pthread_rwlockattr_setpshared(pthread_rwlockattr_t * _Nonnull,
284315282Spfg		    int);
28593032Simppthread_t	pthread_self(void);
28693032Simpint		pthread_setspecific(pthread_key_t, const void *);
28717706Sjulian
288315282Spfgint		pthread_spin_init(pthread_spinlock_t * _Nonnull __spin, int)
289311234Spfg		    __requires_unlocked(*__spin);
290315282Spfgint		pthread_spin_destroy(pthread_spinlock_t * _Nonnull __spin)
291311234Spfg		    __requires_unlocked(*__spin);
292315282Spfgint		pthread_spin_lock(pthread_spinlock_t * _Nonnull __spin)
293315282Spfg			__locks_exclusive(*__spin);
294315282Spfgint		pthread_spin_trylock(pthread_spinlock_t * _Nonnull __spin)
295315282Spfg			__trylocks_exclusive(0, *__spin);
296315282Spfgint		pthread_spin_unlock(pthread_spinlock_t * _Nonnull __spin)
297315282Spfg			__unlocks(*__spin);
29893032Simpint		pthread_cancel(pthread_t);
29993032Simpint		pthread_setcancelstate(int, int *);
30093032Simpint		pthread_setcanceltype(int, int *);
30193032Simpvoid		pthread_testcancel(void);
30217706Sjulian
303189828Sdas#if __BSD_VISIBLE
30493032Simpint		pthread_getprio(pthread_t);
30593032Simpint		pthread_setprio(pthread_t, int);
30693032Simpvoid		pthread_yield(void);
307189828Sdas#endif
30844965Sjb
309315282Spfgint		pthread_mutexattr_getprioceiling(pthread_mutexattr_t *, int *);
310315282Spfgint		pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
31193032Simpint		pthread_mutex_getprioceiling(pthread_mutex_t *, int *);
31293032Simpint		pthread_mutex_setprioceiling(pthread_mutex_t *, int, int *);
31344965Sjb
31493032Simpint		pthread_mutexattr_getprotocol(pthread_mutexattr_t *, int *);
31593032Simpint		pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
31644965Sjb
317315282Spfgint		pthread_mutexattr_getrobust(
318315282Spfg		    pthread_mutexattr_t * _Nonnull __restrict,
319315282Spfg		    int * _Nonnull __restrict);
320315282Spfgint		pthread_mutexattr_setrobust(pthread_mutexattr_t * _Nonnull,
321315282Spfg		    int);
322300043Skib
32393032Simpint		pthread_attr_getinheritsched(const pthread_attr_t *, int *);
324315282Spfgint		pthread_attr_getschedparam(const pthread_attr_t * _Nonnull,
325315282Spfg		    struct sched_param * _Nonnull);
326315282Spfgint		pthread_attr_getschedpolicy(const pthread_attr_t * _Nonnull,
327315282Spfg		    int * _Nonnull);
328315282Spfgint		pthread_attr_getscope(const pthread_attr_t * _Nonnull,
329315282Spfg		    int * _Nonnull);
33093032Simpint		pthread_attr_setinheritsched(pthread_attr_t *, int);
331315282Spfgint		pthread_attr_setschedparam(pthread_attr_t * _Nonnull,
332315282Spfg		    const struct sched_param * _Nonnull);
333315282Spfgint		pthread_attr_setschedpolicy(pthread_attr_t * _Nonnull, int);
334315282Spfgint		pthread_attr_setscope(pthread_attr_t * _Nonnull, int);
335315282Spfgint		pthread_getschedparam(pthread_t pthread, int * _Nonnull,
336315282Spfg		    struct sched_param * _Nonnull);
33793032Simpint		pthread_setschedparam(pthread_t, int,
338315282Spfg		    const struct sched_param * _Nonnull);
339189828Sdas#if __XSI_VISIBLE
340113729Sjdpint		pthread_getconcurrency(void);
341113729Sjdpint		pthread_setconcurrency(int);
342189828Sdas#endif
343179662Sdavidxu
344179662Sdavidxuvoid		__pthread_cleanup_push_imp(void (*)(void *), void *,
345179662Sdavidxu			struct _pthread_cleanup_info *);
346179662Sdavidxuvoid		__pthread_cleanup_pop_imp(int);
34713547Sjulian__END_DECLS
348315282Spfg__NULLABILITY_PRAGMA_POP
34913547Sjulian
350331190Seadler#endif	/* !_PTHREAD_H_ */
351