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