1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5185029Spjd * Common Development and Distribution License (the "License").
6185029Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21185029Spjd
22168404Spjd/*
23185029Spjd * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24168404Spjd * Use is subject to license terms.
25168404Spjd */
26168404Spjd
27168404Spjd#ifndef	_SYS_SYNCH_H
28168404Spjd#define	_SYS_SYNCH_H
29168404Spjd
30168404Spjd#pragma ident	"%Z%%M%	%I%	%E% SMI"
31168404Spjd
32168404Spjd#ifndef _ASM
33168404Spjd#include <sys/types.h>
34168404Spjd#include <sys/int_types.h>
35168404Spjd#endif /* _ASM */
36168404Spjd
37168404Spjd#ifdef	__cplusplus
38168404Spjdextern "C" {
39168404Spjd#endif
40168404Spjd
41168404Spjd#ifndef _ASM
42168404Spjd/*
43168404Spjd * Thread and LWP mutexes have the same type
44168404Spjd * definitions.
45168404Spjd *
46168404Spjd * NOTE:
47168404Spjd *
48168404Spjd * POSIX requires that <pthread.h> define the structures pthread_mutex_t
49168404Spjd * and pthread_cond_t.  Although these structures are identical to mutex_t
50168404Spjd * (lwp_mutex_t) and cond_t (lwp_cond_t), defined here, a typedef of these
51168404Spjd * types would require including <synch.h> in <pthread.h>, pulling in
52168404Spjd * non-posix symbols/constants, violating POSIX namespace restrictions.  Hence,
53168404Spjd * pthread_mutex_t/pthread_cond_t have been redefined (in <sys/types.h>).
54168404Spjd * Any modifications done to mutex_t/lwp_mutex_t or cond_t/lwp_cond_t must
55168404Spjd * also be done to pthread_mutex_t/pthread_cond_t.
56168404Spjd */
57168404Spjdtypedef struct _lwp_mutex {
58168404Spjd	struct {
59168404Spjd		uint16_t	flag1;
60168404Spjd		uint8_t		flag2;
61168404Spjd		uint8_t		ceiling;
62168404Spjd		union {
63168404Spjd			uint16_t bcptype;
64168404Spjd			struct {
65168404Spjd				uint8_t	count_type1;
66168404Spjd				uint8_t	count_type2;
67168404Spjd			} mtype_rcount;
68168404Spjd		} mbcp_type_un;
69168404Spjd		uint16_t	magic;
70168404Spjd	} flags;
71168404Spjd	union {
72168404Spjd		struct {
73168404Spjd			uint8_t	pad[8];
74168404Spjd		} lock64;
75168404Spjd		struct {
76168404Spjd			uint32_t ownerpid;
77168404Spjd			uint32_t lockword;
78168404Spjd		} lock32;
79168404Spjd		upad64_t owner64;
80168404Spjd	} lock;
81168404Spjd	upad64_t data;
82168404Spjd} lwp_mutex_t;
83168404Spjd
84168404Spjd/*
85168404Spjd * Thread and LWP condition variables have the same
86168404Spjd * type definition.
87168404Spjd * NOTE:
88168404Spjd * The layout of the following structure should be kept in sync with the
89168404Spjd * layout of pthread_cond_t in sys/types.h. See NOTE above for lwp_mutex_t.
90168404Spjd */
91168404Spjdtypedef struct _lwp_cond {
92168404Spjd	struct {
93168404Spjd		uint8_t		flag[4];
94168404Spjd		uint16_t 	type;
95168404Spjd		uint16_t 	magic;
96168404Spjd	} flags;
97168404Spjd	upad64_t data;
98168404Spjd} lwp_cond_t;
99168404Spjd
100168404Spjd/*
101168404Spjd * LWP semaphores
102168404Spjd */
103168404Spjdtypedef struct _lwp_sema {
104168404Spjd	uint32_t	count;		/* semaphore count */
105168404Spjd	uint16_t 	type;
106168404Spjd	uint16_t 	magic;
107168404Spjd	uint8_t		flags[8];	/* last byte reserved for waiters */
108168404Spjd	upad64_t	data;		/* optional data */
109168404Spjd} lwp_sema_t;
110168404Spjd
111168404Spjd/*
112168404Spjd * Thread and LWP rwlocks have the same type definition.
113168404Spjd * NOTE: The layout of this structure should be kept in sync with the layout
114168404Spjd * of the correponding structure of pthread_rwlock_t in sys/types.h.
115168404Spjd * Also, because we have to deal with C++, there is an identical structure
116168404Spjd * for rwlock_t in head/sync.h that we cannot change.
117168404Spjd */
118168404Spjdtypedef struct _lwp_rwlock {
119185029Spjd	int32_t		readers;	/* rwstate word */
120168404Spjd	uint16_t	type;
121168404Spjd	uint16_t	magic;
122185029Spjd	lwp_mutex_t	mutex;		/* used with process-shared rwlocks */
123185029Spjd	lwp_cond_t	readercv;	/* used only to indicate ownership */
124185029Spjd	lwp_cond_t	writercv;	/* used only to indicate ownership */
125168404Spjd} lwp_rwlock_t;
126168404Spjd
127168404Spjd#endif /* _ASM */
128168404Spjd/*
129168404Spjd * Definitions of synchronization types.
130168404Spjd */
131168404Spjd#define	USYNC_THREAD	0x00		/* private to a process */
132168404Spjd#define	USYNC_PROCESS	0x01		/* shared by processes */
133168404Spjd
134185029Spjd/* Keep the following values in sync with pthread.h */
135185029Spjd#define	LOCK_NORMAL		0x00		/* same as USYNC_THREAD */
136185029Spjd#define	LOCK_SHARED		0x01		/* same as USYNC_PROCESS */
137185029Spjd#define	LOCK_ERRORCHECK		0x02		/* error check lock */
138185029Spjd#define	LOCK_RECURSIVE		0x04		/* recursive lock */
139185029Spjd#define	LOCK_PRIO_INHERIT	0x10		/* priority inheritance lock */
140185029Spjd#define	LOCK_PRIO_PROTECT	0x20		/* priority ceiling lock */
141185029Spjd#define	LOCK_ROBUST		0x40		/* robust lock */
142168404Spjd
143185029Spjd/*
144185029Spjd * USYNC_PROCESS_ROBUST is a deprecated historical type.  It is mapped
145185029Spjd * into (USYNC_PROCESS | LOCK_ROBUST) by mutex_init().  Application code
146185029Spjd * should be revised to use (USYNC_PROCESS | LOCK_ROBUST) rather than this.
147185029Spjd */
148185029Spjd#define	USYNC_PROCESS_ROBUST	0x08
149168404Spjd
150168404Spjd/*
151168404Spjd * lwp_mutex_t flags
152168404Spjd */
153168404Spjd#define	LOCK_OWNERDEAD		0x1
154168404Spjd#define	LOCK_NOTRECOVERABLE	0x2
155168404Spjd#define	LOCK_INITED		0x4
156168404Spjd#define	LOCK_UNMAPPED		0x8
157168404Spjd
158168404Spjd#ifdef	__cplusplus
159168404Spjd}
160168404Spjd#endif
161168404Spjd
162168404Spjd#endif /* _SYS_SYNCH_H */
163