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