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