xref: /haiku/headers/posix/pthread.h (revision 52f7c9389475e19fc21487b38064b4390eeb6fea)
1 /*
2  * Copyright 2001-2011 Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT 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 */
53 struct __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
74 extern "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 #define PTHREAD_RWLOCK_INITIALIZER	\
84 	{ 0, -1, {{0}} }
85 
86 /* mutex functions */
87 extern int pthread_mutex_destroy(pthread_mutex_t *mutex);
88 extern int pthread_mutex_getprioceiling(const pthread_mutex_t *mutex,
89 	int *_priorityCeiling);
90 extern int pthread_mutex_init(pthread_mutex_t *mutex,
91 	const pthread_mutexattr_t *attr);
92 extern int pthread_mutex_lock(pthread_mutex_t *mutex);
93 extern int pthread_mutex_setprioceiling(pthread_mutex_t *mutex,
94 	int newPriorityCeiling, int *_oldPriorityCeiling);
95 extern int pthread_mutex_clocklock(pthread_mutex_t *mutex,
96 	clockid_t clock_id, const struct timespec *abstime);
97 extern int pthread_mutex_timedlock(pthread_mutex_t *mutex,
98 	const struct timespec *abstime);
99 extern int pthread_mutex_trylock(pthread_mutex_t *mutex);
100 extern int pthread_mutex_unlock(pthread_mutex_t *mutex);
101 
102 /* mutex attribute functions */
103 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *mutexAttr);
104 extern int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *mutexAttr,
105 	int *_priorityCeiling);
106 extern int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *mutexAttr,
107 	int *_protocol);
108 extern int pthread_mutexattr_getpshared(const pthread_mutexattr_t *mutexAttr,
109 	int *_processShared);
110 extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *mutexAttr,
111 	int *_type);
112 extern int pthread_mutexattr_init(pthread_mutexattr_t *mutexAttr);
113 extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mutexAttr,
114 	int priorityCeiling);
115 extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *mutexAttr,
116 	int protocol);
117 extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *mutexAttr,
118 	int processShared);
119 extern int pthread_mutexattr_settype(pthread_mutexattr_t *mutexAttr, int type);
120 
121 /* barrier functions */
122 extern int pthread_barrier_init(pthread_barrier_t *barrier,
123 	const pthread_barrierattr_t *attr, unsigned count);
124 extern int pthread_barrier_destroy(pthread_barrier_t *barrier);
125 extern int pthread_barrier_wait(pthread_barrier_t *barrier);
126 
127 /* barrier attribute functions */
128 extern int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
129 extern int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
130 	int *shared);
131 extern int pthread_barrierattr_init(pthread_barrierattr_t *attr);
132 extern int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr,
133 	int shared);
134 
135 /* condition variable functions */
136 extern int pthread_cond_destroy(pthread_cond_t *cond);
137 extern int pthread_cond_init(pthread_cond_t *cond,
138 	const pthread_condattr_t *attr);
139 extern int pthread_cond_broadcast(pthread_cond_t *cond);
140 extern int pthread_cond_signal(pthread_cond_t *cond);
141 extern int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
142 	clockid_t clock_id, const struct timespec *abstime);
143 extern int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
144 	const struct timespec *abstime);
145 extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
146 
147 /* condition variable attribute functions */
148 extern int pthread_condattr_destroy(pthread_condattr_t *condAttr);
149 extern int pthread_condattr_init(pthread_condattr_t *condAttr);
150 extern int pthread_condattr_getpshared(const pthread_condattr_t *condAttr,
151 	int *processShared);
152 extern int pthread_condattr_setpshared(pthread_condattr_t *condAttr,
153 	int processShared);
154 extern int pthread_condattr_getclock(const pthread_condattr_t *condAttr,
155 	clockid_t *clockID);
156 extern int pthread_condattr_setclock(pthread_condattr_t *condAttr,
157 	clockid_t clockID);
158 
159 /* rwlock functions */
160 extern int pthread_rwlock_init(pthread_rwlock_t *lock,
161 	const pthread_rwlockattr_t *attr);
162 extern int pthread_rwlock_destroy(pthread_rwlock_t *lock);
163 extern int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
164 extern int pthread_rwlock_tryrdlock(pthread_rwlock_t *lock);
165 extern int pthread_rwlock_clockrdlock(pthread_rwlock_t* rwlock,
166 	clockid_t clock_id, const struct timespec* abstime);
167 extern int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
168 	const struct timespec *abstime);
169 extern int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
170 extern int pthread_rwlock_trywrlock(pthread_rwlock_t *lock);
171 extern int pthread_rwlock_clockwrlock(pthread_rwlock_t* rwlock,
172 	clockid_t clock_id, const struct timespec* abstime);
173 extern int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
174 	const struct timespec *abstime);
175 extern int pthread_rwlock_unlock(pthread_rwlock_t *lock);
176 
177 /* rwlock attribute functions */
178 extern int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
179 extern int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
180 extern int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr,
181 	int *shared);
182 extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr,
183 	int shared);
184 
185 /* spinlock functions */
186 extern int pthread_spin_init(pthread_spinlock_t* spinlock, int pshared);
187 extern int pthread_spin_destroy(pthread_spinlock_t* spinlock);
188 extern int pthread_spin_lock(pthread_spinlock_t* spinlock);
189 extern int pthread_spin_trylock(pthread_spinlock_t* spinlock);
190 extern int pthread_spin_unlock(pthread_spinlock_t* spinlock);
191 
192 /* misc. functions */
193 extern int pthread_atfork(void (*prepare)(void), void (*parent)(void),
194 	void (*child)(void));
195 extern int pthread_once(pthread_once_t *once_control,
196 	void (*init_routine)(void));
197 
198 /* thread attributes functions */
199 extern int pthread_attr_destroy(pthread_attr_t *attr);
200 extern int pthread_attr_init(pthread_attr_t *attr);
201 extern int pthread_attr_getdetachstate(const pthread_attr_t *attr,
202 	int *detachstate);
203 extern int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
204 extern int pthread_attr_getstacksize(const pthread_attr_t *attr,
205 	size_t *stacksize);
206 extern int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
207 extern int pthread_attr_getscope(const pthread_attr_t *attr,
208 	int *contentionScope);
209 extern int pthread_attr_setscope(pthread_attr_t *attr, int contentionScope);
210 
211 extern int pthread_attr_getschedparam(const pthread_attr_t *attr,
212 	struct sched_param *param);
213 extern int pthread_attr_setschedparam(pthread_attr_t *attr,
214 	const struct sched_param *param);
215 
216 extern int pthread_attr_getguardsize(const pthread_attr_t *attr,
217 	size_t *guardsize);
218 extern int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
219 
220 extern int pthread_attr_getstack(const pthread_attr_t *attr,
221 	void **stackaddr, size_t *stacksize);
222 extern int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
223 	size_t stacksize);
224 
225 #if 0   /* Unimplemented attribute functions: */
226 
227 /* [TPS] */
228 extern int pthread_attr_getinheritsched(const pthread_attr_t *attr,
229 	int *inheritsched);
230 extern int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
231 
232 extern int pthread_attr_getschedpolicy(const pthread_attr_t *attr,
233 	int *policy);
234 extern int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
235 
236 #endif	/* 0 */
237 
238 /* thread functions */
239 extern int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
240 	void *(*start_routine)(void*), void *arg);
241 extern int pthread_detach(pthread_t thread);
242 extern int pthread_equal(pthread_t t1, pthread_t t2);
243 extern void pthread_exit(void *value_ptr) __attribute__ ((noreturn));
244 extern int pthread_join(pthread_t thread, void **_value);
245 extern pthread_t pthread_self(void);
246 extern int pthread_getconcurrency(void);
247 extern int pthread_setconcurrency(int newLevel);
248 
249 extern int pthread_cancel(pthread_t thread);
250 extern int pthread_setcancelstate(int state, int *_oldState);
251 extern int pthread_setcanceltype(int type, int *_oldType);
252 extern void pthread_testcancel(void);
253 
254 extern int pthread_getschedparam(pthread_t thread, int *policy,
255 	struct sched_param *param);
256 extern int pthread_setschedparam(pthread_t thread, int policy,
257 	const struct sched_param *param);
258 
259 /* thread specific data functions */
260 extern int pthread_key_create(pthread_key_t *key,
261 	void (*destructorFunc)(void*));
262 extern int pthread_key_delete(pthread_key_t key);
263 extern void *pthread_getspecific(pthread_key_t key);
264 extern int pthread_setspecific(pthread_key_t key, const void *value);
265 
266 /* private functions */
267 extern void __pthread_cleanup_push_handler(
268 	struct __pthread_cleanup_handler *handler);
269 extern struct __pthread_cleanup_handler *__pthread_cleanup_pop_handler(void);
270 
271 #ifdef __cplusplus
272 }
273 #endif
274 
275 #endif	/* _PTHREAD_ */
276