xref: /illumos-gate/usr/src/head/synch.h (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #ifndef	_SYNCH_H
28*7c478bd9Sstevel@tonic-gate #define	_SYNCH_H
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * synch.h:
34*7c478bd9Sstevel@tonic-gate  * definitions needed to use the thread synchronization interface
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifndef _ASM
38*7c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/time_impl.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/synch.h>
41*7c478bd9Sstevel@tonic-gate #endif /* _ASM */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
44*7c478bd9Sstevel@tonic-gate extern "C" {
45*7c478bd9Sstevel@tonic-gate #endif
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #ifndef _ASM
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate  * Semaphores
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate typedef struct _sema {
53*7c478bd9Sstevel@tonic-gate 	/* this structure must be the same as sem_t in <semaphore.h> */
54*7c478bd9Sstevel@tonic-gate 	uint32_t	count;		/* semaphore count */
55*7c478bd9Sstevel@tonic-gate 	uint16_t	type;
56*7c478bd9Sstevel@tonic-gate 	uint16_t	magic;
57*7c478bd9Sstevel@tonic-gate 	upad64_t	pad1[3];	/* reserved for a mutex_t */
58*7c478bd9Sstevel@tonic-gate 	upad64_t 	pad2[2];	/* reserved for a cond_t */
59*7c478bd9Sstevel@tonic-gate } sema_t;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * POSIX.1c Note:
63*7c478bd9Sstevel@tonic-gate  * POSIX.1c requires that <pthread.h> define the structures pthread_mutex_t
64*7c478bd9Sstevel@tonic-gate  * and pthread_cond_t.  These structures are identical to mutex_t (lwp_mutex_t)
65*7c478bd9Sstevel@tonic-gate  * and cond_t (lwp_cond_t) which are defined in <synch.h>.  A nested included
66*7c478bd9Sstevel@tonic-gate  * of <synch.h> (to allow a "#typedef mutex_t  pthread_mutex_t") would pull in
67*7c478bd9Sstevel@tonic-gate  * non-posix symbols/constants violating the namespace restrictions.  Hence,
68*7c478bd9Sstevel@tonic-gate  * pthread_mutex_t/pthread_cond_t have been redefined in <pthread.h> (actually
69*7c478bd9Sstevel@tonic-gate  * in <sys/types.h>).  Any modifications done to mutex_t/lwp_mutex_t or
70*7c478bd9Sstevel@tonic-gate  * cond_t/lwp_cond_t should also be done to pthread_mutex_t/pthread_cond_t.
71*7c478bd9Sstevel@tonic-gate  */
72*7c478bd9Sstevel@tonic-gate typedef lwp_mutex_t mutex_t;
73*7c478bd9Sstevel@tonic-gate typedef lwp_cond_t cond_t;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * Readers/writer locks
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * NOTE: The layout of this structure should be kept in sync with the layout
79*7c478bd9Sstevel@tonic-gate  * of the correponding structure of pthread_rwlock_t in sys/types.h.
80*7c478bd9Sstevel@tonic-gate  * Also, there is an identical structure for lwp_rwlock_t in <sys/synch.h>.
81*7c478bd9Sstevel@tonic-gate  * Because we have to deal with C++, we cannot redefine this one as that one.
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate typedef struct _rwlock {
84*7c478bd9Sstevel@tonic-gate 	int32_t		readers;	/* -1 == writer else # of readers */
85*7c478bd9Sstevel@tonic-gate 	uint16_t	type;
86*7c478bd9Sstevel@tonic-gate 	uint16_t	magic;
87*7c478bd9Sstevel@tonic-gate 	mutex_t		mutex;		/* used to indicate ownership */
88*7c478bd9Sstevel@tonic-gate 	cond_t		readercv;	/* unused */
89*7c478bd9Sstevel@tonic-gate 	cond_t		writercv;	/* unused */
90*7c478bd9Sstevel@tonic-gate } rwlock_t;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate #ifdef	__STDC__
93*7c478bd9Sstevel@tonic-gate int	_lwp_mutex_lock(lwp_mutex_t *);
94*7c478bd9Sstevel@tonic-gate int	_lwp_mutex_unlock(lwp_mutex_t *);
95*7c478bd9Sstevel@tonic-gate int	_lwp_mutex_trylock(lwp_mutex_t *);
96*7c478bd9Sstevel@tonic-gate int	_lwp_cond_wait(lwp_cond_t *, lwp_mutex_t *);
97*7c478bd9Sstevel@tonic-gate int	_lwp_cond_timedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *);
98*7c478bd9Sstevel@tonic-gate int	_lwp_cond_reltimedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *);
99*7c478bd9Sstevel@tonic-gate int	_lwp_cond_signal(lwp_cond_t *);
100*7c478bd9Sstevel@tonic-gate int	_lwp_cond_broadcast(lwp_cond_t *);
101*7c478bd9Sstevel@tonic-gate int	_lwp_sema_init(lwp_sema_t *, int);
102*7c478bd9Sstevel@tonic-gate int	_lwp_sema_wait(lwp_sema_t *);
103*7c478bd9Sstevel@tonic-gate int	_lwp_sema_trywait(lwp_sema_t *);
104*7c478bd9Sstevel@tonic-gate int	_lwp_sema_post(lwp_sema_t *);
105*7c478bd9Sstevel@tonic-gate int	cond_init(cond_t *, int, void *);
106*7c478bd9Sstevel@tonic-gate int	cond_destroy(cond_t *);
107*7c478bd9Sstevel@tonic-gate int	cond_wait(cond_t *, mutex_t *);
108*7c478bd9Sstevel@tonic-gate int	cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
109*7c478bd9Sstevel@tonic-gate int	cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
110*7c478bd9Sstevel@tonic-gate int	cond_signal(cond_t *);
111*7c478bd9Sstevel@tonic-gate int	cond_broadcast(cond_t *);
112*7c478bd9Sstevel@tonic-gate int	mutex_init(mutex_t *, int, void *);
113*7c478bd9Sstevel@tonic-gate int	mutex_destroy(mutex_t *);
114*7c478bd9Sstevel@tonic-gate int	mutex_lock(mutex_t *);
115*7c478bd9Sstevel@tonic-gate int	mutex_trylock(mutex_t *);
116*7c478bd9Sstevel@tonic-gate int	mutex_unlock(mutex_t *);
117*7c478bd9Sstevel@tonic-gate int	rwlock_init(rwlock_t *, int, void *);
118*7c478bd9Sstevel@tonic-gate int	rwlock_destroy(rwlock_t *);
119*7c478bd9Sstevel@tonic-gate int	rw_rdlock(rwlock_t *);
120*7c478bd9Sstevel@tonic-gate int	rw_wrlock(rwlock_t *);
121*7c478bd9Sstevel@tonic-gate int	rw_unlock(rwlock_t *);
122*7c478bd9Sstevel@tonic-gate int	rw_tryrdlock(rwlock_t *);
123*7c478bd9Sstevel@tonic-gate int	rw_trywrlock(rwlock_t *);
124*7c478bd9Sstevel@tonic-gate int	sema_init(sema_t *, unsigned int, int, void *);
125*7c478bd9Sstevel@tonic-gate int	sema_destroy(sema_t *);
126*7c478bd9Sstevel@tonic-gate int	sema_wait(sema_t *);
127*7c478bd9Sstevel@tonic-gate int	sema_timedwait(sema_t *, const timespec_t *);
128*7c478bd9Sstevel@tonic-gate int	sema_reltimedwait(sema_t *, const timespec_t *);
129*7c478bd9Sstevel@tonic-gate int	sema_post(sema_t *);
130*7c478bd9Sstevel@tonic-gate int	sema_trywait(sema_t *);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate int	_lwp_mutex_lock();
135*7c478bd9Sstevel@tonic-gate int	_lwp_mutex_unlock();
136*7c478bd9Sstevel@tonic-gate int	_lwp_mutex_trylock();
137*7c478bd9Sstevel@tonic-gate int	_lwp_cond_wait();
138*7c478bd9Sstevel@tonic-gate int	_lwp_cond_timedwait();
139*7c478bd9Sstevel@tonic-gate int	_lwp_cond_reltimedwait();
140*7c478bd9Sstevel@tonic-gate int	_lwp_cond_signal();
141*7c478bd9Sstevel@tonic-gate int	_lwp_cond_broadcast();
142*7c478bd9Sstevel@tonic-gate int	_lwp_sema_init();
143*7c478bd9Sstevel@tonic-gate int	_lwp_sema_wait();
144*7c478bd9Sstevel@tonic-gate int	_lwp_sema_trywait();
145*7c478bd9Sstevel@tonic-gate int	_lwp_sema_post();
146*7c478bd9Sstevel@tonic-gate int	cond_init();
147*7c478bd9Sstevel@tonic-gate int	cond_destroy();
148*7c478bd9Sstevel@tonic-gate int	cond_wait();
149*7c478bd9Sstevel@tonic-gate int	cond_timedwait();
150*7c478bd9Sstevel@tonic-gate int	cond_reltimedwait();
151*7c478bd9Sstevel@tonic-gate int	cond_signal();
152*7c478bd9Sstevel@tonic-gate int	cond_broadcast();
153*7c478bd9Sstevel@tonic-gate int	mutex_init();
154*7c478bd9Sstevel@tonic-gate int	mutex_destroy();
155*7c478bd9Sstevel@tonic-gate int	mutex_lock();
156*7c478bd9Sstevel@tonic-gate int	mutex_trylock();
157*7c478bd9Sstevel@tonic-gate int	mutex_unlock();
158*7c478bd9Sstevel@tonic-gate int	rwlock_init();
159*7c478bd9Sstevel@tonic-gate int	rwlock_destroy();
160*7c478bd9Sstevel@tonic-gate int	rw_rdlock();
161*7c478bd9Sstevel@tonic-gate int	rw_wrlock();
162*7c478bd9Sstevel@tonic-gate int	rw_unlock();
163*7c478bd9Sstevel@tonic-gate int	rw_tryrdlock();
164*7c478bd9Sstevel@tonic-gate int	rw_trywrlock();
165*7c478bd9Sstevel@tonic-gate int	sema_init();
166*7c478bd9Sstevel@tonic-gate int	sema_destroy();
167*7c478bd9Sstevel@tonic-gate int	sema_wait();
168*7c478bd9Sstevel@tonic-gate int	sema_timedwait();
169*7c478bd9Sstevel@tonic-gate int	sema_reltimedwait();
170*7c478bd9Sstevel@tonic-gate int	sema_post();
171*7c478bd9Sstevel@tonic-gate int	sema_trywait();
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate #endif /* _ASM */
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate /* "Magic numbers" tagging synchronization object types */
178*7c478bd9Sstevel@tonic-gate #define	MUTEX_MAGIC	_MUTEX_MAGIC
179*7c478bd9Sstevel@tonic-gate #define	SEMA_MAGIC	_SEMA_MAGIC
180*7c478bd9Sstevel@tonic-gate #define	COND_MAGIC	_COND_MAGIC
181*7c478bd9Sstevel@tonic-gate #define	RWL_MAGIC	_RWL_MAGIC
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate  * POSIX.1c Note:
185*7c478bd9Sstevel@tonic-gate  * DEFAULTMUTEX is defined same as PTHREAD_MUTEX_INITIALIZER in <pthread.h>.
186*7c478bd9Sstevel@tonic-gate  * DEFAULTCV is defined same as PTHREAD_COND_INITIALIZER in <pthread.h>.
187*7c478bd9Sstevel@tonic-gate  * DEFAULTRWLOCK is defined same as PTHREAD_RWLOCK_INITIALIZER in <pthread.h>.
188*7c478bd9Sstevel@tonic-gate  * Any changes to these macros should be reflected in <pthread.h>
189*7c478bd9Sstevel@tonic-gate  */
190*7c478bd9Sstevel@tonic-gate #define	DEFAULTMUTEX	\
191*7c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD}, MUTEX_MAGIC}, \
192*7c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
193*7c478bd9Sstevel@tonic-gate #define	SHAREDMUTEX	\
194*7c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_PROCESS}, MUTEX_MAGIC}, \
195*7c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
196*7c478bd9Sstevel@tonic-gate #define	RECURSIVEMUTEX	\
197*7c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE}, MUTEX_MAGIC}, \
198*7c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
199*7c478bd9Sstevel@tonic-gate #define	ERRORCHECKMUTEX	\
200*7c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD|LOCK_ERRORCHECK}, MUTEX_MAGIC}, \
201*7c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
202*7c478bd9Sstevel@tonic-gate #define	RECURSIVE_ERRORCHECKMUTEX	\
203*7c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE|LOCK_ERRORCHECK}, \
204*7c478bd9Sstevel@tonic-gate 	MUTEX_MAGIC}, {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
205*7c478bd9Sstevel@tonic-gate #define	DEFAULTCV	\
206*7c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0}, USYNC_THREAD, COND_MAGIC}, 0}
207*7c478bd9Sstevel@tonic-gate #define	SHAREDCV	\
208*7c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0}, USYNC_PROCESS, COND_MAGIC}, 0}
209*7c478bd9Sstevel@tonic-gate #define	DEFAULTSEMA	\
210*7c478bd9Sstevel@tonic-gate 	{0, USYNC_THREAD, SEMA_MAGIC, {0, 0, 0}, {0, 0}}
211*7c478bd9Sstevel@tonic-gate #define	SHAREDSEMA	\
212*7c478bd9Sstevel@tonic-gate 	{0, USYNC_PROCESS, SEMA_MAGIC, {0, 0, 0}, {0, 0}}
213*7c478bd9Sstevel@tonic-gate #define	DEFAULTRWLOCK	\
214*7c478bd9Sstevel@tonic-gate 	{0, USYNC_THREAD, RWL_MAGIC, DEFAULTMUTEX, DEFAULTCV, DEFAULTCV}
215*7c478bd9Sstevel@tonic-gate #define	SHAREDRWLOCK	\
216*7c478bd9Sstevel@tonic-gate 	{0, USYNC_PROCESS, RWL_MAGIC, SHAREDMUTEX, SHAREDCV, SHAREDCV}
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate /*
219*7c478bd9Sstevel@tonic-gate  * Tests on lock states.
220*7c478bd9Sstevel@tonic-gate  */
221*7c478bd9Sstevel@tonic-gate #define	SEMA_HELD(x)		_sema_held(x)
222*7c478bd9Sstevel@tonic-gate #define	RW_READ_HELD(x)		_rw_read_held(x)
223*7c478bd9Sstevel@tonic-gate #define	RW_WRITE_HELD(x)	_rw_write_held(x)
224*7c478bd9Sstevel@tonic-gate #define	RW_LOCK_HELD(x)		(RW_READ_HELD(x) || RW_WRITE_HELD(x))
225*7c478bd9Sstevel@tonic-gate #define	MUTEX_HELD(x)		_mutex_held(x)
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate  * The following definitions are for assertions which can be checked
229*7c478bd9Sstevel@tonic-gate  * statically by tools like lock_lint.  You can also define your own
230*7c478bd9Sstevel@tonic-gate  * run-time test for each.  If you don't, we define them to 1 so that
231*7c478bd9Sstevel@tonic-gate  * such assertions simply pass.
232*7c478bd9Sstevel@tonic-gate  */
233*7c478bd9Sstevel@tonic-gate #ifndef NO_LOCKS_HELD
234*7c478bd9Sstevel@tonic-gate #define	NO_LOCKS_HELD	1
235*7c478bd9Sstevel@tonic-gate #endif
236*7c478bd9Sstevel@tonic-gate #ifndef NO_COMPETING_THREADS
237*7c478bd9Sstevel@tonic-gate #define	NO_COMPETING_THREADS	1
238*7c478bd9Sstevel@tonic-gate #endif
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate #ifndef _ASM
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate #ifdef	__STDC__
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate int _sema_held(sema_t *);
245*7c478bd9Sstevel@tonic-gate int _rw_read_held(rwlock_t *);
246*7c478bd9Sstevel@tonic-gate int _rw_write_held(rwlock_t *);
247*7c478bd9Sstevel@tonic-gate int _mutex_held(mutex_t *);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate int _sema_held();
252*7c478bd9Sstevel@tonic-gate int _rw_read_held();
253*7c478bd9Sstevel@tonic-gate int _rw_write_held();
254*7c478bd9Sstevel@tonic-gate int _mutex_held();
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate #endif /* _ASM */
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate #endif
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate #endif	/* _SYNCH_H */
265