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