pthread.h revision 139902
113547Sjulian/*
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: head/include/pthread.h 139902 2005-01-08 11:07:13Z davidxu $
3413547Sjulian */
3513547Sjulian#ifndef _PTHREAD_H_
3613547Sjulian#define _PTHREAD_H_
3713547Sjulian
3813547Sjulian/*
3913547Sjulian * Header files.
4013547Sjulian */
4113547Sjulian#include <sys/cdefs.h>
4213547Sjulian#include <sys/types.h>
4313547Sjulian#include <sys/time.h>
4417706Sjulian#include <sys/signal.h>
4517706Sjulian#include <limits.h>
4644965Sjb#include <sched.h>
4713547Sjulian
4813547Sjulian/*
4917706Sjulian * Run-time invariant values:
5013547Sjulian */
5117706Sjulian#define PTHREAD_DESTRUCTOR_ITERATIONS		4
5217706Sjulian#define PTHREAD_KEYS_MAX			256
5317706Sjulian#define PTHREAD_STACK_MIN			1024
5417706Sjulian#define PTHREAD_THREADS_MAX			ULONG_MAX
55119736Sdavidxu#define PTHREAD_BARRIER_SERIAL_THREAD		-1
5613547Sjulian
5713547Sjulian/*
5822315Sjulian * Flags for threads and thread attributes.
5922315Sjulian */
6022315Sjulian#define PTHREAD_DETACHED            0x1
6122315Sjulian#define PTHREAD_SCOPE_SYSTEM        0x2
6222315Sjulian#define PTHREAD_INHERIT_SCHED       0x4
6322315Sjulian#define PTHREAD_NOFLOAT             0x8
6422315Sjulian
6522315Sjulian#define PTHREAD_CREATE_DETACHED     PTHREAD_DETACHED
6622315Sjulian#define PTHREAD_CREATE_JOINABLE     0
6722315Sjulian#define PTHREAD_SCOPE_PROCESS       0
6822315Sjulian#define PTHREAD_EXPLICIT_SCHED      0
6922315Sjulian
7022315Sjulian/*
7138919Salex * Flags for read/write lock attributes
7238919Salex */
7338919Salex#define PTHREAD_PROCESS_PRIVATE     0
7438919Salex#define PTHREAD_PROCESS_SHARED      1
7538919Salex
7638919Salex/*
7753812Salfred * Flags for cancelling threads
7853812Salfred */
7953812Salfred#define PTHREAD_CANCEL_ENABLE		0
8053812Salfred#define PTHREAD_CANCEL_DISABLE		1
8153812Salfred#define PTHREAD_CANCEL_DEFERRED		0
8253812Salfred#define PTHREAD_CANCEL_ASYNCHRONOUS	2
8353812Salfred#define PTHREAD_CANCELED		((void *) 1)
8453812Salfred
8553812Salfred/*
8617706Sjulian * Forward structure definitions.
8717706Sjulian *
8817706Sjulian * These are mostly opaque to the user.
8913547Sjulian */
9017706Sjulianstruct pthread;
9117706Sjulianstruct pthread_attr;
9217706Sjulianstruct pthread_cond;
9317706Sjulianstruct pthread_cond_attr;
9417706Sjulianstruct pthread_mutex;
9517706Sjulianstruct pthread_mutex_attr;
9617706Sjulianstruct pthread_once;
9738919Salexstruct pthread_rwlock;
9838919Salexstruct pthread_rwlockattr;
99119736Sdavidxustruct pthread_barrier;
100119736Sdavidxustruct pthread_barrier_attr;
101119909Sdavidxustruct pthread_spinlock;
10213547Sjulian
10313547Sjulian/*
10417706Sjulian * Primitive system data type definitions required by P1003.1c.
10517706Sjulian *
10617706Sjulian * Note that P1003.1c specifies that there are no defined comparison
10717706Sjulian * or assignment operators for the types pthread_attr_t, pthread_cond_t,
10817706Sjulian * pthread_condattr_t, pthread_mutex_t, pthread_mutexattr_t.
10913547Sjulian */
11017706Sjuliantypedef struct	pthread			*pthread_t;
11117706Sjuliantypedef struct	pthread_attr		*pthread_attr_t;
11217706Sjuliantypedef struct	pthread_mutex		*pthread_mutex_t;
11317706Sjuliantypedef struct	pthread_mutex_attr	*pthread_mutexattr_t;
11417706Sjuliantypedef struct	pthread_cond		*pthread_cond_t;
11517706Sjuliantypedef struct	pthread_cond_attr	*pthread_condattr_t;
11617706Sjuliantypedef int     			pthread_key_t;
11717706Sjuliantypedef struct	pthread_once		pthread_once_t;
11838919Salextypedef struct	pthread_rwlock		*pthread_rwlock_t;
11938919Salextypedef struct	pthread_rwlockattr	*pthread_rwlockattr_t;
120119736Sdavidxutypedef struct	pthread_barrier		*pthread_barrier_t;
121119736Sdavidxutypedef struct	pthread_barrierattr	*pthread_barrierattr_t;
122119909Sdavidxutypedef struct	pthread_spinlock	*pthread_spinlock_t;
12313547Sjulian
12413547Sjulian/*
12517706Sjulian * Additional type definitions:
12617706Sjulian *
12717706Sjulian * Note that P1003.1c reserves the prefixes pthread_ and PTHREAD_ for
12817706Sjulian * use in header symbols.
12913547Sjulian */
13017706Sjuliantypedef void	*pthread_addr_t;
13193032Simptypedef void	*(*pthread_startroutine_t)(void *);
13213547Sjulian
13313547Sjulian/*
13413547Sjulian * Once definitions.
13513547Sjulian */
13613547Sjulianstruct pthread_once {
13717706Sjulian	int		state;
13817706Sjulian	pthread_mutex_t	mutex;
13913547Sjulian};
14013547Sjulian
14113547Sjulian/*
14213547Sjulian * Flags for once initialization.
14313547Sjulian */
14413547Sjulian#define PTHREAD_NEEDS_INIT  0
14513547Sjulian#define PTHREAD_DONE_INIT   1
14613547Sjulian
14713547Sjulian/*
14813547Sjulian * Static once initialization values.
14913547Sjulian */
15017706Sjulian#define PTHREAD_ONCE_INIT   { PTHREAD_NEEDS_INIT, NULL }
15113547Sjulian
15213547Sjulian/*
15335025Sjb * Static initialization values.
15413547Sjulian */
15535025Sjb#define PTHREAD_MUTEX_INITIALIZER	NULL
15635025Sjb#define PTHREAD_COND_INITIALIZER	NULL
15738919Salex#define PTHREAD_RWLOCK_INITIALIZER	NULL
15835025Sjb
15935025Sjb/*
16035025Sjb * Default attribute arguments (draft 4, deprecated).
16135025Sjb */
16222315Sjulian#ifndef PTHREAD_KERNEL
16313547Sjulian#define pthread_condattr_default    NULL
16413547Sjulian#define pthread_mutexattr_default   NULL
16513547Sjulian#define pthread_attr_default        NULL
16613547Sjulian#endif
16713547Sjulian
16844965Sjb#define PTHREAD_PRIO_NONE	0
16944965Sjb#define PTHREAD_PRIO_INHERIT	1
17044965Sjb#define PTHREAD_PRIO_PROTECT	2
17144965Sjb
17244965Sjb/*
17344965Sjb * Mutex types (Single UNIX Specification, Version 2, 1997).
17444965Sjb *
17544965Sjb * Note that a mutex attribute with one of the following types:
17644965Sjb *
17744965Sjb *	PTHREAD_MUTEX_NORMAL
17844965Sjb *	PTHREAD_MUTEX_RECURSIVE
17944965Sjb *      MUTEX_TYPE_FAST (deprecated)
18044965Sjb *	MUTEX_TYPE_COUNTING_FAST (deprecated)
18144965Sjb *
18244965Sjb * will deviate from POSIX specified semantics.
18344965Sjb */
18419637Shsuenum pthread_mutextype {
18544965Sjb	PTHREAD_MUTEX_ERRORCHECK	= 1,	/* Default POSIX mutex */
18644965Sjb	PTHREAD_MUTEX_RECURSIVE		= 2,	/* Recursive mutex */
18744965Sjb	PTHREAD_MUTEX_NORMAL		= 3,	/* No error checking */
18819637Shsu	MUTEX_TYPE_MAX
18919637Shsu};
19019637Shsu
19144965Sjb#define PTHREAD_MUTEX_DEFAULT		PTHREAD_MUTEX_ERRORCHECK
19244965Sjb#define MUTEX_TYPE_FAST			PTHREAD_MUTEX_NORMAL
19341390Seivind#define MUTEX_TYPE_COUNTING_FAST	PTHREAD_MUTEX_RECURSIVE
19441390Seivind
19513547Sjulian/*
19613547Sjulian * Thread function prototype definitions:
19713547Sjulian */
19813547Sjulian__BEGIN_DECLS
199122076Sdeischenint		pthread_atfork(void (*prepare)(void), void (*parent)(void),
200122076Sdeischen			void (*child)(void));
20193032Simpint		pthread_attr_destroy(pthread_attr_t *);
202110636Salfredint		pthread_attr_getstack(const pthread_attr_t * __restrict,
203110636Salfred			void ** __restrict stackaddr,
204110636Salfred			size_t * __restrict stacksize);
20593032Simpint		pthread_attr_getstacksize(const pthread_attr_t *, size_t *);
20693032Simpint		pthread_attr_getguardsize(const pthread_attr_t *, size_t *);
20793032Simpint		pthread_attr_getstackaddr(const pthread_attr_t *, void **);
20893032Simpint		pthread_attr_getdetachstate(const pthread_attr_t *, int *);
20993032Simpint		pthread_attr_init(pthread_attr_t *);
21093032Simpint		pthread_attr_setstacksize(pthread_attr_t *, size_t);
21193032Simpint		pthread_attr_setguardsize(pthread_attr_t *, size_t);
212110636Salfredint		pthread_attr_setstack(pthread_attr_t *, void *, size_t);
21393032Simpint		pthread_attr_setstackaddr(pthread_attr_t *, void *);
21493032Simpint		pthread_attr_setdetachstate(pthread_attr_t *, int);
215119736Sdavidxuint		pthread_barrier_destroy(pthread_barrier_t *);
216119736Sdavidxuint		pthread_barrier_init(pthread_barrier_t *,
217119736Sdavidxu			const pthread_barrierattr_t *, unsigned);
218119736Sdavidxuint		pthread_barrier_wait(pthread_barrier_t *);
219119736Sdavidxuint		pthread_barrierattr_destroy(pthread_barrierattr_t *);
220119736Sdavidxuint		pthread_barrierattr_getpshared(const pthread_barrierattr_t *,
221119736Sdavidxu			int *);
222119736Sdavidxuint		pthread_barrierattr_init(pthread_barrierattr_t *);
223119736Sdavidxuint		pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
22493032Simpvoid		pthread_cleanup_pop(int);
22593032Simpvoid		pthread_cleanup_push(void (*) (void *), void *routine_arg);
22693032Simpint		pthread_condattr_destroy(pthread_condattr_t *);
22793032Simpint		pthread_condattr_init(pthread_condattr_t *);
22844965Sjb
22993032Simpint		pthread_cond_broadcast(pthread_cond_t *);
23093032Simpint		pthread_cond_destroy(pthread_cond_t *);
23193032Simpint		pthread_cond_init(pthread_cond_t *,
23293032Simp			const pthread_condattr_t *);
23393032Simpint		pthread_cond_signal(pthread_cond_t *);
23493032Simpint		pthread_cond_timedwait(pthread_cond_t *,
23593032Simp			pthread_mutex_t *, const struct timespec *);
23693032Simpint		pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
23793032Simpint		pthread_create(pthread_t *, const pthread_attr_t *,
23893032Simp			void *(*) (void *), void *);
23993032Simpint		pthread_detach(pthread_t);
24093032Simpint		pthread_equal(pthread_t, pthread_t);
24193032Simpvoid		pthread_exit(void *) __dead2;
24293032Simpvoid		*pthread_getspecific(pthread_key_t);
24393032Simpint		pthread_join(pthread_t, void **);
24493032Simpint		pthread_key_create(pthread_key_t *,
24593032Simp			void (*) (void *));
24693032Simpint		pthread_key_delete(pthread_key_t);
24793032Simpint		pthread_kill(pthread_t, int);
24893032Simpint		pthread_mutexattr_init(pthread_mutexattr_t *);
24993032Simpint		pthread_mutexattr_destroy(pthread_mutexattr_t *);
25093032Simpint		pthread_mutexattr_gettype(pthread_mutexattr_t *, int *);
25193032Simpint		pthread_mutexattr_settype(pthread_mutexattr_t *, int);
25293032Simpint		pthread_mutex_destroy(pthread_mutex_t *);
25393032Simpint		pthread_mutex_init(pthread_mutex_t *,
25493032Simp			const pthread_mutexattr_t *);
25593032Simpint		pthread_mutex_lock(pthread_mutex_t *);
25693032Simpint		pthread_mutex_trylock(pthread_mutex_t *);
257119736Sdavidxuint		pthread_mutex_timedlock(pthread_mutex_t *,
258119736Sdavidxu			const struct timespec *);
25993032Simpint		pthread_mutex_unlock(pthread_mutex_t *);
26093032Simpint		pthread_once(pthread_once_t *, void (*) (void));
26193032Simpint		pthread_rwlock_destroy(pthread_rwlock_t *);
26293032Simpint		pthread_rwlock_init(pthread_rwlock_t *,
26393032Simp			const pthread_rwlockattr_t *);
26493032Simpint		pthread_rwlock_rdlock(pthread_rwlock_t *);
265119790Sdavidxuint		pthread_rwlock_timedrdlock(pthread_rwlock_t *,
266119790Sdavidxu			const struct timespec *);
267139902Sdavidxuint		pthread_rwlock_timedwrlock(pthread_rwlock_t *,
268119790Sdavidxu			const struct timespec *);
26993032Simpint		pthread_rwlock_tryrdlock(pthread_rwlock_t *);
27093032Simpint		pthread_rwlock_trywrlock(pthread_rwlock_t *);
27193032Simpint		pthread_rwlock_unlock(pthread_rwlock_t *);
27293032Simpint		pthread_rwlock_wrlock(pthread_rwlock_t *);
27393032Simpint		pthread_rwlockattr_init(pthread_rwlockattr_t *);
27493032Simpint		pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *,
27593032Simp			int *);
27693032Simpint		pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int);
27793032Simpint		pthread_rwlockattr_destroy(pthread_rwlockattr_t *);
27893032Simppthread_t	pthread_self(void);
27993032Simpint		pthread_setspecific(pthread_key_t, const void *);
28093032Simpint		pthread_sigmask(int, const sigset_t *, sigset_t *);
28117706Sjulian
282119909Sdavidxuint		pthread_spin_init(pthread_spinlock_t *, int);
283119909Sdavidxuint		pthread_spin_destroy(pthread_spinlock_t *);
284119909Sdavidxuint		pthread_spin_lock(pthread_spinlock_t *);
285119909Sdavidxuint		pthread_spin_trylock(pthread_spinlock_t *);
286119909Sdavidxuint		pthread_spin_unlock(pthread_spinlock_t *);
28793032Simpint		pthread_cancel(pthread_t);
28893032Simpint		pthread_setcancelstate(int, int *);
28993032Simpint		pthread_setcanceltype(int, int *);
29093032Simpvoid		pthread_testcancel(void);
29117706Sjulian
29293032Simpint		pthread_getprio(pthread_t);
29393032Simpint		pthread_setprio(pthread_t, int);
29493032Simpvoid		pthread_yield(void);
29544965Sjb
29693032Simpint		pthread_mutexattr_getprioceiling(pthread_mutexattr_t *,
29793032Simp			int *);
29893032Simpint		pthread_mutexattr_setprioceiling(pthread_mutexattr_t *,
29993032Simp			int);
30093032Simpint		pthread_mutex_getprioceiling(pthread_mutex_t *, int *);
30193032Simpint		pthread_mutex_setprioceiling(pthread_mutex_t *, int, int *);
30244965Sjb
30393032Simpint		pthread_mutexattr_getprotocol(pthread_mutexattr_t *, int *);
30493032Simpint		pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
30544965Sjb
30693032Simpint		pthread_attr_getinheritsched(const pthread_attr_t *, int *);
30793032Simpint		pthread_attr_getschedparam(const pthread_attr_t *,
30893032Simp			struct sched_param *);
30993032Simpint		pthread_attr_getschedpolicy(const pthread_attr_t *, int *);
31093032Simpint		pthread_attr_getscope(const pthread_attr_t *, int *);
31193032Simpint		pthread_attr_setinheritsched(pthread_attr_t *, int);
31293032Simpint		pthread_attr_setschedparam(pthread_attr_t *,
31393032Simp			const struct sched_param *);
31493032Simpint		pthread_attr_setschedpolicy(pthread_attr_t *, int);
31593032Simpint		pthread_attr_setscope(pthread_attr_t *, int);
31693032Simpint		pthread_getschedparam(pthread_t pthread, int *,
31793032Simp			struct sched_param *);
31893032Simpint		pthread_setschedparam(pthread_t, int,
31993032Simp			const struct sched_param *);
320113729Sjdpint		pthread_getconcurrency(void);
321113729Sjdpint		pthread_setconcurrency(int);
32213547Sjulian__END_DECLS
32313547Sjulian
32413547Sjulian#endif
325