]>
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 BB |
8 | * This file is part of the SPL, Solaris Porting Layer. |
9 | * For details, see <http://github.com/behlendorf/spl/>. | |
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 | { | |
53 | unsigned long flags; | |
54 | ||
55 | spin_lock_irqsave(&SEM(rwp)->wait_lock, flags); | |
56 | rwp->rw_owner = current; | |
57 | spin_unlock_irqrestore(&SEM(rwp)->wait_lock, flags); | |
58 | } | |
59 | ||
60 | static inline void | |
61 | spl_rw_clear_owner(krwlock_t *rwp) | |
62 | { | |
63 | unsigned long flags; | |
64 | ||
65 | spin_lock_irqsave(&SEM(rwp)->wait_lock, flags); | |
66 | rwp->rw_owner = NULL; | |
67 | spin_unlock_irqrestore(&SEM(rwp)->wait_lock, flags); | |
68 | } | |
69 | ||
70 | static inline kthread_t * | |
71 | rw_owner(krwlock_t *rwp) | |
72 | { | |
73 | unsigned long flags; | |
74 | kthread_t *owner; | |
75 | ||
76 | spin_lock_irqsave(&SEM(rwp)->wait_lock, flags); | |
46aa7b39 | 77 | owner = rwp->rw_owner; |
d28db80f BB |
78 | spin_unlock_irqrestore(&SEM(rwp)->wait_lock, flags); |
79 | ||
80 | return owner; | |
81 | } | |
82 | ||
83 | static inline int | |
84 | RW_READ_HELD(krwlock_t *rwp) | |
85 | { | |
46aa7b39 NB |
86 | return (spl_rwsem_is_locked(SEM(rwp)) && |
87 | rw_owner(rwp) == NULL); | |
d28db80f BB |
88 | } |
89 | ||
90 | static inline int | |
91 | RW_WRITE_HELD(krwlock_t *rwp) | |
92 | { | |
46aa7b39 NB |
93 | return (spl_rwsem_is_locked(SEM(rwp)) && |
94 | rw_owner(rwp) == current); | |
d28db80f BB |
95 | } |
96 | ||
97 | static inline int | |
98 | RW_LOCK_HELD(krwlock_t *rwp) | |
99 | { | |
46aa7b39 | 100 | return spl_rwsem_is_locked(SEM(rwp)); |
d28db80f BB |
101 | } |
102 | ||
103 | /* | |
104 | * The following functions must be a #define and not static inline. | |
105 | * This ensures that the native linux semaphore functions (down/up) | |
106 | * will be correctly located in the users code which is important | |
107 | * for the built in kernel lock analysis tools | |
108 | */ | |
109 | #define rw_init(rwp, name, type, arg) \ | |
110 | ({ \ | |
0e77fc11 BB |
111 | static struct lock_class_key __key; \ |
112 | \ | |
113 | __init_rwsem(SEM(rwp), #rwp, &__key); \ | |
d28db80f | 114 | spl_rw_clear_owner(rwp); \ |
e811949a | 115 | }) |
f1ca4da6 | 116 | |
d28db80f BB |
117 | #define rw_destroy(rwp) \ |
118 | ({ \ | |
119 | VERIFY(!RW_LOCK_HELD(rwp)); \ | |
e811949a | 120 | }) |
ed61a7d0 | 121 | |
d28db80f BB |
122 | #define rw_tryenter(rwp, rw) \ |
123 | ({ \ | |
124 | int _rc_ = 0; \ | |
125 | \ | |
126 | switch (rw) { \ | |
127 | case RW_READER: \ | |
128 | _rc_ = down_read_trylock(SEM(rwp)); \ | |
129 | break; \ | |
130 | case RW_WRITER: \ | |
131 | if ((_rc_ = down_write_trylock(SEM(rwp)))) \ | |
132 | spl_rw_set_owner(rwp); \ | |
133 | break; \ | |
134 | default: \ | |
55abb092 | 135 | VERIFY(0); \ |
d28db80f BB |
136 | } \ |
137 | _rc_; \ | |
e8b31e84 | 138 | }) |
e8b31e84 | 139 | |
d28db80f BB |
140 | #define rw_enter(rwp, rw) \ |
141 | ({ \ | |
142 | switch (rw) { \ | |
143 | case RW_READER: \ | |
144 | down_read(SEM(rwp)); \ | |
145 | break; \ | |
146 | case RW_WRITER: \ | |
147 | down_write(SEM(rwp)); \ | |
148 | spl_rw_set_owner(rwp); \ | |
149 | break; \ | |
150 | default: \ | |
55abb092 | 151 | VERIFY(0); \ |
d28db80f | 152 | } \ |
e811949a | 153 | }) |
f1ca4da6 | 154 | |
d28db80f BB |
155 | #define rw_exit(rwp) \ |
156 | ({ \ | |
157 | if (RW_WRITE_HELD(rwp)) { \ | |
158 | spl_rw_clear_owner(rwp); \ | |
159 | up_write(SEM(rwp)); \ | |
160 | } else { \ | |
161 | ASSERT(RW_READ_HELD(rwp)); \ | |
162 | up_read(SEM(rwp)); \ | |
163 | } \ | |
164 | }) | |
165 | ||
166 | #define rw_downgrade(rwp) \ | |
167 | ({ \ | |
168 | spl_rw_clear_owner(rwp); \ | |
169 | downgrade_write(SEM(rwp)); \ | |
170 | }) | |
171 | ||
ef6c1368 | 172 | #if defined(CONFIG_RWSEM_GENERIC_SPINLOCK) |
32f5faff BB |
173 | /* |
174 | * For the generic implementations of rw-semaphores the following is | |
175 | * true. If your semaphore implementation internally represents the | |
176 | * semaphore state differently then special case handling is required. | |
177 | * - if activity/count is 0 then there are no active readers or writers | |
178 | * - if activity/count is +ve then that is the number of active readers | |
179 | * - if activity/count is -1 then there is one active writer | |
180 | */ | |
181 | ||
182 | extern void __up_read_locked(struct rw_semaphore *); | |
183 | extern int __down_write_trylock_locked(struct rw_semaphore *); | |
184 | ||
d28db80f BB |
185 | #define rw_tryupgrade(rwp) \ |
186 | ({ \ | |
187 | unsigned long _flags_; \ | |
188 | int _rc_ = 0; \ | |
189 | \ | |
190 | spin_lock_irqsave(&SEM(rwp)->wait_lock, _flags_); \ | |
32f5faff BB |
191 | if ((list_empty(&SEM(rwp)->wait_list)) && \ |
192 | (SEM(rwp)->activity == 1)) { \ | |
193 | __up_read_locked(SEM(rwp)); \ | |
194 | VERIFY(_rc_ = __down_write_trylock_locked(SEM(rwp))); \ | |
d28db80f BB |
195 | (rwp)->rw_owner = current; \ |
196 | } \ | |
197 | spin_unlock_irqrestore(&SEM(rwp)->wait_lock, _flags_); \ | |
198 | _rc_; \ | |
199 | }) | |
ef6c1368 BB |
200 | #else |
201 | /* | |
32f5faff BB |
202 | * rw_tryupgrade() can be implemented correctly but for each supported |
203 | * arch we will need a custom implementation. For the x86 implementation | |
204 | * it looks like a custom cmpxchg() to atomically check and promote the | |
205 | * rwsem would be safe. For now that's not worth the trouble so in this | |
206 | * case rw_tryupgrade() has just been disabled. | |
ef6c1368 BB |
207 | */ |
208 | #define rw_tryupgrade(rwp) ({ 0; }) | |
209 | #endif | |
d28db80f BB |
210 | |
211 | int spl_rw_init(void); | |
212 | void spl_rw_fini(void); | |
213 | ||
e8b31e84 | 214 | #endif /* _SPL_RWLOCK_H */ |