]>
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>
44 struct rw_semaphore rw_rwlock
;
48 #define SEM(rwp) ((struct rw_semaphore *)(rwp))
51 spl_rw_set_owner(krwlock_t
*rwp
)
53 rwp
->rw_owner
= current
;
57 spl_rw_clear_owner(krwlock_t
*rwp
)
62 static inline kthread_t
*
63 rw_owner(krwlock_t
*rwp
)
69 RW_READ_HELD(krwlock_t
*rwp
)
71 return (spl_rwsem_is_locked(SEM(rwp
)) && rw_owner(rwp
) == NULL
);
75 RW_WRITE_HELD(krwlock_t
*rwp
)
77 return (rw_owner(rwp
) == current
);
81 RW_LOCK_HELD(krwlock_t
*rwp
)
83 return spl_rwsem_is_locked(SEM(rwp
));
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
92 #define rw_init(rwp, name, type, arg) \
94 static struct lock_class_key __key; \
96 __init_rwsem(SEM(rwp), #rwp, &__key); \
97 spl_rw_clear_owner(rwp); \
100 #define rw_destroy(rwp) \
102 VERIFY(!RW_LOCK_HELD(rwp)); \
105 #define rw_tryenter(rwp, rw) \
111 _rc_ = down_read_trylock(SEM(rwp)); \
114 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
115 spl_rw_set_owner(rwp); \
123 #define rw_enter(rwp, rw) \
127 down_read(SEM(rwp)); \
130 down_write(SEM(rwp)); \
131 spl_rw_set_owner(rwp); \
138 #define rw_exit(rwp) \
140 if (RW_WRITE_HELD(rwp)) { \
141 spl_rw_clear_owner(rwp); \
142 up_write(SEM(rwp)); \
144 ASSERT(RW_READ_HELD(rwp)); \
149 #define rw_downgrade(rwp) \
151 spl_rw_clear_owner(rwp); \
152 downgrade_write(SEM(rwp)); \
155 #if defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
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
165 extern void __up_read_locked(struct rw_semaphore
*);
166 extern int __down_write_trylock_locked(struct rw_semaphore
*);
168 #define rw_tryupgrade(rwp) \
170 unsigned long _flags_; \
173 spl_rwsem_lock_irqsave(&SEM(rwp)->wait_lock, _flags_); \
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))); \
178 (rwp)->rw_owner = current; \
180 spl_rwsem_unlock_irqrestore(&SEM(rwp)->wait_lock, _flags_); \
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.
191 #define rw_tryupgrade(rwp) ({ 0; })
194 int spl_rw_init(void);
195 void spl_rw_fini(void);
197 #endif /* _SPL_RWLOCK_H */