FreeBSD/Linux Kernel Cross Reference
sys/sys/rwlock.h
1 /* $NetBSD: rwlock.h,v 1.6 2008/04/28 20:24:11 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _SYS_RWLOCK_H_
33 #define _SYS_RWLOCK_H_
34
35 /*
36 * The rwlock provides exclusive access when held as a "writer",
37 * and shared access when held as a "reader".
38 *
39 * Architectures may optionally provide stubs for the following functions to
40 * implement the easy (unlocked, no waiters) cases. If these stubs are
41 * provided, __HAVE_RW_STUBS should be defined.
42 *
43 * rw_enter()
44 * rw_exit()
45 * rw_tryenter()
46 */
47
48 #if defined(_KERNEL_OPT)
49 #include "opt_lockdebug.h"
50 #endif
51
52 #if !defined(_KERNEL)
53 #include <sys/types.h>
54 #include <sys/inttypes.h>
55 #endif
56
57 typedef enum krw_t {
58 RW_READER = 0,
59 RW_WRITER = 1
60 } krw_t;
61
62 typedef struct krwlock krwlock_t;
63
64 #ifdef __RWLOCK_PRIVATE
65 /*
66 * Bits in the owner field of the lock that indicate lock state. If the
67 * WRITE_LOCKED bit is clear, then the owner field is actually a count of
68 * the number of readers. The rw_owner field is laid out like so:
69 *
70 * N 4 3 2 1 0
71 * +------------------------------------------------------+
72 * | owner or read count | debug | wrlock | wrwant | wait |
73 * +------------------------------------------------------+
74 */
75 #define RW_HAS_WAITERS 0x01UL /* lock has waiters */
76 #define RW_WRITE_WANTED 0x02UL /* >= 1 waiter is a writer */
77 #define RW_WRITE_LOCKED 0x04UL /* lock is currently write locked */
78 #define RW_DEBUG 0x08UL /* LOCKDEBUG enabled */
79
80 #define RW_READ_COUNT_SHIFT 4
81 #define RW_READ_INCR (1UL << RW_READ_COUNT_SHIFT)
82 #define RW_THREAD ((uintptr_t)-RW_READ_INCR)
83 #define RW_OWNER(rw) ((rw)->rw_owner & RW_THREAD)
84 #define RW_COUNT(rw) ((rw)->rw_owner & RW_THREAD)
85 #define RW_FLAGS(rw) ((rw)->rw_owner & ~RW_THREAD)
86
87 void rw_vector_enter(krwlock_t *, const krw_t);
88 void rw_vector_exit(krwlock_t *);
89 int rw_vector_tryenter(krwlock_t *, const krw_t);
90 #endif /* __RWLOCK_PRIVATE */
91
92 #include <machine/rwlock.h>
93
94 #ifdef _KERNEL
95
96 void rw_init(krwlock_t *);
97 void rw_destroy(krwlock_t *);
98
99 int rw_tryenter(krwlock_t *, const krw_t);
100 int rw_tryupgrade(krwlock_t *);
101 void rw_downgrade(krwlock_t *);
102
103 int rw_read_held(krwlock_t *);
104 int rw_write_held(krwlock_t *);
105 int rw_lock_held(krwlock_t *);
106
107 void rw_enter(krwlock_t *, const krw_t);
108 void rw_exit(krwlock_t *);
109
110 #endif /* _KERNEL */
111
112 #endif /* _SYS_RWLOCK_H_ */
Cache object: 079a78a1855d970c5e186603f5efca17
|