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.
10 * The SPL is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * The SPL is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
28 #include <linux/rwsem.h>
29 #include <linux/sched.h>
44 struct rw_semaphore rw_rwlock;
48 #endif /* CONFIG_LOCKDEP */
51 #define SEM(rwp) (&(rwp)->rw_rwlock)
54 spl_rw_set_owner(krwlock_t *rwp)
56 rwp->rw_owner = current;
60 spl_rw_clear_owner(krwlock_t *rwp)
65 static inline kthread_t *
66 rw_owner(krwlock_t *rwp)
68 return (rwp->rw_owner);
73 spl_rw_set_type(krwlock_t *rwp, krw_type_t type)
78 spl_rw_lockdep_off_maybe(krwlock_t *rwp) \
80 if (rwp && rwp->rw_type == RW_NOLOCKDEP) \
84 spl_rw_lockdep_on_maybe(krwlock_t *rwp) \
86 if (rwp && rwp->rw_type == RW_NOLOCKDEP) \
89 #else /* CONFIG_LOCKDEP */
90 #define spl_rw_set_type(rwp, type)
91 #define spl_rw_lockdep_off_maybe(rwp)
92 #define spl_rw_lockdep_on_maybe(rwp)
93 #endif /* CONFIG_LOCKDEP */
96 RW_LOCK_HELD(krwlock_t *rwp)
98 return (rwsem_is_locked(SEM(rwp)));
102 RW_WRITE_HELD(krwlock_t *rwp)
104 return (rw_owner(rwp) == current);
108 RW_READ_HELD(krwlock_t *rwp)
110 return (RW_LOCK_HELD(rwp) && rw_owner(rwp) == NULL);
114 * The following functions must be a #define and not static inline.
115 * This ensures that the native linux semaphore functions (down/up)
116 * will be correctly located in the users code which is important
117 * for the built in kernel lock analysis tools
120 #define rw_init(rwp, name, type, arg) \
122 static struct lock_class_key __key; \
123 ASSERT(type == RW_DEFAULT || type == RW_NOLOCKDEP); \
125 __init_rwsem(SEM(rwp), #rwp, &__key); \
126 spl_rw_clear_owner(rwp); \
127 spl_rw_set_type(rwp, type); \
131 * The Linux rwsem implementation does not require a matching destroy.
133 #define rw_destroy(rwp) ((void) 0)
136 * Upgrading a rwsem from a reader to a writer is not supported by the
137 * Linux kernel. The lock must be dropped and reacquired as a writer.
139 #define rw_tryupgrade(rwp) RW_WRITE_HELD(rwp)
141 #define rw_tryenter(rwp, rw) \
145 spl_rw_lockdep_off_maybe(rwp); \
148 _rc_ = down_read_trylock(SEM(rwp)); \
151 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
152 spl_rw_set_owner(rwp); \
157 spl_rw_lockdep_on_maybe(rwp); \
161 #define rw_enter(rwp, rw) \
163 spl_rw_lockdep_off_maybe(rwp); \
166 down_read(SEM(rwp)); \
169 down_write(SEM(rwp)); \
170 spl_rw_set_owner(rwp); \
175 spl_rw_lockdep_on_maybe(rwp); \
178 #define rw_exit(rwp) \
180 spl_rw_lockdep_off_maybe(rwp); \
181 if (RW_WRITE_HELD(rwp)) { \
182 spl_rw_clear_owner(rwp); \
183 up_write(SEM(rwp)); \
185 ASSERT(RW_READ_HELD(rwp)); \
188 spl_rw_lockdep_on_maybe(rwp); \
191 #define rw_downgrade(rwp) \
193 spl_rw_lockdep_off_maybe(rwp); \
194 spl_rw_clear_owner(rwp); \
195 downgrade_write(SEM(rwp)); \
196 spl_rw_lockdep_on_maybe(rwp); \
200 #endif /* _SPL_RWLOCK_H */