xref: /illumos-gate/usr/src/uts/common/sys/synch.h (revision fafb665d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
541efec22Sraf  * Common Development and Distribution License (the "License").
641efec22Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2141efec22Sraf 
227c478bd9Sstevel@tonic-gate /*
2341efec22Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_SYS_SYNCH_H
287c478bd9Sstevel@tonic-gate #define	_SYS_SYNCH_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifndef _ASM
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/int_types.h>
337c478bd9Sstevel@tonic-gate #endif /* _ASM */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
367c478bd9Sstevel@tonic-gate extern "C" {
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifndef _ASM
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Thread and LWP mutexes have the same type
427c478bd9Sstevel@tonic-gate  * definitions.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * NOTE:
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * POSIX requires that <pthread.h> define the structures pthread_mutex_t
477c478bd9Sstevel@tonic-gate  * and pthread_cond_t.  Although these structures are identical to mutex_t
487c478bd9Sstevel@tonic-gate  * (lwp_mutex_t) and cond_t (lwp_cond_t), defined here, a typedef of these
497c478bd9Sstevel@tonic-gate  * types would require including <synch.h> in <pthread.h>, pulling in
507c478bd9Sstevel@tonic-gate  * non-posix symbols/constants, violating POSIX namespace restrictions.  Hence,
517c478bd9Sstevel@tonic-gate  * pthread_mutex_t/pthread_cond_t have been redefined (in <sys/types.h>).
527c478bd9Sstevel@tonic-gate  * Any modifications done to mutex_t/lwp_mutex_t or cond_t/lwp_cond_t must
537c478bd9Sstevel@tonic-gate  * also be done to pthread_mutex_t/pthread_cond_t.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate typedef struct _lwp_mutex {
567c478bd9Sstevel@tonic-gate 	struct {
577c478bd9Sstevel@tonic-gate 		uint16_t	flag1;
587c478bd9Sstevel@tonic-gate 		uint8_t		flag2;
597c478bd9Sstevel@tonic-gate 		uint8_t		ceiling;
607c478bd9Sstevel@tonic-gate 		union {
617c478bd9Sstevel@tonic-gate 			uint16_t bcptype;
627c478bd9Sstevel@tonic-gate 			struct {
637c478bd9Sstevel@tonic-gate 				uint8_t	count_type1;
647c478bd9Sstevel@tonic-gate 				uint8_t	count_type2;
657c478bd9Sstevel@tonic-gate 			} mtype_rcount;
667c478bd9Sstevel@tonic-gate 		} mbcp_type_un;
677c478bd9Sstevel@tonic-gate 		uint16_t	magic;
687c478bd9Sstevel@tonic-gate 	} flags;
697c478bd9Sstevel@tonic-gate 	union {
707c478bd9Sstevel@tonic-gate 		struct {
717c478bd9Sstevel@tonic-gate 			uint8_t	pad[8];
727c478bd9Sstevel@tonic-gate 		} lock64;
737c478bd9Sstevel@tonic-gate 		struct {
747c478bd9Sstevel@tonic-gate 			uint32_t ownerpid;
757c478bd9Sstevel@tonic-gate 			uint32_t lockword;
767c478bd9Sstevel@tonic-gate 		} lock32;
777c478bd9Sstevel@tonic-gate 		upad64_t owner64;
787c478bd9Sstevel@tonic-gate 	} lock;
797c478bd9Sstevel@tonic-gate 	upad64_t data;
807c478bd9Sstevel@tonic-gate } lwp_mutex_t;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Thread and LWP condition variables have the same
847c478bd9Sstevel@tonic-gate  * type definition.
857c478bd9Sstevel@tonic-gate  * NOTE:
867c478bd9Sstevel@tonic-gate  * The layout of the following structure should be kept in sync with the
877c478bd9Sstevel@tonic-gate  * layout of pthread_cond_t in sys/types.h. See NOTE above for lwp_mutex_t.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate typedef struct _lwp_cond {
907c478bd9Sstevel@tonic-gate 	struct {
917c478bd9Sstevel@tonic-gate 		uint8_t		flag[4];
927c478bd9Sstevel@tonic-gate 		uint16_t 	type;
937c478bd9Sstevel@tonic-gate 		uint16_t 	magic;
947c478bd9Sstevel@tonic-gate 	} flags;
957c478bd9Sstevel@tonic-gate 	upad64_t data;
967c478bd9Sstevel@tonic-gate } lwp_cond_t;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * LWP semaphores
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate typedef struct _lwp_sema {
1027c478bd9Sstevel@tonic-gate 	uint32_t	count;		/* semaphore count */
1037c478bd9Sstevel@tonic-gate 	uint16_t 	type;
1047c478bd9Sstevel@tonic-gate 	uint16_t 	magic;
1057c478bd9Sstevel@tonic-gate 	uint8_t		flags[8];	/* last byte reserved for waiters */
1067c478bd9Sstevel@tonic-gate 	upad64_t	data;		/* optional data */
1077c478bd9Sstevel@tonic-gate } lwp_sema_t;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Thread and LWP rwlocks have the same type definition.
1117c478bd9Sstevel@tonic-gate  * NOTE: The layout of this structure should be kept in sync with the layout
1127c478bd9Sstevel@tonic-gate  * of the correponding structure of pthread_rwlock_t in sys/types.h.
1137c478bd9Sstevel@tonic-gate  * Also, because we have to deal with C++, there is an identical structure
1147c478bd9Sstevel@tonic-gate  * for rwlock_t in head/sync.h that we cannot change.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate typedef struct _lwp_rwlock {
11741efec22Sraf 	int32_t		readers;	/* rwstate word */
1187c478bd9Sstevel@tonic-gate 	uint16_t	type;
1197c478bd9Sstevel@tonic-gate 	uint16_t	magic;
12041efec22Sraf 	lwp_mutex_t	mutex;		/* used with process-shared rwlocks */
12141efec22Sraf 	lwp_cond_t	readercv;	/* used only to indicate ownership */
12241efec22Sraf 	lwp_cond_t	writercv;	/* used only to indicate ownership */
1237c478bd9Sstevel@tonic-gate } lwp_rwlock_t;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #endif /* _ASM */
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Definitions of synchronization types.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate #define	USYNC_THREAD	0x00		/* private to a process */
1307c478bd9Sstevel@tonic-gate #define	USYNC_PROCESS	0x01		/* shared by processes */
1317c478bd9Sstevel@tonic-gate 
132*883492d5Sraf /* Keep the following values in sync with pthread.h */
133*883492d5Sraf #define	LOCK_NORMAL		0x00		/* same as USYNC_THREAD */
134*883492d5Sraf #define	LOCK_SHARED		0x01		/* same as USYNC_PROCESS */
135*883492d5Sraf #define	LOCK_ERRORCHECK		0x02		/* error check lock */
136*883492d5Sraf #define	LOCK_RECURSIVE		0x04		/* recursive lock */
137*883492d5Sraf #define	LOCK_PRIO_INHERIT	0x10		/* priority inheritance lock */
138*883492d5Sraf #define	LOCK_PRIO_PROTECT	0x20		/* priority ceiling lock */
139*883492d5Sraf #define	LOCK_ROBUST		0x40		/* robust lock */
1407c478bd9Sstevel@tonic-gate 
141*883492d5Sraf /*
142*883492d5Sraf  * USYNC_PROCESS_ROBUST is a deprecated historical type.  It is mapped
143*883492d5Sraf  * into (USYNC_PROCESS | LOCK_ROBUST) by mutex_init().  Application code
144*883492d5Sraf  * should be revised to use (USYNC_PROCESS | LOCK_ROBUST) rather than this.
145*883492d5Sraf  */
146*883492d5Sraf #define	USYNC_PROCESS_ROBUST	0x08
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * lwp_mutex_t flags
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate #define	LOCK_OWNERDEAD		0x1
1527c478bd9Sstevel@tonic-gate #define	LOCK_NOTRECOVERABLE	0x2
1537c478bd9Sstevel@tonic-gate #define	LOCK_INITED		0x4
1547c478bd9Sstevel@tonic-gate #define	LOCK_UNMAPPED		0x8
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate #endif
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #endif /* _SYS_SYNCH_H */
161