]>
git.proxmox.com Git - mirror_spl-debian.git/blob - include/sys/rwlock.h
c8ed223d4461c4b2ac7d1921aeec712c13fadf66
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
)
55 spl_rwsem_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
56 rwp
->rw_owner
= current
;
57 spl_rwsem_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
61 spl_rw_clear_owner(krwlock_t
*rwp
)
65 spl_rwsem_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
67 spl_rwsem_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
70 static inline kthread_t
*
71 rw_owner(krwlock_t
*rwp
)
76 spl_rwsem_lock_irqsave(&SEM(rwp
)->wait_lock
, flags
);
77 owner
= rwp
->rw_owner
;
78 spl_rwsem_unlock_irqrestore(&SEM(rwp
)->wait_lock
, flags
);
84 RW_READ_HELD(krwlock_t
*rwp
)
87 * Linux 4.8 will set owner to 1 when read held instead of leave it
88 * NULL. So we check whether owner <= 1.
90 return (spl_rwsem_is_locked(SEM(rwp
)) &&
91 (unsigned long)rw_owner(rwp
) <= 1);
95 RW_WRITE_HELD(krwlock_t
*rwp
)
97 return (spl_rwsem_is_locked(SEM(rwp
)) && rw_owner(rwp
) == current
);
101 RW_LOCK_HELD(krwlock_t
*rwp
)
103 return spl_rwsem_is_locked(SEM(rwp
));
107 * The following functions must be a #define and not static inline.
108 * This ensures that the native linux semaphore functions (down/up)
109 * will be correctly located in the users code which is important
110 * for the built in kernel lock analysis tools
112 #define rw_init(rwp, name, type, arg) \
114 static struct lock_class_key __key; \
116 __init_rwsem(SEM(rwp), #rwp, &__key); \
117 spl_rw_clear_owner(rwp); \
120 #define rw_destroy(rwp) \
122 VERIFY(!RW_LOCK_HELD(rwp)); \
125 #define rw_tryenter(rwp, rw) \
131 _rc_ = down_read_trylock(SEM(rwp)); \
134 if ((_rc_ = down_write_trylock(SEM(rwp)))) \
135 spl_rw_set_owner(rwp); \
143 #define rw_enter(rwp, rw) \
147 down_read(SEM(rwp)); \
150 down_write(SEM(rwp)); \
151 spl_rw_set_owner(rwp); \
158 #define rw_exit(rwp) \
160 if (RW_WRITE_HELD(rwp)) { \
161 spl_rw_clear_owner(rwp); \
162 up_write(SEM(rwp)); \
164 ASSERT(RW_READ_HELD(rwp)); \
169 #define rw_downgrade(rwp) \
171 spl_rw_clear_owner(rwp); \
172 downgrade_write(SEM(rwp)); \
176 * This implementation of rw_tryupgrade() behaves slightly differently
177 * from its counterparts on other platforms. It drops the RW_READER lock
178 * and then acquires the RW_WRITER lock leaving a small window where no
179 * lock is held. On other platforms the lock is never released during
180 * the upgrade process. This is necessary under Linux because the kernel
181 * does not provide an upgrade function.
183 #define rw_tryupgrade(rwp) \
187 if (RW_WRITE_HELD(rwp)) { \
190 if ((_rc_ = rwsem_tryupgrade(SEM(rwp)))) \
191 spl_rw_set_owner(rwp); \
196 int spl_rw_init(void);
197 void spl_rw_fini(void);
199 #endif /* _SPL_RWLOCK_H */