xref: /titanic_51/usr/src/head/pthread.h (revision db2bae3047e71d795bde12e3baa621f4b6cc8930)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _PTHREAD_H
28 #define	_PTHREAD_H
29 
30 #include <sys/feature_tests.h>
31 
32 #ifndef	_ASM
33 #include <sys/types.h>
34 #include <time.h>
35 #include <sched.h>
36 #endif	/* _ASM */
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * Thread related attribute values defined as in thread.h.
44  * These are defined as bit pattern in thread.h.
45  * Any change here should be reflected in thread.h.
46  */
47 /* detach */
48 #define	PTHREAD_CREATE_DETACHED		0x40	/* = THR_DETACHED */
49 #define	PTHREAD_CREATE_JOINABLE		0
50 /* scope */
51 #define	PTHREAD_SCOPE_SYSTEM		0x01	/* = THR_BOUND */
52 #define	PTHREAD_SCOPE_PROCESS		0
53 
54 /*
55  * Other attributes which are not defined in thread.h
56  */
57 /* inherit */
58 #define	PTHREAD_INHERIT_SCHED		1
59 #define	PTHREAD_EXPLICIT_SCHED		0
60 
61 /*
62  * Value of process-shared attribute
63  * These are defined as values defined in sys/synch.h
64  * Any change here should be reflected in sys/synch.h.
65  */
66 #define	PTHREAD_PROCESS_SHARED		1	/* = USYNC_PROCESS */
67 #define	PTHREAD_PROCESS_PRIVATE		0	/* = USYNC_THREAD */
68 
69 #define	_DEFAULT_TYPE 			PTHREAD_PROCESS_PRIVATE
70 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
71 #define	DEFAULT_TYPE			_DEFAULT_TYPE
72 #endif
73 
74 /*
75  * mutex types
76  * keep these in synch which sys/synch.h lock flags
77  */
78 #define	PTHREAD_MUTEX_NORMAL		0x0
79 #define	PTHREAD_MUTEX_ERRORCHECK	0x2
80 #define	PTHREAD_MUTEX_RECURSIVE		0x4
81 #define	PTHREAD_MUTEX_DEFAULT		PTHREAD_MUTEX_NORMAL
82 
83 /*
84  * Mutex protocol values. Keep these in synch with sys/synch.h lock types.
85  */
86 #define	PTHREAD_PRIO_NONE		0x0
87 #define	PTHREAD_PRIO_INHERIT		0x10
88 #define	PTHREAD_PRIO_PROTECT		0x20
89 
90 /*
91  * Mutex robustness attribute values. The robustness attribute is a
92  * Solaris specific extension to support robust mutexes. Note the _NP suffix
93  * to indicate these are not part of the current POSIX spec (POSIX 1003.1 1996),
94  * but are platform specific non-portable extensions. Keep these in synch
95  * with sys/synch.h lock types.
96  */
97 #define	PTHREAD_MUTEX_STALL_NP		0x0
98 #define	PTHREAD_MUTEX_ROBUST_NP		0x40
99 
100 /*
101  * macros - default initializers defined as in synch.h
102  * Any change here should be reflected in synch.h.
103  *
104  * NOTE:
105  * Make sure that any change in the macros is consistent with the definition
106  * of the corresponding types in sys/types.h (e.g. PTHREAD_MUTEX_INITIALIZER
107  * should be consistent with the definition for pthread_mutex_t).
108  */
109 #define	PTHREAD_MUTEX_INITIALIZER		/* = DEFAULTMUTEX */	\
110 	{{0, 0, 0, _DEFAULT_TYPE, _MUTEX_MAGIC}, {{{0}}}, 0}
111 
112 #define	PTHREAD_COND_INITIALIZER		/* = DEFAULTCV */	\
113 	{{{0, 0, 0, 0}, _DEFAULT_TYPE, _COND_MAGIC}, 0}
114 
115 #define	PTHREAD_RWLOCK_INITIALIZER		/* = DEFAULTRWLOCK */	\
116 	{0, _DEFAULT_TYPE, _RWL_MAGIC, PTHREAD_MUTEX_INITIALIZER,	\
117 	PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER}
118 
119 /* cancellation type and state */
120 #define	PTHREAD_CANCEL_ENABLE		0x00
121 #define	PTHREAD_CANCEL_DISABLE		0x01
122 #define	PTHREAD_CANCEL_DEFERRED		0x00
123 #define	PTHREAD_CANCEL_ASYNCHRONOUS	0x02
124 #define	PTHREAD_CANCELED		(void *)-19
125 
126 /* pthread_once related values */
127 #define	PTHREAD_ONCE_NOTDONE	0
128 #define	PTHREAD_ONCE_DONE	1
129 #define	PTHREAD_ONCE_INIT	{ {0, 0, 0, PTHREAD_ONCE_NOTDONE} }
130 
131 /*
132  * The key to be created by pthread_key_create_once_np()
133  * must be statically initialized with PTHREAD_ONCE_KEY_NP.
134  * This must be the same as THR_ONCE_KEY in <thread.h>
135  */
136 #define	PTHREAD_ONCE_KEY_NP	(pthread_key_t)(-1)
137 
138 /* barriers */
139 #define	PTHREAD_BARRIER_SERIAL_THREAD	-2
140 
141 #ifndef	_ASM
142 
143 /*
144  * cancellation cleanup structure
145  */
146 typedef struct _cleanup {
147 	uintptr_t	pthread_cleanup_pad[4];
148 } _cleanup_t;
149 
150 #ifdef	__STDC__
151 
152 void	__pthread_cleanup_push(void (*)(void *), void *, caddr_t, _cleanup_t *);
153 void	__pthread_cleanup_pop(int, _cleanup_t *);
154 caddr_t	_getfp(void);
155 
156 #else	/* __STDC__ */
157 
158 void	__pthread_cleanup_push();
159 void	__pthread_cleanup_pop();
160 caddr_t	_getfp();
161 
162 #endif	/* __STDC__ */
163 
164 #if __cplusplus
165 extern "C" {
166 #endif
167 
168 typedef void (*_Voidfp)(void*); /* pointer to extern "C" function */
169 
170 #if __cplusplus
171 } /* extern "C" */
172 #endif
173 
174 #define	pthread_cleanup_push(routine, args) { \
175 	_cleanup_t _cleanup_info; \
176 	__pthread_cleanup_push((_Voidfp)(routine), (void *)(args), \
177 				(caddr_t)_getfp(), &_cleanup_info);
178 
179 #define	pthread_cleanup_pop(ex) \
180 	__pthread_cleanup_pop(ex, &_cleanup_info); \
181 }
182 
183 #ifdef	__STDC__
184 
185 /*
186  * function prototypes - thread related calls
187  */
188 
189 /*
190  * pthread_atfork() is also declared in <unistd.h> as per SUSv2. The
191  * declarations are identical. A change to either one may also require
192  * appropriate namespace updates in order to avoid redeclaration
193  * warnings in the case where both prototypes are exposed via inclusion
194  * of both <pthread.h> and <unistd.h>.
195  */
196 extern int pthread_atfork(void (*) (void), void (*) (void), void (*) (void));
197 extern int pthread_attr_init(pthread_attr_t *);
198 extern int pthread_attr_destroy(pthread_attr_t *);
199 extern int pthread_attr_setstack(pthread_attr_t *, void *, size_t);
200 extern int pthread_attr_getstack(const pthread_attr_t *_RESTRICT_KYWD,
201 		void **_RESTRICT_KYWD, size_t *_RESTRICT_KYWD);
202 extern int pthread_attr_setstacksize(pthread_attr_t *, size_t);
203 extern int pthread_attr_getstacksize(const pthread_attr_t *_RESTRICT_KYWD,
204 		size_t *_RESTRICT_KYWD);
205 extern int pthread_attr_setstackaddr(pthread_attr_t *, void *);
206 extern int pthread_attr_getstackaddr(const pthread_attr_t *_RESTRICT_KYWD,
207 		void **_RESTRICT_KYWD);
208 extern int pthread_attr_setdetachstate(pthread_attr_t *, int);
209 extern int pthread_attr_getdetachstate(const pthread_attr_t *, int *);
210 extern int pthread_attr_setscope(pthread_attr_t *, int);
211 extern int pthread_attr_getscope(const pthread_attr_t *_RESTRICT_KYWD,
212 	int *_RESTRICT_KYWD);
213 extern int pthread_attr_setinheritsched(pthread_attr_t *, int);
214 extern int pthread_attr_getinheritsched(const pthread_attr_t *_RESTRICT_KYWD,
215 	int *_RESTRICT_KYWD);
216 extern int pthread_attr_setschedpolicy(pthread_attr_t *, int);
217 extern int pthread_attr_getschedpolicy(const pthread_attr_t *_RESTRICT_KYWD,
218 	int *_RESTRICT_KYWD);
219 extern int pthread_attr_setschedparam(pthread_attr_t *_RESTRICT_KYWD,
220 		const struct sched_param *_RESTRICT_KYWD);
221 extern int pthread_attr_getschedparam(const pthread_attr_t *_RESTRICT_KYWD,
222 		struct sched_param *_RESTRICT_KYWD);
223 extern int pthread_create(pthread_t *_RESTRICT_KYWD,
224 		const pthread_attr_t *_RESTRICT_KYWD, void * (*)(void *),
225 		void *_RESTRICT_KYWD);
226 extern int pthread_once(pthread_once_t *, void (*)(void));
227 extern int pthread_join(pthread_t, void **);
228 extern int pthread_detach(pthread_t);
229 extern void pthread_exit(void *) __NORETURN;
230 extern int pthread_cancel(pthread_t);
231 extern int pthread_setschedparam(pthread_t, int, const struct sched_param *);
232 extern int pthread_getschedparam(pthread_t, int *_RESTRICT_KYWD,
233 		struct sched_param *_RESTRICT_KYWD);
234 extern int pthread_setschedprio(pthread_t, int);
235 extern int pthread_setcancelstate(int, int *);
236 extern int pthread_setcanceltype(int, int *);
237 extern void pthread_testcancel(void);
238 extern int pthread_equal(pthread_t, pthread_t);
239 extern int pthread_key_create(pthread_key_t *, void (*)(void *));
240 extern int pthread_key_create_once_np(pthread_key_t *, void (*)(void *));
241 extern int pthread_key_delete(pthread_key_t);
242 extern int pthread_setspecific(pthread_key_t, const void *);
243 extern void *pthread_getspecific(pthread_key_t);
244 extern pthread_t pthread_self(void);
245 
246 /*
247  * function prototypes - synchronization related calls
248  */
249 extern int pthread_mutexattr_init(pthread_mutexattr_t *);
250 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *);
251 extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
252 extern int pthread_mutexattr_getpshared(
253 	const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
254 extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
255 extern int pthread_mutexattr_getprotocol(
256 	const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
257 extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
258 extern int pthread_mutexattr_getprioceiling(
259 	const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
260 extern int pthread_mutexattr_setrobust_np(pthread_mutexattr_t *, int);
261 extern int pthread_mutexattr_getrobust_np(
262 	const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
263 extern int pthread_mutex_init(pthread_mutex_t *_RESTRICT_KYWD,
264 	const pthread_mutexattr_t *_RESTRICT_KYWD);
265 extern int pthread_mutex_consistent_np(pthread_mutex_t *);
266 extern int pthread_mutex_destroy(pthread_mutex_t *);
267 extern int pthread_mutex_lock(pthread_mutex_t *);
268 extern int pthread_mutex_timedlock(pthread_mutex_t *_RESTRICT_KYWD,
269 	const struct timespec *_RESTRICT_KYWD);
270 extern int pthread_mutex_reltimedlock_np(pthread_mutex_t *_RESTRICT_KYWD,
271 	const struct timespec *_RESTRICT_KYWD);
272 extern int pthread_mutex_unlock(pthread_mutex_t *);
273 extern int pthread_mutex_trylock(pthread_mutex_t *);
274 extern int pthread_mutex_setprioceiling(pthread_mutex_t *_RESTRICT_KYWD,
275 	int, int *_RESTRICT_KYWD);
276 extern int pthread_mutex_getprioceiling(const pthread_mutex_t *_RESTRICT_KYWD,
277 	int *_RESTRICT_KYWD);
278 extern int pthread_condattr_init(pthread_condattr_t *);
279 extern int pthread_condattr_destroy(pthread_condattr_t *);
280 extern int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
281 extern int pthread_condattr_getclock(const pthread_condattr_t *_RESTRICT_KYWD,
282 	clockid_t *_RESTRICT_KYWD);
283 extern int pthread_condattr_setpshared(pthread_condattr_t *, int);
284 extern int pthread_condattr_getpshared(const pthread_condattr_t *_RESTRICT_KYWD,
285 	int *_RESTRICT_KYWD);
286 extern int pthread_cond_init(pthread_cond_t *_RESTRICT_KYWD,
287 	const pthread_condattr_t *_RESTRICT_KYWD);
288 extern int pthread_cond_destroy(pthread_cond_t *);
289 extern int pthread_cond_broadcast(pthread_cond_t *);
290 extern int pthread_cond_signal(pthread_cond_t *);
291 extern int pthread_cond_wait(pthread_cond_t *_RESTRICT_KYWD,
292 	pthread_mutex_t *_RESTRICT_KYWD);
293 extern int pthread_cond_timedwait(pthread_cond_t *_RESTRICT_KYWD,
294 	pthread_mutex_t *_RESTRICT_KYWD, const struct timespec *_RESTRICT_KYWD);
295 extern int pthread_cond_reltimedwait_np(pthread_cond_t *_RESTRICT_KYWD,
296 	pthread_mutex_t *_RESTRICT_KYWD, const struct timespec *_RESTRICT_KYWD);
297 extern int pthread_attr_getguardsize(const pthread_attr_t *_RESTRICT_KYWD,
298 	size_t *_RESTRICT_KYWD);
299 extern int pthread_attr_setguardsize(pthread_attr_t *, size_t);
300 extern int pthread_getconcurrency(void);
301 extern int pthread_setconcurrency(int);
302 extern int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
303 extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *_RESTRICT_KYWD,
304 	int *_RESTRICT_KYWD);
305 extern int pthread_rwlock_init(pthread_rwlock_t *_RESTRICT_KYWD,
306 	const pthread_rwlockattr_t *_RESTRICT_KYWD);
307 extern int pthread_rwlock_destroy(pthread_rwlock_t *);
308 extern int pthread_rwlock_rdlock(pthread_rwlock_t *);
309 extern int pthread_rwlock_timedrdlock(pthread_rwlock_t *_RESTRICT_KYWD,
310 	const struct timespec *_RESTRICT_KYWD);
311 extern int pthread_rwlock_reltimedrdlock_np(pthread_rwlock_t *_RESTRICT_KYWD,
312 	const struct timespec *_RESTRICT_KYWD);
313 extern int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
314 extern int pthread_rwlock_wrlock(pthread_rwlock_t *);
315 extern int pthread_rwlock_timedwrlock(pthread_rwlock_t *_RESTRICT_KYWD,
316 	const struct timespec *_RESTRICT_KYWD);
317 extern int pthread_rwlock_reltimedwrlock_np(pthread_rwlock_t *_RESTRICT_KYWD,
318 	const struct timespec *_RESTRICT_KYWD);
319 extern int pthread_rwlock_trywrlock(pthread_rwlock_t *);
320 extern int pthread_rwlock_unlock(pthread_rwlock_t *);
321 extern int pthread_rwlockattr_init(pthread_rwlockattr_t *);
322 extern int pthread_rwlockattr_destroy(pthread_rwlockattr_t *);
323 extern int pthread_rwlockattr_getpshared(
324 	const pthread_rwlockattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
325 extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int);
326 extern int pthread_spin_init(pthread_spinlock_t *, int);
327 extern int pthread_spin_destroy(pthread_spinlock_t *);
328 extern int pthread_spin_lock(pthread_spinlock_t *);
329 extern int pthread_spin_trylock(pthread_spinlock_t *);
330 extern int pthread_spin_unlock(pthread_spinlock_t *);
331 extern int pthread_barrierattr_init(pthread_barrierattr_t *);
332 extern int pthread_barrierattr_destroy(pthread_barrierattr_t *);
333 extern int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
334 extern int pthread_barrierattr_getpshared(
335 	const pthread_barrierattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
336 extern int pthread_barrier_init(pthread_barrier_t *_RESTRICT_KYWD,
337 	const pthread_barrierattr_t *_RESTRICT_KYWD, uint_t);
338 extern int pthread_barrier_destroy(pthread_barrier_t *);
339 extern int pthread_barrier_wait(pthread_barrier_t *);
340 
341 #else	/* __STDC__ */
342 
343 /*
344  * function prototypes - thread related calls
345  */
346 extern int pthread_atfork();
347 extern int pthread_attr_init();
348 extern int pthread_attr_destroy();
349 extern int pthread_attr_setstack();
350 extern int pthread_attr_getstack();
351 extern int pthread_attr_setstacksize();
352 extern int pthread_attr_getstacksize();
353 extern int pthread_attr_setstackaddr();
354 extern int pthread_attr_getstackaddr();
355 extern int pthread_attr_setdetachstate();
356 extern int pthread_attr_getdetachstate();
357 extern int pthread_attr_setscope();
358 extern int pthread_attr_getscope();
359 extern int pthread_attr_setinheritsched();
360 extern int pthread_attr_getinheritsched();
361 extern int pthread_attr_setschedpolicy();
362 extern int pthread_attr_getschedpolicy();
363 extern int pthread_attr_setschedparam();
364 extern int pthread_attr_getschedparam();
365 extern int pthread_create();
366 extern int pthread_once();
367 extern int pthread_join();
368 extern int pthread_detach();
369 extern void pthread_exit();
370 extern int pthread_cancel();
371 extern int pthread_setschedparam();
372 extern int pthread_getschedparam();
373 extern int pthread_setschedprio();
374 extern int pthread_setcancelstate();
375 extern int pthread_setcanceltype();
376 extern void pthread_testcancel();
377 extern int pthread_equal();
378 extern int pthread_key_create();
379 extern int pthread_key_create_once_np();
380 extern int pthread_key_delete();
381 extern int pthread_setspecific();
382 extern void *pthread_getspecific();
383 extern pthread_t pthread_self();
384 /*
385  * function prototypes - synchronization related calls
386  */
387 extern int pthread_mutexattr_init();
388 extern int pthread_mutexattr_destroy();
389 extern int pthread_mutexattr_setpshared();
390 extern int pthread_mutexattr_getpshared();
391 extern int pthread_mutexattr_setprotocol();
392 extern int pthread_mutexattr_getprotocol();
393 extern int pthread_mutexattr_setprioceiling();
394 extern int pthread_mutexattr_getprioceiling();
395 extern int pthread_mutexattr_setrobust_np();
396 extern int pthread_mutexattr_getrobust_np();
397 extern int pthread_mutex_init();
398 extern int pthread_mutex_consistent_np();
399 extern int pthread_mutex_destroy();
400 extern int pthread_mutex_lock();
401 extern int pthread_mutex_timedlock();
402 extern int pthread_mutex_reltimedlock_np();
403 extern int pthread_mutex_unlock();
404 extern int pthread_mutex_trylock();
405 extern int pthread_mutex_setprioceiling();
406 extern int pthread_mutex_getprioceiling();
407 extern int pthread_condattr_init();
408 extern int pthread_condattr_destroy();
409 extern int pthread_condattr_setclock();
410 extern int pthread_condattr_getclock();
411 extern int pthread_condattr_setpshared();
412 extern int pthread_condattr_getpshared();
413 extern int pthread_cond_init();
414 extern int pthread_cond_destroy();
415 extern int pthread_cond_broadcast();
416 extern int pthread_cond_signal();
417 extern int pthread_cond_wait();
418 extern int pthread_cond_timedwait();
419 extern int pthread_cond_reltimedwait_np();
420 extern int pthread_attr_getguardsize();
421 extern int pthread_attr_setguardsize();
422 extern int pthread_getconcurrency();
423 extern int pthread_setconcurrency();
424 extern int pthread_mutexattr_settype();
425 extern int pthread_mutexattr_gettype();
426 extern int pthread_rwlock_init();
427 extern int pthread_rwlock_destroy();
428 extern int pthread_rwlock_rdlock();
429 extern int pthread_rwlock_tryrdlock();
430 extern int pthread_rwlock_wrlock();
431 extern int pthread_rwlock_trywrlock();
432 extern int pthread_rwlock_unlock();
433 extern int pthread_rwlockattr_init();
434 extern int pthread_rwlockattr_destroy();
435 extern int pthread_rwlockattr_getpshared();
436 extern int pthread_rwlockattr_setpshared();
437 extern int pthread_spin_init();
438 extern int pthread_spin_destroy();
439 extern int pthread_spin_lock();
440 extern int pthread_spin_trylock();
441 extern int pthread_spin_unlock();
442 extern int pthread_barrierattr_init();
443 extern int pthread_barrierattr_destroy();
444 extern int pthread_barrierattr_setpshared();
445 extern int pthread_barrierattr_getpshared();
446 extern int pthread_barrier_init();
447 extern int pthread_barrier_destroy();
448 extern int pthread_barrier_wait();
449 
450 #endif	/* __STDC__ */
451 
452 #endif	/* _ASM */
453 
454 #ifdef	__cplusplus
455 }
456 #endif
457 
458 #endif	/* _PTHREAD_H */
459