1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3eda14cbcSMatt Macy  * All rights reserved.
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * Redistribution and use in source and binary forms, with or without
6eda14cbcSMatt Macy  * modification, are permitted provided that the following conditions
7eda14cbcSMatt Macy  * are met:
8eda14cbcSMatt Macy  * 1. Redistributions of source code must retain the above copyright
9eda14cbcSMatt Macy  *    notice, this list of conditions and the following disclaimer.
10eda14cbcSMatt Macy  * 2. Redistributions in binary form must reproduce the above copyright
11eda14cbcSMatt Macy  *    notice, this list of conditions and the following disclaimer in the
12eda14cbcSMatt Macy  *    documentation and/or other materials provided with the distribution.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15eda14cbcSMatt Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16eda14cbcSMatt Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17eda14cbcSMatt Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18eda14cbcSMatt Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19eda14cbcSMatt Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20eda14cbcSMatt Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21eda14cbcSMatt Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22eda14cbcSMatt Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23eda14cbcSMatt Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24eda14cbcSMatt Macy  * SUCH DAMAGE.
25eda14cbcSMatt Macy  *
26eda14cbcSMatt Macy  * $FreeBSD$
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #ifndef _OPENSOLARIS_SYS_RWLOCK_H_
30eda14cbcSMatt Macy #define	_OPENSOLARIS_SYS_RWLOCK_H_
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy #include <sys/param.h>
33eda14cbcSMatt Macy #include <sys/proc.h>
34eda14cbcSMatt Macy #include <sys/lock.h>
35eda14cbcSMatt Macy #include <sys/sx.h>
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy typedef enum {
38eda14cbcSMatt Macy 	RW_DEFAULT = 4		/* kernel default rwlock */
39eda14cbcSMatt Macy } krw_type_t;
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy 
42eda14cbcSMatt Macy typedef enum {
43eda14cbcSMatt Macy 	RW_NONE		= 0,
44eda14cbcSMatt Macy 	RW_WRITER	= 1,
45eda14cbcSMatt Macy 	RW_READER	= 2
46eda14cbcSMatt Macy } krw_t;
47eda14cbcSMatt Macy 
48eda14cbcSMatt Macy typedef	struct sx	krwlock_t;
49eda14cbcSMatt Macy 
50eda14cbcSMatt Macy #ifndef OPENSOLARIS_WITNESS
51*c03c5b1cSMartin Matuska #define	RW_FLAGS	(SX_DUPOK | SX_NEW | SX_NOWITNESS)
52eda14cbcSMatt Macy #else
53*c03c5b1cSMartin Matuska #define	RW_FLAGS	(SX_DUPOK | SX_NEW)
54eda14cbcSMatt Macy #endif
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy #define	RW_READ_HELD(x)		(rw_read_held((x)))
57eda14cbcSMatt Macy #define	RW_WRITE_HELD(x)	(rw_write_held((x)))
58eda14cbcSMatt Macy #define	RW_LOCK_HELD(x)		(rw_lock_held((x)))
59eda14cbcSMatt Macy #define	RW_ISWRITER(x)		(rw_iswriter(x))
60eda14cbcSMatt Macy #define	rw_init(lock, desc, type, arg)	do {				\
61eda14cbcSMatt Macy 	const char *_name;						\
62eda14cbcSMatt Macy 	ASSERT((type) == 0 || (type) == RW_DEFAULT);			\
63eda14cbcSMatt Macy 	for (_name = #lock; *_name != '\0'; _name++) {			\
64eda14cbcSMatt Macy 		if (*_name >= 'a' && *_name <= 'z')			\
65eda14cbcSMatt Macy 			break;						\
66eda14cbcSMatt Macy 	}								\
67eda14cbcSMatt Macy 	if (*_name == '\0')						\
68eda14cbcSMatt Macy 		_name = #lock;						\
69eda14cbcSMatt Macy 	sx_init_flags((lock), _name, RW_FLAGS);				\
70eda14cbcSMatt Macy } while (0)
71eda14cbcSMatt Macy #define	rw_destroy(lock)	sx_destroy(lock)
72eda14cbcSMatt Macy #define	rw_enter(lock, how)	do {					\
73eda14cbcSMatt Macy 	if ((how) == RW_READER)						\
74eda14cbcSMatt Macy 		sx_slock(lock);						\
75eda14cbcSMatt Macy 	else /* if ((how) == RW_WRITER) */				\
76eda14cbcSMatt Macy 		sx_xlock(lock);						\
77eda14cbcSMatt Macy 	} while (0)
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy #define	rw_tryenter(lock, how)			   \
80eda14cbcSMatt Macy 	((how) == RW_READER ? sx_try_slock(lock) : sx_try_xlock(lock))
81eda14cbcSMatt Macy #define	rw_exit(lock)		sx_unlock(lock)
82eda14cbcSMatt Macy #define	rw_downgrade(lock)	sx_downgrade(lock)
83eda14cbcSMatt Macy #define	rw_tryupgrade(lock)	sx_try_upgrade(lock)
84eda14cbcSMatt Macy #define	rw_read_held(lock)					  \
85eda14cbcSMatt Macy 	((lock)->sx_lock != SX_LOCK_UNLOCKED &&	  \
86eda14cbcSMatt Macy 	    ((lock)->sx_lock & SX_LOCK_SHARED))
87eda14cbcSMatt Macy #define	rw_write_held(lock)	sx_xlocked(lock)
88eda14cbcSMatt Macy #define	rw_lock_held(lock)	(rw_read_held(lock) || rw_write_held(lock))
89eda14cbcSMatt Macy #define	rw_iswriter(lock)	sx_xlocked(lock)
90eda14cbcSMatt Macy #define	rw_owner(lock)		sx_xholder(lock)
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy #endif	/* _OPENSOLARIS_SYS_RWLOCK_H_ */
93