]>
Commit | Line | Data |
---|---|---|
716154c5 BB |
1 | /*****************************************************************************\ |
2 | * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. | |
3 | * Copyright (C) 2007 The Regents of the University of California. | |
4 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
5 | * Written by Brian Behlendorf <behlendorf1@llnl.gov>. | |
715f6251 | 6 | * UCRL-CODE-235197 |
7 | * | |
716154c5 | 8 | * This file is part of the SPL, Solaris Porting Layer. |
3d6af2dd | 9 | * For details, see <http://zfsonlinux.org/>. |
716154c5 BB |
10 | * |
11 | * The SPL is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
715f6251 | 15 | * |
716154c5 | 16 | * The SPL is distributed in the hope that it will be useful, but WITHOUT |
715f6251 | 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | * for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
716154c5 BB |
22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
23 | \*****************************************************************************/ | |
715f6251 | 24 | |
09b414e8 | 25 | #ifndef _SPL_RWLOCK_H |
e8b31e84 | 26 | #define _SPL_RWLOCK_H |
f1ca4da6 | 27 | |
f4b37741 | 28 | #include <sys/types.h> |
d28db80f | 29 | #include <linux/rwsem.h> |
46aa7b39 | 30 | #include <linux/rwsem_compat.h> |
f1ca4da6 | 31 | |
32 | typedef enum { | |
e811949a BB |
33 | RW_DRIVER = 2, |
34 | RW_DEFAULT = 4 | |
f1ca4da6 | 35 | } krw_type_t; |
36 | ||
37 | typedef enum { | |
d28db80f | 38 | RW_NONE = 0, |
e811949a BB |
39 | RW_WRITER = 1, |
40 | RW_READER = 2 | |
f1ca4da6 | 41 | } krw_t; |
42 | ||
d28db80f BB |
43 | typedef struct { |
44 | struct rw_semaphore rw_rwlock; | |
45 | kthread_t *rw_owner; | |
46 | } krwlock_t; | |
f1ca4da6 | 47 | |
d28db80f BB |
48 | #define SEM(rwp) ((struct rw_semaphore *)(rwp)) |
49 | ||
d28db80f BB |
50 | static inline void |
51 | spl_rw_set_owner(krwlock_t *rwp) | |
52 | { | |
4f8e643a | 53 | rwp->rw_owner = current; |
d28db80f BB |
54 | } |
55 | ||
56 | static inline void | |
57 | spl_rw_clear_owner(krwlock_t *rwp) | |
58 | { | |
4f8e643a | 59 | rwp->rw_owner = NULL; |
d28db80f BB |
60 | } |
61 | ||
62 | static inline kthread_t * | |
63 | rw_owner(krwlock_t *rwp) | |
64 | { | |
4f8e643a | 65 | return rwp->rw_owner; |
d28db80f BB |
66 | } |
67 | ||
68 | static inline int | |
69 | RW_READ_HELD(krwlock_t *rwp) | |
70 | { | |
86c16c59 | 71 | return (spl_rwsem_is_locked(SEM(rwp)) && rw_owner(rwp) == NULL); |
d28db80f BB |
72 | } |
73 | ||
74 | static inline int | |
75 | RW_WRITE_HELD(krwlock_t *rwp) | |
76 | { | |
4f8e643a | 77 | return (rw_owner(rwp) == current); |
d28db80f BB |
78 | } |
79 | ||
80 | static inline int | |
81 | RW_LOCK_HELD(krwlock_t *rwp) | |
82 | { | |
46aa7b39 | 83 | return spl_rwsem_is_locked(SEM(rwp)); |
d28db80f BB |
84 | } |
85 | ||
86 | /* | |
87 | * The following functions must be a #define and not static inline. | |
88 | * This ensures that the native linux semaphore functions (down/up) | |
89 | * will be correctly located in the users code which is important | |
90 | * for the built in kernel lock analysis tools | |
91 | */ | |
92 | #define rw_init(rwp, name, type, arg) \ | |
93 | ({ \ | |
0e77fc11 BB |
94 | static struct lock_class_key __key; \ |
95 | \ | |
96 | __init_rwsem(SEM(rwp), #rwp, &__key); \ | |
d28db80f | 97 | spl_rw_clear_owner(rwp); \ |
e811949a | 98 | }) |
f1ca4da6 | 99 | |
d28db80f BB |
100 | #define rw_destroy(rwp) \ |
101 | ({ \ | |
102 | VERIFY(!RW_LOCK_HELD(rwp)); \ | |
e811949a | 103 | }) |
ed61a7d0 | 104 | |
d28db80f BB |
105 | #define rw_tryenter(rwp, rw) \ |
106 | ({ \ | |
107 | int _rc_ = 0; \ | |
108 | \ | |
109 | switch (rw) { \ | |
110 | case RW_READER: \ | |
111 | _rc_ = down_read_trylock(SEM(rwp)); \ | |
112 | break; \ | |
113 | case RW_WRITER: \ | |
114 | if ((_rc_ = down_write_trylock(SEM(rwp)))) \ | |
115 | spl_rw_set_owner(rwp); \ | |
116 | break; \ | |
117 | default: \ | |
55abb092 | 118 | VERIFY(0); \ |
d28db80f BB |
119 | } \ |
120 | _rc_; \ | |
e8b31e84 | 121 | }) |
e8b31e84 | 122 | |
d28db80f BB |
123 | #define rw_enter(rwp, rw) \ |
124 | ({ \ | |
125 | switch (rw) { \ | |
126 | case RW_READER: \ | |
127 | down_read(SEM(rwp)); \ | |
128 | break; \ | |
129 | case RW_WRITER: \ | |
130 | down_write(SEM(rwp)); \ | |
131 | spl_rw_set_owner(rwp); \ | |
132 | break; \ | |
133 | default: \ | |
55abb092 | 134 | VERIFY(0); \ |
d28db80f | 135 | } \ |
e811949a | 136 | }) |
f1ca4da6 | 137 | |
d28db80f BB |
138 | #define rw_exit(rwp) \ |
139 | ({ \ | |
140 | if (RW_WRITE_HELD(rwp)) { \ | |
141 | spl_rw_clear_owner(rwp); \ | |
142 | up_write(SEM(rwp)); \ | |
143 | } else { \ | |
144 | ASSERT(RW_READ_HELD(rwp)); \ | |
145 | up_read(SEM(rwp)); \ | |
146 | } \ | |
147 | }) | |
148 | ||
149 | #define rw_downgrade(rwp) \ | |
150 | ({ \ | |
151 | spl_rw_clear_owner(rwp); \ | |
152 | downgrade_write(SEM(rwp)); \ | |
153 | }) | |
154 | ||
ef6c1368 | 155 | #if defined(CONFIG_RWSEM_GENERIC_SPINLOCK) |
32f5faff BB |
156 | /* |
157 | * For the generic implementations of rw-semaphores the following is | |
158 | * true. If your semaphore implementation internally represents the | |
159 | * semaphore state differently then special case handling is required. | |
160 | * - if activity/count is 0 then there are no active readers or writers | |
161 | * - if activity/count is +ve then that is the number of active readers | |
162 | * - if activity/count is -1 then there is one active writer | |
163 | */ | |
164 | ||
165 | extern void __up_read_locked(struct rw_semaphore *); | |
166 | extern int __down_write_trylock_locked(struct rw_semaphore *); | |
167 | ||
d28db80f BB |
168 | #define rw_tryupgrade(rwp) \ |
169 | ({ \ | |
170 | unsigned long _flags_; \ | |
171 | int _rc_ = 0; \ | |
172 | \ | |
588d9004 | 173 | spl_rwsem_lock_irqsave(&SEM(rwp)->wait_lock, _flags_); \ |
32f5faff BB |
174 | if ((list_empty(&SEM(rwp)->wait_list)) && \ |
175 | (SEM(rwp)->activity == 1)) { \ | |
176 | __up_read_locked(SEM(rwp)); \ | |
177 | VERIFY(_rc_ = __down_write_trylock_locked(SEM(rwp))); \ | |
d28db80f BB |
178 | (rwp)->rw_owner = current; \ |
179 | } \ | |
588d9004 | 180 | spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, _flags_); \ |
d28db80f BB |
181 | _rc_; \ |
182 | }) | |
ef6c1368 BB |
183 | #else |
184 | /* | |
32f5faff BB |
185 | * rw_tryupgrade() can be implemented correctly but for each supported |
186 | * arch we will need a custom implementation. For the x86 implementation | |
187 | * it looks like a custom cmpxchg() to atomically check and promote the | |
188 | * rwsem would be safe. For now that's not worth the trouble so in this | |
189 | * case rw_tryupgrade() has just been disabled. | |
ef6c1368 BB |
190 | */ |
191 | #define rw_tryupgrade(rwp) ({ 0; }) | |
192 | #endif | |
d28db80f BB |
193 | |
194 | int spl_rw_init(void); | |
195 | void spl_rw_fini(void); | |
196 | ||
e8b31e84 | 197 | #endif /* _SPL_RWLOCK_H */ |