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