1 /*
2  *  Written by Joel Sherrill <joel.sherrill@OARcorp.com>.
3  *
4  *  COPYRIGHT (c) 1989-2013, 2015.
5  *  On-Line Applications Research Corporation (OAR).
6  *
7  *  Permission to use, copy, modify, and distribute this software for any
8  *  purpose without fee is hereby granted, provided that this entire notice
9  *  is included in all copies of any software which is or includes a copy
10  *  or modification of this software.
11  *
12  *  THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
13  *  WARRANTY.  IN PARTICULAR,  THE AUTHOR MAKES NO REPRESENTATION
14  *  OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
15  *  SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
16  */
17 
18 #ifndef __PTHREAD_h
19 #define __PTHREAD_h
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #include <unistd.h>
26 
27 #if defined(_POSIX_THREADS)
28 
29 #include <sys/types.h>
30 #include <time.h>
31 #include <sched.h>
32 #include <sys/cdefs.h>
33 
34 struct _pthread_cleanup_context {
35   void (*_routine)(void *);
36   void *_arg;
37   int _canceltype;
38   struct _pthread_cleanup_context *_previous;
39 };
40 
41 /* Register Fork Handlers */
42 int	pthread_atfork (void (*prepare)(void), void (*parent)(void),
43                    void (*child)(void));
44 
45 /* Mutex Initialization Attributes, P1003.1c/Draft 10, p. 81 */
46 
47 int	pthread_mutexattr_init (pthread_mutexattr_t *__attr);
48 int	pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
49 int	pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
50 				      int  *__pshared);
51 int	pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
52 				      int __pshared);
53 
54 #if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
55 
56 /* Single UNIX Specification 2 Mutex Attributes types */
57 
58 int pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr, int *__kind);
59 int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind);
60 
61 #endif
62 
63 /* Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87 */
64 
65 int	pthread_mutex_init (pthread_mutex_t *__mutex,
66 			    const pthread_mutexattr_t *__attr);
67 int	pthread_mutex_destroy (pthread_mutex_t *__mutex);
68 
69 /* This is used to statically initialize a pthread_mutex_t. Example:
70 
71     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
72  */
73 
74 #define PTHREAD_MUTEX_INITIALIZER _PTHREAD_MUTEX_INITIALIZER
75 
76 /*  Locking and Unlocking a Mutex, P1003.1c/Draft 10, p. 93
77     NOTE: P1003.4b/D8 adds pthread_mutex_timedlock(), p. 29 */
78 
79 int	pthread_mutex_lock (pthread_mutex_t *__mutex);
80 int	pthread_mutex_trylock (pthread_mutex_t *__mutex);
81 int	pthread_mutex_unlock (pthread_mutex_t *__mutex);
82 
83 #if defined(_POSIX_TIMEOUTS)
84 
85 int	pthread_mutex_timedlock (pthread_mutex_t *__mutex,
86 				 const struct timespec *__timeout);
87 
88 #endif /* _POSIX_TIMEOUTS */
89 
90 /* Condition Variable Initialization Attributes, P1003.1c/Draft 10, p. 96 */
91 
92 int	pthread_condattr_init (pthread_condattr_t *__attr);
93 int	pthread_condattr_destroy (pthread_condattr_t *__attr);
94 
95 int	pthread_condattr_getclock (const pthread_condattr_t *__restrict __attr,
96 				   clockid_t *__restrict __clock_id);
97 int	pthread_condattr_setclock (pthread_condattr_t *__attr,
98 				   clockid_t __clock_id);
99 
100 int	pthread_condattr_getpshared (const pthread_condattr_t *__attr,
101 				     int *__pshared);
102 int	pthread_condattr_setpshared (pthread_condattr_t *__attr, int __pshared);
103 
104 /* Initializing and Destroying a Condition Variable, P1003.1c/Draft 10, p. 87 */
105 
106 int	pthread_cond_init (pthread_cond_t *__cond,
107 			   const pthread_condattr_t *__attr);
108 int	pthread_cond_destroy (pthread_cond_t *__mutex);
109 
110 /* This is used to statically initialize a pthread_cond_t. Example:
111 
112     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
113  */
114 
115 #define PTHREAD_COND_INITIALIZER _PTHREAD_COND_INITIALIZER
116 
117 /* Broadcasting and Signaling a Condition, P1003.1c/Draft 10, p. 101 */
118 
119 int	pthread_cond_signal (pthread_cond_t *__cond);
120 int	pthread_cond_broadcast (pthread_cond_t *__cond);
121 
122 /* Waiting on a Condition, P1003.1c/Draft 10, p. 105 */
123 
124 int	pthread_cond_wait (pthread_cond_t *__cond, pthread_mutex_t *__mutex);
125 
126 int	pthread_cond_timedwait (pthread_cond_t *__cond,
127 				pthread_mutex_t *__mutex,
128 				const struct timespec *__abstime);
129 
130 #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
131 
132 /* Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 */
133 
134 int	pthread_attr_setscope (pthread_attr_t *__attr, int __contentionscope);
135 int	pthread_attr_getscope (const pthread_attr_t *__attr,
136 			       int *__contentionscope);
137 int	pthread_attr_setinheritsched (pthread_attr_t *__attr,
138 				      int __inheritsched);
139 int	pthread_attr_getinheritsched (const pthread_attr_t *__attr,
140 				      int *__inheritsched);
141 int	pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy);
142 int	pthread_attr_getschedpolicy (const pthread_attr_t *__attr,
143 				     int *__policy);
144 
145 #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
146 
147 int	pthread_attr_setschedparam (pthread_attr_t *__attr,
148 				    const struct sched_param *__param);
149 int	pthread_attr_getschedparam (const pthread_attr_t *__attr,
150 				    struct sched_param *__param);
151 
152 #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
153 
154 /* Dynamic Thread Scheduling Parameters Access, P1003.1c/Draft 10, p. 124 */
155 
156 int	pthread_getschedparam (pthread_t __pthread, int *__policy,
157 			       struct sched_param *__param);
158 int	pthread_setschedparam (pthread_t __pthread, int __policy,
159 			       const struct sched_param *__param);
160 
161 /* Set Scheduling Priority of a Thread */
162 int	pthread_setschedprio (pthread_t thread, int prio);
163 
164 #endif /* defined(_POSIX_THREAD_PRIORITY_SCHEDULING) */
165 
166 #if __GNU_VISIBLE
167 int	pthread_getname_np(pthread_t, char *, size_t) __nonnull((2));
168 
169 int	pthread_setname_np(pthread_t, const char *) __nonnull((2));
170 #endif
171 
172 #if defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)
173 
174 /* Mutex Initialization Scheduling Attributes, P1003.1c/Draft 10, p. 128 */
175 
176 int	pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr,
177 				       int __protocol);
178 int	pthread_mutexattr_getprotocol (const pthread_mutexattr_t *__attr,
179 				       int *__protocol);
180 int	pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr,
181 					  int __prioceiling);
182 int	pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *__attr,
183 					  int *__prioceiling);
184 
185 #endif /* _POSIX_THREAD_PRIO_INHERIT || _POSIX_THREAD_PRIO_PROTECT */
186 
187 #if defined(_POSIX_THREAD_PRIO_PROTECT)
188 
189 /* Change the Priority Ceiling of a Mutex, P1003.1c/Draft 10, p. 131 */
190 
191 int	pthread_mutex_setprioceiling (pthread_mutex_t *__mutex,
192 				      int __prioceiling, int *__old_ceiling);
193 int	pthread_mutex_getprioceiling (const pthread_mutex_t *__restrict __mutex,
194 				      int *__prioceiling);
195 
196 #endif /* _POSIX_THREAD_PRIO_PROTECT */
197 
198 /* Thread Creation Attributes, P1003.1c/Draft 10, p, 140 */
199 
200 int	pthread_attr_init (pthread_attr_t *__attr);
201 int	pthread_attr_destroy (pthread_attr_t *__attr);
202 int	pthread_attr_setstack (pthread_attr_t *attr,
203 	void *__stackaddr, size_t __stacksize);
204 int	pthread_attr_getstack (const pthread_attr_t *attr,
205 	void **__stackaddr, size_t *__stacksize);
206 int	pthread_attr_getstacksize (const pthread_attr_t *__attr,
207 				   size_t *__stacksize);
208 int	pthread_attr_setstacksize (pthread_attr_t *__attr, size_t __stacksize);
209 int	pthread_attr_getstackaddr (const pthread_attr_t *__attr,
210 				   void **__stackaddr);
211 int	pthread_attr_setstackaddr (pthread_attr_t  *__attr, void *__stackaddr);
212 int	pthread_attr_getdetachstate (const pthread_attr_t *__attr,
213 				     int *__detachstate);
214 int	pthread_attr_setdetachstate (pthread_attr_t *__attr, int __detachstate);
215 int	pthread_attr_getguardsize (const pthread_attr_t *__attr,
216 				   size_t *__guardsize);
217 int	pthread_attr_setguardsize (pthread_attr_t *__attr, size_t __guardsize);
218 
219 /* POSIX thread APIs beyond the POSIX standard but provided
220  * in GNU/Linux. They may be provided by other OSes for
221  * compatibility.
222  */
223 #if __GNU_VISIBLE
224 #if defined(__rtems__)
225 int	pthread_attr_setaffinity_np (pthread_attr_t *__attr,
226 				     size_t __cpusetsize,
227 				     const cpu_set_t *__cpuset);
228 int 	pthread_attr_getaffinity_np (const pthread_attr_t *__attr,
229 				     size_t __cpusetsize, cpu_set_t *__cpuset);
230 
231 int	pthread_setaffinity_np (pthread_t __id, size_t __cpusetsize,
232 				const cpu_set_t *__cpuset);
233 int	pthread_getaffinity_np (const pthread_t __id, size_t __cpusetsize,
234 				cpu_set_t *__cpuset);
235 
236 int	pthread_getattr_np (pthread_t __id, pthread_attr_t *__attr);
237 #endif /* defined(__rtems__) */
238 #endif /* __GNU_VISIBLE */
239 
240 /* Thread Creation, P1003.1c/Draft 10, p. 144 */
241 
242 int	pthread_create (pthread_t *__pthread, const pthread_attr_t  *__attr,
243 			void *(*__start_routine)(void *), void *__arg);
244 
245 /* Wait for Thread Termination, P1003.1c/Draft 10, p. 147 */
246 
247 int	pthread_join (pthread_t __pthread, void **__value_ptr);
248 
249 /* Detaching a Thread, P1003.1c/Draft 10, p. 149 */
250 
251 int	pthread_detach (pthread_t __pthread);
252 
253 /* Thread Termination, p1003.1c/Draft 10, p. 150 */
254 
255 void	pthread_exit (void *__value_ptr) __dead2;
256 
257 /* Get Calling Thread's ID, p1003.1c/Draft 10, p. XXX */
258 
259 pthread_t	pthread_self (void);
260 
261 /* Compare Thread IDs, p1003.1c/Draft 10, p. 153 */
262 
263 int	pthread_equal (pthread_t __t1, pthread_t __t2);
264 
265 /* Retrieve ID of a Thread's CPU Time Clock */
266 int	pthread_getcpuclockid (pthread_t thread, clockid_t *clock_id);
267 
268 /* Get/Set Current Thread's Concurrency Level */
269 int	pthread_setconcurrency (int new_level);
270 int	pthread_getconcurrency (void);
271 
272 #if __BSD_VISIBLE || __GNU_VISIBLE
273 void	pthread_yield (void);
274 #endif
275 
276 /* Dynamic Package Initialization */
277 
278 /* This is used to statically initialize a pthread_once_t. Example:
279 
280     pthread_once_t once = PTHREAD_ONCE_INIT;
281 
282     NOTE:  This is named inconsistently -- it should be INITIALIZER.  */
283 
284 #define PTHREAD_ONCE_INIT _PTHREAD_ONCE_INIT
285 
286 int	pthread_once (pthread_once_t *__once_control,
287 		      void (*__init_routine)(void));
288 
289 /* Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163 */
290 
291 int	pthread_key_create (pthread_key_t *__key,
292 			    void (*__destructor)(void *));
293 
294 /* Thread-Specific Data Management, P1003.1c/Draft 10, p. 165 */
295 
296 int	pthread_setspecific (pthread_key_t __key, const void *__value);
297 void *	pthread_getspecific (pthread_key_t __key);
298 
299 /* Thread-Specific Data Key Deletion, P1003.1c/Draft 10, p. 167 */
300 
301 int	pthread_key_delete (pthread_key_t __key);
302 
303 /* Execution of a Thread, P1003.1c/Draft 10, p. 181 */
304 
305 #define PTHREAD_CANCEL_ENABLE  0
306 #define PTHREAD_CANCEL_DISABLE 1
307 
308 #define PTHREAD_CANCEL_DEFERRED 0
309 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
310 
311 #define PTHREAD_CANCELED ((void *) -1)
312 
313 int	pthread_cancel (pthread_t __pthread);
314 
315 /* Setting Cancelability State, P1003.1c/Draft 10, p. 183 */
316 
317 int	pthread_setcancelstate (int __state, int *__oldstate);
318 int	pthread_setcanceltype (int __type, int *__oldtype);
319 void 	pthread_testcancel (void);
320 
321 /* Establishing Cancellation Handlers, P1003.1c/Draft 10, p. 184 */
322 
323 void	_pthread_cleanup_push (struct _pthread_cleanup_context *_context,
324 			       void (*_routine)(void *), void *_arg);
325 
326 void	_pthread_cleanup_pop (struct _pthread_cleanup_context *_context,
327 			      int _execute);
328 
329 /* It is intentional to open and close the scope in two different macros */
330 #define pthread_cleanup_push(_routine, _arg) \
331   do { \
332     struct _pthread_cleanup_context _pthread_clup_ctx; \
333     _pthread_cleanup_push(&_pthread_clup_ctx, (_routine), (_arg))
334 
335 #define pthread_cleanup_pop(_execute) \
336     _pthread_cleanup_pop(&_pthread_clup_ctx, (_execute)); \
337   } while (0)
338 
339 #if __GNU_VISIBLE
340 void	_pthread_cleanup_push_defer (struct _pthread_cleanup_context *_context,
341 				     void (*_routine)(void *), void *_arg);
342 
343 void	_pthread_cleanup_pop_restore (struct _pthread_cleanup_context *_context,
344 				      int _execute);
345 
346 /* It is intentional to open and close the scope in two different macros */
347 #define pthread_cleanup_push_defer_np(_routine, _arg) \
348   do { \
349     struct _pthread_cleanup_context _pthread_clup_ctx; \
350     _pthread_cleanup_push_defer(&_pthread_clup_ctx, (_routine), (_arg))
351 
352 #define pthread_cleanup_pop_restore_np(_execute) \
353     _pthread_cleanup_pop_restore(&_pthread_clup_ctx, (_execute)); \
354   } while (0)
355 #endif /* __GNU_VISIBLE */
356 
357 #if defined(_POSIX_THREAD_CPUTIME)
358 
359 /* Accessing a Thread CPU-time Clock, P1003.4b/D8, p. 58 */
360 
361 int	pthread_getcpuclockid (pthread_t __pthread_id, clockid_t *__clock_id);
362 
363 #endif /* defined(_POSIX_THREAD_CPUTIME) */
364 
365 
366 #endif /* defined(_POSIX_THREADS) */
367 
368 #if defined(_POSIX_BARRIERS)
369 
370 int	pthread_barrierattr_init (pthread_barrierattr_t *__attr);
371 int	pthread_barrierattr_destroy (pthread_barrierattr_t *__attr);
372 int	pthread_barrierattr_getpshared (const pthread_barrierattr_t *__attr,
373 					int *__pshared);
374 int	pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr,
375 					int __pshared);
376 
377 #define PTHREAD_BARRIER_SERIAL_THREAD -1
378 
379 int	pthread_barrier_init (pthread_barrier_t *__barrier,
380 			      const pthread_barrierattr_t *__attr,
381 			      unsigned __count);
382 int	pthread_barrier_destroy (pthread_barrier_t *__barrier);
383 int	pthread_barrier_wait (pthread_barrier_t *__barrier);
384 
385 #endif /* defined(_POSIX_BARRIERS) */
386 
387 #if defined(_POSIX_SPIN_LOCKS)
388 
389 int	pthread_spin_init (pthread_spinlock_t *__spinlock, int __pshared);
390 int	pthread_spin_destroy (pthread_spinlock_t *__spinlock);
391 int	pthread_spin_lock (pthread_spinlock_t *__spinlock);
392 int	pthread_spin_trylock (pthread_spinlock_t *__spinlock);
393 int	pthread_spin_unlock (pthread_spinlock_t *__spinlock);
394 
395 #endif /* defined(_POSIX_SPIN_LOCKS) */
396 
397 #if defined(_POSIX_READER_WRITER_LOCKS)
398 
399 /* This is used to statically initialize a pthread_rwlock_t. Example:
400 
401     pthread_mutex_t mutex = PTHREAD_RWLOCK_INITIALIZER;
402  */
403 
404 #define PTHREAD_RWLOCK_INITIALIZER _PTHREAD_RWLOCK_INITIALIZER
405 
406 int	pthread_rwlockattr_init (pthread_rwlockattr_t *__attr);
407 int	pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
408 int	pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *__attr,
409 				       int *__pshared);
410 int	pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr,
411 				       int __pshared);
412 
413 int	pthread_rwlock_init (pthread_rwlock_t *__rwlock,
414 			     const pthread_rwlockattr_t *__attr);
415 int	pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
416 int	pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
417 int	pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
418 int	pthread_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
419 				    const struct timespec *__abstime);
420 int	pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
421 int	pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
422 int	pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
423 int	pthread_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
424 				    const struct timespec *__abstime);
425 
426 #endif /* defined(_POSIX_READER_WRITER_LOCKS) */
427 
428 
429 #ifdef __cplusplus
430 }
431 #endif
432 
433 #endif
434 /* end of include file */
435