xref: /illumos-gate/usr/src/uts/common/sys/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	_SYS_SYNCH_H
28*7c478bd9Sstevel@tonic-gate #define	_SYS_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 #ifndef _ASM
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/int_types.h>
35*7c478bd9Sstevel@tonic-gate #endif /* _ASM */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
38*7c478bd9Sstevel@tonic-gate extern "C" {
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #ifndef _ASM
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Thread and LWP mutexes have the same type
44*7c478bd9Sstevel@tonic-gate  * definitions.
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * NOTE:
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  * POSIX requires that <pthread.h> define the structures pthread_mutex_t
49*7c478bd9Sstevel@tonic-gate  * and pthread_cond_t.  Although these structures are identical to mutex_t
50*7c478bd9Sstevel@tonic-gate  * (lwp_mutex_t) and cond_t (lwp_cond_t), defined here, a typedef of these
51*7c478bd9Sstevel@tonic-gate  * types would require including <synch.h> in <pthread.h>, pulling in
52*7c478bd9Sstevel@tonic-gate  * non-posix symbols/constants, violating POSIX namespace restrictions.  Hence,
53*7c478bd9Sstevel@tonic-gate  * pthread_mutex_t/pthread_cond_t have been redefined (in <sys/types.h>).
54*7c478bd9Sstevel@tonic-gate  * Any modifications done to mutex_t/lwp_mutex_t or cond_t/lwp_cond_t must
55*7c478bd9Sstevel@tonic-gate  * also be done to pthread_mutex_t/pthread_cond_t.
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate typedef struct _lwp_mutex {
58*7c478bd9Sstevel@tonic-gate 	struct {
59*7c478bd9Sstevel@tonic-gate 		uint16_t	flag1;
60*7c478bd9Sstevel@tonic-gate 		uint8_t		flag2;
61*7c478bd9Sstevel@tonic-gate 		uint8_t		ceiling;
62*7c478bd9Sstevel@tonic-gate 		union {
63*7c478bd9Sstevel@tonic-gate 			uint16_t bcptype;
64*7c478bd9Sstevel@tonic-gate 			struct {
65*7c478bd9Sstevel@tonic-gate 				uint8_t	count_type1;
66*7c478bd9Sstevel@tonic-gate 				uint8_t	count_type2;
67*7c478bd9Sstevel@tonic-gate 			} mtype_rcount;
68*7c478bd9Sstevel@tonic-gate 		} mbcp_type_un;
69*7c478bd9Sstevel@tonic-gate 		uint16_t	magic;
70*7c478bd9Sstevel@tonic-gate 	} flags;
71*7c478bd9Sstevel@tonic-gate 	union {
72*7c478bd9Sstevel@tonic-gate 		struct {
73*7c478bd9Sstevel@tonic-gate 			uint8_t	pad[8];
74*7c478bd9Sstevel@tonic-gate 		} lock64;
75*7c478bd9Sstevel@tonic-gate 		struct {
76*7c478bd9Sstevel@tonic-gate 			uint32_t ownerpid;
77*7c478bd9Sstevel@tonic-gate 			uint32_t lockword;
78*7c478bd9Sstevel@tonic-gate 		} lock32;
79*7c478bd9Sstevel@tonic-gate 		upad64_t owner64;
80*7c478bd9Sstevel@tonic-gate 	} lock;
81*7c478bd9Sstevel@tonic-gate 	upad64_t data;
82*7c478bd9Sstevel@tonic-gate } lwp_mutex_t;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * Thread and LWP condition variables have the same
86*7c478bd9Sstevel@tonic-gate  * type definition.
87*7c478bd9Sstevel@tonic-gate  * NOTE:
88*7c478bd9Sstevel@tonic-gate  * The layout of the following structure should be kept in sync with the
89*7c478bd9Sstevel@tonic-gate  * layout of pthread_cond_t in sys/types.h. See NOTE above for lwp_mutex_t.
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate typedef struct _lwp_cond {
92*7c478bd9Sstevel@tonic-gate 	struct {
93*7c478bd9Sstevel@tonic-gate 		uint8_t		flag[4];
94*7c478bd9Sstevel@tonic-gate 		uint16_t 	type;
95*7c478bd9Sstevel@tonic-gate 		uint16_t 	magic;
96*7c478bd9Sstevel@tonic-gate 	} flags;
97*7c478bd9Sstevel@tonic-gate 	upad64_t data;
98*7c478bd9Sstevel@tonic-gate } lwp_cond_t;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * LWP semaphores
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate typedef struct _lwp_sema {
104*7c478bd9Sstevel@tonic-gate 	uint32_t	count;		/* semaphore count */
105*7c478bd9Sstevel@tonic-gate 	uint16_t 	type;
106*7c478bd9Sstevel@tonic-gate 	uint16_t 	magic;
107*7c478bd9Sstevel@tonic-gate 	uint8_t		flags[8];	/* last byte reserved for waiters */
108*7c478bd9Sstevel@tonic-gate 	upad64_t	data;		/* optional data */
109*7c478bd9Sstevel@tonic-gate } lwp_sema_t;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * Thread and LWP rwlocks have the same type definition.
113*7c478bd9Sstevel@tonic-gate  * NOTE: The layout of this structure should be kept in sync with the layout
114*7c478bd9Sstevel@tonic-gate  * of the correponding structure of pthread_rwlock_t in sys/types.h.
115*7c478bd9Sstevel@tonic-gate  * Also, because we have to deal with C++, there is an identical structure
116*7c478bd9Sstevel@tonic-gate  * for rwlock_t in head/sync.h that we cannot change.
117*7c478bd9Sstevel@tonic-gate  */
118*7c478bd9Sstevel@tonic-gate typedef struct _lwp_rwlock {
119*7c478bd9Sstevel@tonic-gate 	int32_t		readers;	/* -1 == writer else # of readers */
120*7c478bd9Sstevel@tonic-gate 	uint16_t	type;
121*7c478bd9Sstevel@tonic-gate 	uint16_t	magic;
122*7c478bd9Sstevel@tonic-gate 	lwp_mutex_t	mutex;		/* used to indicate ownership */
123*7c478bd9Sstevel@tonic-gate 	lwp_cond_t	readercv;	/* unused */
124*7c478bd9Sstevel@tonic-gate 	lwp_cond_t	writercv;	/* unused */
125*7c478bd9Sstevel@tonic-gate } lwp_rwlock_t;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate #endif /* _ASM */
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate  * Definitions of synchronization types.
130*7c478bd9Sstevel@tonic-gate  */
131*7c478bd9Sstevel@tonic-gate #define	USYNC_THREAD	0x00		/* private to a process */
132*7c478bd9Sstevel@tonic-gate #define	USYNC_PROCESS	0x01		/* shared by processes */
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate /* Keep the following 3 fields in sync with pthread.h */
135*7c478bd9Sstevel@tonic-gate #define	LOCK_NORMAL	0x00		/* same as USYNC_THREAD */
136*7c478bd9Sstevel@tonic-gate #define	LOCK_ERRORCHECK	0x02		/* error check lock */
137*7c478bd9Sstevel@tonic-gate #define	LOCK_RECURSIVE	0x04		/* recursive lock */
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate #define	USYNC_PROCESS_ROBUST	0x08	/* shared by processes robustly */
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate /* Keep the following 5 fields in sync with pthread.h */
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate #define	LOCK_PRIO_NONE		0x00
144*7c478bd9Sstevel@tonic-gate #define	LOCK_PRIO_INHERIT	0x10
145*7c478bd9Sstevel@tonic-gate #define	LOCK_PRIO_PROTECT	0x20
146*7c478bd9Sstevel@tonic-gate #define	LOCK_STALL_NP		0x00
147*7c478bd9Sstevel@tonic-gate #define	LOCK_ROBUST_NP		0x40
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate /*
150*7c478bd9Sstevel@tonic-gate  * lwp_mutex_t flags
151*7c478bd9Sstevel@tonic-gate  */
152*7c478bd9Sstevel@tonic-gate #define	LOCK_OWNERDEAD		0x1
153*7c478bd9Sstevel@tonic-gate #define	LOCK_NOTRECOVERABLE	0x2
154*7c478bd9Sstevel@tonic-gate #define	LOCK_INITED		0x4
155*7c478bd9Sstevel@tonic-gate #define	LOCK_UNMAPPED		0x8
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate #endif
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate #endif /* _SYS_SYNCH_H */
162