pthread.h revision 44965
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 *
3313547Sjulian */
3413547Sjulian#ifndef _PTHREAD_H_
3513547Sjulian#define _PTHREAD_H_
3613547Sjulian
3713547Sjulian/*
3813547Sjulian * Header files.
3913547Sjulian */
4013547Sjulian#include <sys/cdefs.h>
4113547Sjulian#include <sys/types.h>
4213547Sjulian#include <sys/time.h>
4317706Sjulian#include <sys/signal.h>
4417706Sjulian#include <limits.h>
4544965Sjb#include <sched.h>
4613547Sjulian
4713547Sjulian/*
4817706Sjulian * Run-time invariant values:
4913547Sjulian */
5017706Sjulian#define PTHREAD_DESTRUCTOR_ITERATIONS		4
5117706Sjulian#define PTHREAD_KEYS_MAX			256
5217706Sjulian#define PTHREAD_STACK_MIN			1024
5317706Sjulian#define PTHREAD_THREADS_MAX			ULONG_MAX
5413547Sjulian
5513547Sjulian/*
5617706Sjulian * Compile time symbolic constants for portability specifications:
5717706Sjulian *
5817706Sjulian * Note that those commented out are not currently supported by the
5917706Sjulian * implementation.
6013547Sjulian */
6117706Sjulian#define _POSIX_THREADS
6217706Sjulian#define _POSIX_THREAD_ATTR_STACKADDR
6317706Sjulian#define _POSIX_THREAD_ATTR_STACKSIZE
6444965Sjb#define _POSIX_THREAD_PRIORITY_SCHEDULING
6544965Sjb#define _POSIX_THREAD_PRIO_INHERIT
6644965Sjb#define _POSIX_THREAD_PRIO_PROTECT
6717706Sjulian/* #define _POSIX_THREAD_PROCESS_SHARED */
6817706Sjulian#define _POSIX_THREAD_SAFE_FUNCTIONS
6913547Sjulian
7013547Sjulian/*
7122315Sjulian * Flags for threads and thread attributes.
7222315Sjulian */
7322315Sjulian#define PTHREAD_DETACHED            0x1
7422315Sjulian#define PTHREAD_SCOPE_SYSTEM        0x2
7522315Sjulian#define PTHREAD_INHERIT_SCHED       0x4
7622315Sjulian#define PTHREAD_NOFLOAT             0x8
7722315Sjulian
7822315Sjulian#define PTHREAD_CREATE_DETACHED     PTHREAD_DETACHED
7922315Sjulian#define PTHREAD_CREATE_JOINABLE     0
8022315Sjulian#define PTHREAD_SCOPE_PROCESS       0
8122315Sjulian#define PTHREAD_EXPLICIT_SCHED      0
8222315Sjulian
8322315Sjulian/*
8438919Salex * Flags for read/write lock attributes
8538919Salex */
8638919Salex#define PTHREAD_PROCESS_PRIVATE     0
8738919Salex#define PTHREAD_PROCESS_SHARED      1
8838919Salex
8938919Salex/*
9017706Sjulian * Forward structure definitions.
9117706Sjulian *
9217706Sjulian * These are mostly opaque to the user.
9313547Sjulian */
9417706Sjulianstruct pthread;
9517706Sjulianstruct pthread_attr;
9617706Sjulianstruct pthread_cond;
9717706Sjulianstruct pthread_cond_attr;
9817706Sjulianstruct pthread_mutex;
9917706Sjulianstruct pthread_mutex_attr;
10017706Sjulianstruct pthread_once;
10138919Salexstruct pthread_rwlock;
10238919Salexstruct pthread_rwlockattr;
10313547Sjulian
10413547Sjulian/*
10517706Sjulian * Primitive system data type definitions required by P1003.1c.
10617706Sjulian *
10717706Sjulian * Note that P1003.1c specifies that there are no defined comparison
10817706Sjulian * or assignment operators for the types pthread_attr_t, pthread_cond_t,
10917706Sjulian * pthread_condattr_t, pthread_mutex_t, pthread_mutexattr_t.
11013547Sjulian */
11117706Sjuliantypedef struct	pthread			*pthread_t;
11217706Sjuliantypedef struct	pthread_attr		*pthread_attr_t;
11317706Sjuliantypedef struct	pthread_mutex		*pthread_mutex_t;
11417706Sjuliantypedef struct	pthread_mutex_attr	*pthread_mutexattr_t;
11517706Sjuliantypedef struct	pthread_cond		*pthread_cond_t;
11617706Sjuliantypedef struct	pthread_cond_attr	*pthread_condattr_t;
11717706Sjuliantypedef int     			pthread_key_t;
11817706Sjuliantypedef struct	pthread_once		pthread_once_t;
11938919Salextypedef struct	pthread_rwlock		*pthread_rwlock_t;
12038919Salextypedef struct	pthread_rwlockattr	*pthread_rwlockattr_t;
12113547Sjulian
12213547Sjulian/*
12317706Sjulian * Additional type definitions:
12417706Sjulian *
12517706Sjulian * Note that P1003.1c reserves the prefixes pthread_ and PTHREAD_ for
12617706Sjulian * use in header symbols.
12713547Sjulian */
12817706Sjuliantypedef void	*pthread_addr_t;
12925519Sbdetypedef void	*(*pthread_startroutine_t) __P((void *));
13013547Sjulian
13113547Sjulian/*
13213547Sjulian * Once definitions.
13313547Sjulian */
13413547Sjulianstruct pthread_once {
13517706Sjulian	int		state;
13617706Sjulian	pthread_mutex_t	mutex;
13713547Sjulian};
13813547Sjulian
13913547Sjulian/*
14013547Sjulian * Flags for once initialization.
14113547Sjulian */
14213547Sjulian#define PTHREAD_NEEDS_INIT  0
14313547Sjulian#define PTHREAD_DONE_INIT   1
14413547Sjulian
14513547Sjulian/*
14613547Sjulian * Static once initialization values.
14713547Sjulian */
14817706Sjulian#define PTHREAD_ONCE_INIT   { PTHREAD_NEEDS_INIT, NULL }
14913547Sjulian
15013547Sjulian/*
15135025Sjb * Static initialization values.
15213547Sjulian */
15335025Sjb#define PTHREAD_MUTEX_INITIALIZER	NULL
15435025Sjb#define PTHREAD_COND_INITIALIZER	NULL
15538919Salex#define PTHREAD_RWLOCK_INITIALIZER	NULL
15635025Sjb
15735025Sjb/*
15835025Sjb * Default attribute arguments (draft 4, deprecated).
15935025Sjb */
16022315Sjulian#ifndef PTHREAD_KERNEL
16113547Sjulian#define pthread_condattr_default    NULL
16213547Sjulian#define pthread_mutexattr_default   NULL
16313547Sjulian#define pthread_attr_default        NULL
16413547Sjulian#endif
16513547Sjulian
16644965Sjb#define PTHREAD_PRIO_NONE	0
16744965Sjb#ifdef _POSIX_THREAD_PRIO_PROTECT
16844965Sjb#define PTHREAD_PRIO_INHERIT	1
16944965Sjb#define PTHREAD_PRIO_PROTECT	2
17044965Sjb#endif
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
19917706Sjulianint		pthread_attr_destroy __P((pthread_attr_t *));
20017706Sjulianint		pthread_attr_getstacksize __P((pthread_attr_t *, size_t *));
20117706Sjulianint		pthread_attr_getstackaddr __P((pthread_attr_t *, void **));
20217706Sjulianint		pthread_attr_getdetachstate __P((pthread_attr_t *, int *));
20317706Sjulianint		pthread_attr_init __P((pthread_attr_t *));
20417706Sjulianint		pthread_attr_setstacksize __P((pthread_attr_t *, size_t));
20517706Sjulianint		pthread_attr_setstackaddr __P((pthread_attr_t *, void *));
20617706Sjulianint		pthread_attr_setdetachstate __P((pthread_attr_t *, int));
20717706Sjulianvoid		pthread_cleanup_pop __P((int execute));
20822315Sjulianvoid		pthread_cleanup_push __P((void (*routine) (void *),
20917706Sjulian			void *routine_arg));
21017706Sjulianint		pthread_condattr_destroy __P((pthread_condattr_t *attr));
21117706Sjulianint		pthread_condattr_init __P((pthread_condattr_t *attr));
21244965Sjb
21344965Sjb#if defined(_POSIX_THREAD_PROCESS_SHARED)
21417706Sjulianint		pthread_condattr_getpshared __P((pthread_condattr_t *attr,
21517706Sjulian			int *pshared));
21617706Sjulianint		pthread_condattr_setpshared __P((pthread_condattr_t *attr,
21717706Sjulian			int pshared));
21844965Sjb#endif
21944965Sjb
22017706Sjulianint		pthread_cond_broadcast __P((pthread_cond_t *));
22117706Sjulianint		pthread_cond_destroy __P((pthread_cond_t *));
22217706Sjulianint		pthread_cond_init __P((pthread_cond_t *,
22317706Sjulian			const pthread_condattr_t *));
22417706Sjulianint		pthread_cond_signal __P((pthread_cond_t *));
22517706Sjulianint		pthread_cond_timedwait __P((pthread_cond_t *,
22617706Sjulian			pthread_mutex_t *, const struct timespec * abstime));
22717706Sjulianint		pthread_cond_wait __P((pthread_cond_t *, pthread_mutex_t *));
22817706Sjulianint		pthread_create __P((pthread_t *, const pthread_attr_t *,
22917706Sjulian			void *(*start_routine) (void *), void *));
23031985Salexint		pthread_detach __P((pthread_t));
23117706Sjulianint		pthread_equal __P((pthread_t, pthread_t));
23217706Sjulianvoid		pthread_exit __P((void *));
23319637Shsuvoid		*pthread_getspecific __P((pthread_key_t));
23417706Sjulianint		pthread_join __P((pthread_t, void **));
23517706Sjulianint		pthread_key_create __P((pthread_key_t *,
23617706Sjulian			void (*routine) (void *)));
23717706Sjulianint		pthread_key_delete __P((pthread_key_t));
23817706Sjulianint		pthread_kill __P((struct pthread *, int));
23944965Sjbint		pthread_mutexattr_init __P((pthread_mutexattr_t *));
24017706Sjulianint		pthread_mutexattr_destroy __P((pthread_mutexattr_t *));
24141390Seivindint		pthread_mutexattr_settype __P((pthread_mutexattr_t *, int));
24217706Sjulianint		pthread_mutex_destroy __P((pthread_mutex_t *));
24317706Sjulianint		pthread_mutex_init __P((pthread_mutex_t *,
24417706Sjulian			const pthread_mutexattr_t *));
24517706Sjulianint		pthread_mutex_lock __P((pthread_mutex_t *));
24617706Sjulianint		pthread_mutex_trylock __P((pthread_mutex_t *));
24717706Sjulianint		pthread_mutex_unlock __P((pthread_mutex_t *));
24817706Sjulianint		pthread_once __P((pthread_once_t *,
24917706Sjulian			void (*init_routine) (void)));
25038919Salexint		pthread_rwlock_destroy __P((pthread_rwlock_t *));
25138919Salexint		pthread_rwlock_init __P((pthread_rwlock_t *,
25238919Salex			const pthread_rwlockattr_t *));
25338919Salexint		pthread_rwlock_rdlock __P((pthread_rwlock_t *));
25438919Salexint		pthread_rwlock_tryrdlock __P((pthread_rwlock_t *));
25538919Salexint		pthread_rwlock_trywrlock __P((pthread_rwlock_t *));
25638919Salexint		pthread_rwlock_unlock __P((pthread_rwlock_t *));
25738919Salexint		pthread_rwlock_wrlock __P((pthread_rwlock_t *));
25838919Salexint		pthread_rwlockattr_init __P((pthread_rwlockattr_t *));
25938919Salexint		pthread_rwlockattr_getpshared __P((const pthread_rwlockattr_t *,
26038919Salex			int *));
26138919Salexint		pthread_rwlockattr_setpshared __P((pthread_rwlockattr_t *,
26238919Salex			int *));
26338919Salexint		pthread_rwlockattr_destroy __P((pthread_rwlockattr_t *));
26417706Sjulianpthread_t	pthread_self __P((void));
26517706Sjulianint		pthread_setcancelstate __P((int, int *));
26617706Sjulianint		pthread_setcanceltype __P((int, int *));
26717706Sjulianint		pthread_setspecific __P((pthread_key_t, const void *));
26817706Sjulianint		pthread_sigmask __P((int, const sigset_t *, sigset_t *));
26917706Sjulianint		pthread_testcancel __P((void));
27017706Sjulian
27117706Sjulian
27217706Sjulianint		pthread_getprio __P((pthread_t));
27317706Sjulianint		pthread_setprio __P((pthread_t, int));
27417706Sjulianvoid		pthread_yield __P((void));
27544965Sjb
27644965Sjb#if defined(_POSIX_THREAD_PROCESS_SHARED)
27744965Sjbint		pthread_mutexattr_getpshared __P((pthread_mutexattr_t *,
27844965Sjb			int *pshared));
27944965Sjbint		pthread_mutexattr_setpshared __P((pthread_mutexattr_t *,
28044965Sjb			int pshared));
28144965Sjb#endif
28244965Sjb
28344965Sjb#if defined(_POSIX_THREAD_PRIO_PROTECT)
28444965Sjbint		pthread_mutexattr_getprioceiling __P((pthread_mutexattr_t *,
28544965Sjb			int *prioceiling));
28644965Sjbint		pthread_mutexattr_setprioceiling __P((pthread_mutexattr_t *,
28744965Sjb			int prioceiling));
28844965Sjbint		pthread_mutex_getprioceiling __P((pthread_mutex_t *, int *));
28944965Sjbint		pthread_mutex_setprioceiling __P((pthread_mutex_t *, int, int *));
29044965Sjb#endif
29144965Sjb
29244965Sjb#if defined(_POSIX_THREAD_PRIO_PROTECT) || defined (_POSIX_THREAD_PRIO_INHERIT)
29344965Sjbint		pthread_mutexattr_getprotocol __P((pthread_mutexattr_t *,
29444965Sjb			int *protocol));
29544965Sjbint		pthread_mutexattr_setprotocol __P((pthread_mutexattr_t *,
29644965Sjb			int protocol));
29744965Sjb#endif
29844965Sjb
29944965Sjb#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
30044965Sjbint		pthread_attr_getinheritsched __P((pthread_attr_t *, int *));
30144965Sjbint		pthread_attr_getschedparam __P((pthread_attr_t *,
30244965Sjb			struct sched_param *));
30344965Sjbint		pthread_attr_getschedpolicy __P((pthread_attr_t *, int *));
30444965Sjbint		pthread_attr_getscope __P((pthread_attr_t *, int *));
30544965Sjbint		pthread_attr_setinheritsched __P((pthread_attr_t *, int));
30644965Sjbint		pthread_attr_setschedparam __P((pthread_attr_t *,
30744965Sjb			struct sched_param *));
30844965Sjbint		pthread_attr_setschedpolicy __P((pthread_attr_t *, int));
30944965Sjbint		pthread_attr_setscope __P((pthread_attr_t *, int));
31044965Sjbint		pthread_getschedparam __P((pthread_t pthread, int *policy,
31144965Sjb			struct sched_param * param));
31217706Sjulianint		pthread_setschedparam __P((pthread_t pthread, int policy,
31317706Sjulian			struct sched_param * param));
31444965Sjb#endif
31544965Sjb
31617706Sjulianint		pthread_attr_setfloatstate __P((pthread_attr_t *, int));
31717706Sjulianint		pthread_attr_getfloatstate __P((pthread_attr_t *, int *));
31817706Sjulianint		pthread_attr_setcleanup __P((pthread_attr_t *,
31917706Sjulian			void (*routine) (void *), void *));
32013547Sjulian__END_DECLS
32113547Sjulian
32213547Sjulian#endif
323