]>
git.proxmox.com Git - mirror_spl-debian.git/blob - include/sys/rwlock.h
2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2009 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
31 #include <linux/rwsem.h>
45 struct rw_semaphore rw_rwlock
;
50 * For the generic implementations of rw-semaphores the following is
51 * true. If your semaphore implementation internally represents the
52 * semaphore state differently. Then special case handling will be
53 * required so RW_COUNT() provides these semantics:
54 * - if activity/count is 0 then there are no active readers or writers
55 * - if activity/count is +ve then that is the number of active readers
56 * - if activity/count is -1 then there is one active writer
58 #define SEM(rwp) ((struct rw_semaphore *)(rwp))
60 #if defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
61 # define RW_COUNT(rwp) (SEM(rwp)->activity)
62 # define rw_exit_locked(rwp) __up_read_locked(rwp)
63 # define rw_tryenter_locked(rwp) __down_write_trylock_locked(rwp)
64 extern void __up_read_locked(struct rw_semaphore
*);
65 extern int __down_write_trylock_locked(struct rw_semaphore
*);
68 # define RW_COUNT(rwp) ((SEM(rwp)->count < 0) ? (-1) : \
69 (SEM(rwp)->count & RWSEM_ACTIVE_MASK))
71 # define RW_COUNT(rwp) (SEM(rwp)->count & RWSEM_ACTIVE_MASK)
73 # define rw_exit_locked(rwp) up_read(rwp)
74 # define rw_tryenter_locked(rwp) down_write_trylock(rwp)
77 static inline kthread_t
*
78 spl_rw_get_owner(krwlock_t
*rwp
)
84 spl_rw_set_owner(krwlock_t
*rwp
)
88 spin_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
89 rwp
->rw_owner
= current
;
90 spin_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
94 spl_rw_clear_owner(krwlock_t
*rwp
)
98 spin_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
100 spin_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
103 static inline kthread_t
*
104 rw_owner(krwlock_t
*rwp
)
109 spin_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
110 owner
= spl_rw_get_owner(rwp
);
111 spin_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
117 RW_READ_HELD(krwlock_t
*rwp
)
122 spin_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
123 rc
= ((RW_COUNT(rwp
) > 0) && (spl_rw_get_owner(rwp
) == NULL
));
124 spin_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
130 RW_WRITE_HELD(krwlock_t
*rwp
)
135 spin_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
136 rc
= ((RW_COUNT(rwp
) < 0) && (spl_rw_get_owner(rwp
) == current
));
137 spin_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
143 RW_LOCK_HELD(krwlock_t
*rwp
)
148 spin_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
149 rc
= (RW_COUNT(rwp
) != 0);
150 spin_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
156 * The following functions must be a #define and not static inline.
157 * This ensures that the native linux semaphore functions (down/up)
158 * will be correctly located in the users code which is important
159 * for the built in kernel lock analysis tools
161 #define rw_init(rwp, name, type, arg) \
163 static struct lock_class_key __key; \
165 __init_rwsem(SEM(rwp), #rwp, &__key); \
166 spl_rw_clear_owner(rwp); \
169 #define rw_destroy(rwp) \
171 VERIFY(!RW_LOCK_HELD(rwp)); \
174 #define rw_tryenter(rwp, rw) \
180 _rc_ = down_read_trylock(SEM(rwp)); \
183 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
184 spl_rw_set_owner(rwp); \
192 #define rw_enter(rwp, rw) \
196 down_read(SEM(rwp)); \
199 down_write(SEM(rwp)); \
200 spl_rw_set_owner(rwp); \
207 #define rw_exit(rwp) \
209 if (RW_WRITE_HELD(rwp)) { \
210 spl_rw_clear_owner(rwp); \
211 up_write(SEM(rwp)); \
213 ASSERT(RW_READ_HELD(rwp)); \
218 #define rw_downgrade(rwp) \
220 spl_rw_clear_owner(rwp); \
221 downgrade_write(SEM(rwp)); \
224 #define rw_tryupgrade(rwp) \
226 unsigned long _flags_; \
229 spin_lock_irqsave(&SEM(rwp)->wait_lock, _flags_); \
230 if (list_empty(&SEM(rwp)->wait_list) && (RW_COUNT(rwp) == 1)) { \
231 rw_exit_locked(SEM(rwp)); \
232 VERIFY(_rc_ = rw_tryenter_locked(SEM(rwp))); \
233 (rwp)->rw_owner = current; \
235 spin_unlock_irqrestore(&SEM(rwp)->wait_lock, _flags_); \
239 int spl_rw_init(void);
240 void spl_rw_fini(void);
242 #endif /* _SPL_RWLOCK_H */