xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/sys/rwlock.h (revision 8ac904ce090b1c2e355da8aa122ca2252183f4e1)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
26  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27  */
28 
29 #ifndef _SYS_RWLOCK_H
30 #define	_SYS_RWLOCK_H
31 
32 #include <pthread.h>
33 
34 /*
35  * RW locks
36  */
37 typedef struct krwlock {
38 	pthread_rwlock_t	rw_lock;
39 	pthread_t		rw_owner;
40 	uint_t			rw_readers;
41 } krwlock_t;
42 
43 typedef int krw_t;
44 
45 #define	RW_READER		0
46 #define	RW_WRITER		1
47 #define	RW_DEFAULT		RW_READER
48 #define	RW_NOLOCKDEP		RW_READER
49 
50 #define	RW_READ_HELD(rw)	((rw)->rw_readers > 0)
51 #define	RW_WRITE_HELD(rw)	pthread_equal((rw)->rw_owner, pthread_self())
52 #define	RW_LOCK_HELD(rw)	(RW_READ_HELD(rw) || RW_WRITE_HELD(rw))
53 
54 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
55 extern void rw_destroy(krwlock_t *rwlp);
56 extern void rw_enter(krwlock_t *rwlp, krw_t rw);
57 extern int rw_tryenter(krwlock_t *rwlp, krw_t rw);
58 extern int rw_tryupgrade(krwlock_t *rwlp);
59 extern void rw_exit(krwlock_t *rwlp);
60 #define	rw_downgrade(rwlp) do { } while (0)
61 
62 #endif /* _SYS_RWLOCK_H */
63