]>
git.proxmox.com Git - mirror_spl.git/blob - include/sys/rwlock.h
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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 \*****************************************************************************/
28 #include <sys/types.h>
29 #include <linux/rwsem.h>
30 #include <linux/rwsem_compat.h>
45 * If CONFIG_RWSEM_SPIN_ON_OWNER is defined, rw_semaphore will have an owner
46 * field, so we don't need our own.
49 struct rw_semaphore rw_rwlock
;
50 #ifndef CONFIG_RWSEM_SPIN_ON_OWNER
55 #endif /* CONFIG_LOCKDEP */
58 #define SEM(rwp) (&(rwp)->rw_rwlock)
61 spl_rw_set_owner(krwlock_t
*rwp
)
64 * If CONFIG_RWSEM_SPIN_ON_OWNER is defined, down_write, up_write,
65 * downgrade_write and __init_rwsem will set/clear owner for us.
67 #ifndef CONFIG_RWSEM_SPIN_ON_OWNER
68 rwp
->rw_owner
= current
;
73 spl_rw_clear_owner(krwlock_t
*rwp
)
75 #ifndef CONFIG_RWSEM_SPIN_ON_OWNER
80 static inline kthread_t
*
81 rw_owner(krwlock_t
*rwp
)
83 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
84 return SEM(rwp
)->owner
;
92 spl_rw_set_type(krwlock_t
*rwp
, krw_type_t type
)
97 spl_rw_lockdep_off_maybe(krwlock_t
*rwp
) \
99 if (rwp
&& rwp
->rw_type
== RW_NOLOCKDEP
) \
103 spl_rw_lockdep_on_maybe(krwlock_t
*rwp
) \
105 if (rwp
&& rwp
->rw_type
== RW_NOLOCKDEP
) \
108 #else /* CONFIG_LOCKDEP */
109 #define spl_rw_set_type(rwp, type)
110 #define spl_rw_lockdep_off_maybe(rwp)
111 #define spl_rw_lockdep_on_maybe(rwp)
112 #endif /* CONFIG_LOCKDEP */
115 RW_READ_HELD(krwlock_t
*rwp
)
117 return (spl_rwsem_is_locked(SEM(rwp
)) && rw_owner(rwp
) == NULL
);
121 RW_WRITE_HELD(krwlock_t
*rwp
)
123 return (rw_owner(rwp
) == current
);
127 RW_LOCK_HELD(krwlock_t
*rwp
)
129 return spl_rwsem_is_locked(SEM(rwp
));
133 * The following functions must be a #define and not static inline.
134 * This ensures that the native linux semaphore functions (down/up)
135 * will be correctly located in the users code which is important
136 * for the built in kernel lock analysis tools
138 #define rw_init(rwp, name, type, arg) \
140 static struct lock_class_key __key; \
141 ASSERT(type == RW_DEFAULT || type == RW_NOLOCKDEP); \
143 __init_rwsem(SEM(rwp), #rwp, &__key); \
144 spl_rw_clear_owner(rwp); \
145 spl_rw_set_type(rwp, type); \
148 #define rw_destroy(rwp) \
150 VERIFY(!RW_LOCK_HELD(rwp)); \
153 #define rw_tryenter(rwp, rw) \
157 spl_rw_lockdep_off_maybe(rwp); \
160 _rc_ = down_read_trylock(SEM(rwp)); \
163 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
164 spl_rw_set_owner(rwp); \
169 spl_rw_lockdep_on_maybe(rwp); \
173 #define rw_enter(rwp, rw) \
175 spl_rw_lockdep_off_maybe(rwp); \
178 down_read(SEM(rwp)); \
181 down_write(SEM(rwp)); \
182 spl_rw_set_owner(rwp); \
187 spl_rw_lockdep_on_maybe(rwp); \
190 #define rw_exit(rwp) \
192 spl_rw_lockdep_off_maybe(rwp); \
193 if (RW_WRITE_HELD(rwp)) { \
194 spl_rw_clear_owner(rwp); \
195 up_write(SEM(rwp)); \
197 ASSERT(RW_READ_HELD(rwp)); \
200 spl_rw_lockdep_on_maybe(rwp); \
203 #define rw_downgrade(rwp) \
205 spl_rw_lockdep_off_maybe(rwp); \
206 spl_rw_clear_owner(rwp); \
207 downgrade_write(SEM(rwp)); \
208 spl_rw_lockdep_on_maybe(rwp); \
212 * This implementation of rw_tryupgrade() behaves slightly differently
213 * from its counterparts on other platforms. It drops the RW_READER lock
214 * and then acquires the RW_WRITER lock leaving a small window where no
215 * lock is held. On other platforms the lock is never released during
216 * the upgrade process. This is necessary under Linux because the kernel
217 * does not provide an upgrade function.
219 #define rw_tryupgrade(rwp) \
223 if (RW_WRITE_HELD(rwp)) { \
226 spl_rw_lockdep_off_maybe(rwp); \
227 if ((_rc_ = rwsem_tryupgrade(SEM(rwp)))) \
228 spl_rw_set_owner(rwp); \
229 spl_rw_lockdep_on_maybe(rwp); \
234 int spl_rw_init(void);
235 void spl_rw_fini(void);
237 #endif /* _SPL_RWLOCK_H */