pthread.h revision 0e0f49e7
1/*
2 * Copyright 2001-2011 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the Haiku License.
4 */
5#ifndef _PTHREAD_H_
6#define _PTHREAD_H_
7
8
9#include <sched.h>
10#include <stdint.h>
11#include <sys/types.h>
12#include <time.h>
13
14
15#define PTHREAD_MUTEX_DEFAULT		0
16#define PTHREAD_MUTEX_NORMAL		1
17#define PTHREAD_MUTEX_ERRORCHECK	2
18#define PTHREAD_MUTEX_RECURSIVE		3
19
20#define PTHREAD_PROCESS_PRIVATE		0
21#define PTHREAD_PROCESS_SHARED		1
22
23/*
24 * Flags for threads and thread attributes.
25 */
26#define PTHREAD_DETACHED			0x1
27#define PTHREAD_SCOPE_SYSTEM		0x2
28#define PTHREAD_INHERIT_SCHED		0x4
29#define PTHREAD_NOFLOAT				0x8
30
31#define PTHREAD_CREATE_DETACHED		PTHREAD_DETACHED
32#define PTHREAD_CREATE_JOINABLE		0
33#define PTHREAD_SCOPE_PROCESS		0
34#define PTHREAD_EXPLICIT_SCHED		0
35
36/*
37 * Flags for cancelling threads
38 */
39#define PTHREAD_CANCEL_ENABLE		0
40#define PTHREAD_CANCEL_DISABLE		1
41#define PTHREAD_CANCEL_DEFERRED		0
42#define PTHREAD_CANCEL_ASYNCHRONOUS	2
43#define PTHREAD_CANCELED			((void *) 1)
44
45#define PTHREAD_ONCE_INIT 			{ -1 }
46
47#define PTHREAD_BARRIER_SERIAL_THREAD -1
48#define PTHREAD_PRIO_NONE			0
49#define PTHREAD_PRIO_INHERIT		1
50#define PTHREAD_PRIO_PROTECT		2
51
52/* private structure */
53struct __pthread_cleanup_handler {
54	struct __pthread_cleanup_handler *previous;
55	void	(*function)(void *argument);
56	void	*argument;
57};
58
59#define pthread_cleanup_push(func, arg) \
60	do { \
61		struct __pthread_cleanup_handler __handler; \
62		__handler.function = (func); \
63		__handler.argument = (arg); \
64		__pthread_cleanup_push_handler(&__handler);
65
66#define pthread_cleanup_pop(execute) \
67		if (execute) \
68			__handler.function(__handler.argument); \
69		__pthread_cleanup_pop_handler(); \
70	} while (0)
71
72
73#ifdef __cplusplus
74extern "C" {
75#endif
76
77#define PTHREAD_MUTEX_INITIALIZER \
78	{ PTHREAD_MUTEX_DEFAULT, 0, -42, -1, 0 }
79#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER \
80	{ PTHREAD_MUTEX_RECURSIVE, 0, -42, -1, 0 }
81#define PTHREAD_COND_INITIALIZER	\
82	{ 0, -42, NULL, 0, 0 }
83
84/* mutex functions */
85extern int pthread_mutex_destroy(pthread_mutex_t *mutex);
86extern int pthread_mutex_getprioceiling(pthread_mutex_t *mutex,
87	int *_priorityCeiling);
88extern int pthread_mutex_init(pthread_mutex_t *mutex,
89	const pthread_mutexattr_t *attr);
90extern int pthread_mutex_lock(pthread_mutex_t *mutex);
91extern int pthread_mutex_setprioceiling(pthread_mutex_t *mutex,
92	int newPriorityCeiling, int *_oldPriorityCeiling);
93extern int pthread_mutex_timedlock(pthread_mutex_t *mutex,
94	const struct timespec *spec);
95extern int pthread_mutex_trylock(pthread_mutex_t *mutex);
96extern int pthread_mutex_unlock(pthread_mutex_t *mutex);
97
98/* mutex attribute functions */
99extern int pthread_mutexattr_destroy(pthread_mutexattr_t *mutexAttr);
100extern int pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mutexAttr,
101	int *_priorityCeiling);
102extern int pthread_mutexattr_getprotocol(pthread_mutexattr_t *mutexAttr,
103	int *_protocol);
104extern int pthread_mutexattr_getpshared(pthread_mutexattr_t *mutexAttr,
105	int *_processShared);
106extern int pthread_mutexattr_gettype(pthread_mutexattr_t *mutexAttr,
107	int *_type);
108extern int pthread_mutexattr_init(pthread_mutexattr_t *mutexAttr);
109extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mutexAttr,
110	int priorityCeiling);
111extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *mutexAttr,
112	int protocol);
113extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *mutexAttr,
114	int processShared);
115extern int pthread_mutexattr_settype(pthread_mutexattr_t *mutexAttr, int type);
116
117/* barrier functions */
118extern int pthread_barrier_init(pthread_barrier_t *barrier,
119	const pthread_barrierattr_t *attr, unsigned count);
120extern int pthread_barrier_destroy(pthread_barrier_t *barrier);
121extern int pthread_barrier_wait(pthread_barrier_t *barrier);
122
123/* barrier attribute functions */
124extern int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
125extern int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
126	int *shared);
127extern int pthread_barrierattr_init(pthread_barrierattr_t *attr);
128extern int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr,
129	int shared);
130
131/* condition variable functions */
132extern int pthread_cond_destroy(pthread_cond_t *cond);
133extern int pthread_cond_init(pthread_cond_t *cond,
134	const pthread_condattr_t *attr);
135extern int pthread_cond_broadcast(pthread_cond_t *cond);
136extern int pthread_cond_signal(pthread_cond_t *cond);
137extern int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
138	const struct timespec *abstime);
139extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
140
141/* condition variable attribute functions */
142extern int pthread_condattr_destroy(pthread_condattr_t *condAttr);
143extern int pthread_condattr_init(pthread_condattr_t *condAttr);
144extern int pthread_condattr_getpshared(const pthread_condattr_t *condAttr,
145	int *processShared);
146extern int pthread_condattr_setpshared(pthread_condattr_t *condAttr,
147	int processShared);
148extern int pthread_condattr_getclock(const pthread_condattr_t *condAttr,
149	clockid_t *clockID);
150extern int pthread_condattr_setclock(pthread_condattr_t *condAttr,
151	clockid_t clockID);
152
153/* rwlock functions */
154extern int pthread_rwlock_init(pthread_rwlock_t *lock,
155	const pthread_rwlockattr_t *attr);
156extern int pthread_rwlock_destroy(pthread_rwlock_t *lock);
157extern int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
158extern int pthread_rwlock_tryrdlock(pthread_rwlock_t *lock);
159extern int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
160	const struct timespec *timeout);
161extern int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
162extern int pthread_rwlock_trywrlock(pthread_rwlock_t *lock);
163extern int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
164	const struct timespec *timeout);
165extern int pthread_rwlock_unlock(pthread_rwlock_t *lock);
166
167/* rwlock attribute functions */
168extern int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
169extern int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
170extern int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
171	int *shared);
172extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr,
173	int shared);
174
175/* spinlock functions */
176extern int pthread_spin_init(pthread_spinlock_t* spinlock, int pshared);
177extern int pthread_spin_destroy(pthread_spinlock_t* spinlock);
178extern int pthread_spin_lock(pthread_spinlock_t* spinlock);
179extern int pthread_spin_trylock(pthread_spinlock_t* spinlock);
180extern int pthread_spin_unlock(pthread_spinlock_t* spinlock);
181
182/* misc. functions */
183extern int pthread_atfork(void (*prepare)(void), void (*parent)(void),
184	void (*child)(void));
185extern int pthread_once(pthread_once_t *once_control,
186	void (*init_routine)(void));
187
188/* thread attributes functions */
189extern int pthread_attr_destroy(pthread_attr_t *attr);
190extern int pthread_attr_init(pthread_attr_t *attr);
191extern int pthread_attr_getdetachstate(const pthread_attr_t *attr,
192	int *detachstate);
193extern int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
194extern int pthread_attr_getstacksize(const pthread_attr_t *attr,
195	size_t *stacksize);
196extern int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
197extern int pthread_attr_getscope(const pthread_attr_t *attr,
198	int *contentionScope);
199extern int pthread_attr_setscope(pthread_attr_t *attr, int contentionScope);
200
201extern int pthread_attr_getschedparam(const pthread_attr_t *attr,
202	struct sched_param *param);
203extern int pthread_attr_setschedparam(pthread_attr_t *attr,
204	const struct sched_param *param);
205
206extern int pthread_attr_getguardsize(const pthread_attr_t *attr,
207	size_t *guardsize);
208extern int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
209
210#if 0   /* Unimplemented attribute functions: */
211
212/* [TPS] */
213extern int pthread_attr_getinheritsched(const pthread_attr_t *attr,
214	int *inheritsched);
215extern int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
216
217extern int pthread_attr_getschedpolicy(const pthread_attr_t *attr,
218	int *policy);
219extern int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
220
221/* [TSA] */
222extern int pthread_attr_getstackaddr(const pthread_attr_t *attr,
223	void **stackaddr);
224extern int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
225
226/* [TSA TSS] */
227extern int pthread_attr_getstack(const pthread_attr_t *attr,
228	void **stackaddr, size_t *stacksize);
229extern int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
230
231#endif	/* 0 */
232
233
234/* thread functions */
235extern int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
236	void *(*start_routine)(void*), void *arg);
237extern int pthread_detach(pthread_t thread);
238extern int pthread_equal(pthread_t t1, pthread_t t2);
239extern void pthread_exit(void *value_ptr);
240extern int pthread_join(pthread_t thread, void **_value);
241extern pthread_t pthread_self(void);
242extern int pthread_getconcurrency(void);
243extern int pthread_setconcurrency(int newLevel);
244
245extern int pthread_cancel(pthread_t thread);
246extern int pthread_setcancelstate(int state, int *_oldState);
247extern int pthread_setcanceltype(int type, int *_oldType);
248extern void pthread_testcancel(void);
249
250extern int pthread_getschedparam(pthread_t thread, int *policy,
251	struct sched_param *param);
252extern int pthread_setschedparam(pthread_t thread, int policy,
253	const struct sched_param *param);
254
255/* thread specific data functions */
256extern int pthread_key_create(pthread_key_t *key,
257	void (*destructorFunc)(void*));
258extern int pthread_key_delete(pthread_key_t key);
259extern void *pthread_getspecific(pthread_key_t key);
260extern int pthread_setspecific(pthread_key_t key, const void *value);
261
262/* private functions */
263extern void __pthread_cleanup_push_handler(
264	struct __pthread_cleanup_handler *handler);
265extern struct __pthread_cleanup_handler *__pthread_cleanup_pop_handler(void);
266
267#ifdef __cplusplus
268}
269#endif
270
271#endif	/* _PTHREAD_ */
272