xref: /haiku/headers/posix/pthread.h (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2 ** Distributed under the terms of the Haiku License.
3 */
4 #ifndef _PTHREAD_H_
5 #define _PTHREAD_H_
6 
7 #include <time.h>
8 #include <OS.h>
9 
10 typedef thread_id 					pthread_t;
11 typedef struct  _pthread_attr		*pthread_attr_t;
12 typedef struct  _pthread_mutex		*pthread_mutex_t;
13 typedef struct  _pthread_mutexattr	*pthread_mutexattr_t;
14 typedef struct  _pthread_cond		*pthread_cond_t;
15 typedef struct  _pthread_condattr	*pthread_condattr_t;
16 typedef int							pthread_key_t;
17 typedef struct  _pthread_once		pthread_once_t;
18 typedef struct  _pthread_rwlock		*pthread_rwlock_t;
19 typedef struct  _pthread_rwlockattr	*pthread_rwlockattr_t;
20 typedef struct  _pthread_barrier		*pthread_barrier_t;
21 typedef struct  _pthread_barrierattr	*pthread_barrierattr_t;
22 typedef struct  _pthread_spinlock	*pthread_spinlock_t;
23 
24 struct _pthread_once {
25 	int	state;
26 	pthread_mutex_t mutex;
27 };
28 
29 
30 enum pthread_mutex_type {
31 	PTHREAD_MUTEX_DEFAULT,
32 	PTHREAD_MUTEX_NORMAL,
33 	PTHREAD_MUTEX_ERRORCHECK,
34 	PTHREAD_MUTEX_RECURSIVE,
35 };
36 
37 enum pthread_process_shared {
38 	PTHREAD_PROCESS_PRIVATE,
39 	PTHREAD_PROCESS_SHARED,
40 };
41 
42 /*
43  * Flags for threads and thread attributes.
44  */
45 #define PTHREAD_DETACHED			0x1
46 #define PTHREAD_SCOPE_SYSTEM		0x2
47 #define PTHREAD_INHERIT_SCHED		0x4
48 #define PTHREAD_NOFLOAT				0x8
49 
50 #define PTHREAD_CREATE_DETACHED		PTHREAD_DETACHED
51 #define PTHREAD_CREATE_JOINABLE		0
52 #define PTHREAD_SCOPE_PROCESS		0
53 #define PTHREAD_EXPLICIT_SCHED		0
54 
55 /*
56  * Flags for cancelling threads
57  */
58 #define PTHREAD_CANCEL_ENABLE		0
59 #define PTHREAD_CANCEL_DISABLE		1
60 #define PTHREAD_CANCEL_DEFERRED		0
61 #define PTHREAD_CANCEL_ASYNCHRONOUS	2
62 #define PTHREAD_CANCELED		((void *) 1)
63 
64 #define PTHREAD_NEEDS_INIT	0
65 #define PTHREAD_DONE_INIT	1
66 #define PTHREAD_ONCE_INIT 	{ PTHREAD_NEEDS_INIT, NULL }
67 
68 #define PTHREAD_BARRIER_SERIAL_THREAD	-1
69 #define PTHREAD_PRIO_NONE			0
70 #define PTHREAD_PRIO_INHERIT		1
71 #define PTHREAD_PRIO_PROTECT		2
72 
73 //extern pthread_mutexattr_t pthread_mutexattr_default;
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 extern pthread_mutex_t _pthread_mutex_static_initializer(void);
80 extern pthread_mutex_t _pthread_recursive_mutex_static_initializer(void);
81 #define PTHREAD_MUTEX_INITIALIZER NULL
82 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER \
83 	pthread_recursive_mutex_static_initializer();
84 
85 extern pthread_cond_t _pthread_cond_static_initializer(void);
86 #define PTHREAD_COND_INITIALIZER _pthread_cond_static_initializer();
87 
88 /* mutex functions */
89 extern int pthread_mutex_destroy(pthread_mutex_t *mutex);
90 extern int pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *_priorityCeiling);
91 extern int pthread_mutex_init(pthread_mutex_t *mutex, 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, int newPriorityCeiling,
94 				int *_oldPriorityCeiling);
95 extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *spec);
96 extern int pthread_mutex_trylock(pthread_mutex_t *mutex);
97 extern int pthread_mutex_unlock(pthread_mutex_t *mutex);
98 
99 /* mutex attribute functions */
100 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *mutexAttr);
101 extern int pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mutexAttr, int *_priorityCeiling);
102 extern int pthread_mutexattr_getprotocol(pthread_mutexattr_t *mutexAttr, int *_protocol);
103 extern int pthread_mutexattr_getpshared(pthread_mutexattr_t *mutexAttr, int *_processShared);
104 extern int pthread_mutexattr_gettype(pthread_mutexattr_t *mutexAttr, int *_type);
105 extern int pthread_mutexattr_init(pthread_mutexattr_t *mutexAttr);
106 extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mutexAttr, int priorityCeiling);
107 extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *mutexAttr, int protocol);
108 extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *mutexAttr, int processShared);
109 extern int pthread_mutexattr_settype(pthread_mutexattr_t *mutexAttr, int type);
110 
111 /* condition variable functions */
112 extern int pthread_cond_destroy(pthread_cond_t *cond);
113 extern int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
114 extern int pthread_cond_broadcast(pthread_cond_t *cond);
115 extern int pthread_cond_signal(pthread_cond_t *cond);
116 extern int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
117 	const struct timespec *abstime);
118 extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
119 
120 /* condition variable attribute functions */
121 extern int pthread_condattr_destroy(pthread_condattr_t *condAttr);
122 extern int pthread_condattr_init(pthread_condattr_t *condAttr);
123 extern int pthread_condattr_getpshared(const pthread_condattr_t *condAttr, int *processShared);
124 extern int pthread_condattr_setpshared(pthread_condattr_t *condAttr, int processShared);
125 
126 /* misc. functions */
127 extern int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
128 extern int pthread_once(pthread_once_t *once_control, void (*init_routine)());
129 
130 /* thread attributes functions */
131 extern int pthread_attr_destroy(pthread_attr_t *attr);
132 extern int pthread_attr_init(pthread_attr_t *attr);
133 extern int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
134 extern int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
135 
136 extern int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
137 	void *(*start_routine)(void*), void *arg);
138 extern int pthread_detach(pthread_t thread);
139 extern int pthread_equal(pthread_t t1, pthread_t t2);
140 extern void pthread_exit(void *value_ptr);
141 extern int pthread_join(pthread_t thread, void **value_ptr);
142 extern pthread_t pthread_self(void);
143 
144 extern int pthread_kill(pthread_t thread, int sig);
145 
146 /* thread specific data functions */
147 extern int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
148 extern int pthread_key_delete(pthread_key_t key);
149 extern void *pthread_getspecific(pthread_key_t key);
150 extern int pthread_setspecific(pthread_key_t key, const void *value);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif	/* _PTHREAD_ */
157