]> git.proxmox.com Git - mirror_spl.git/blame - module/spl/spl-rwlock.c
Implement a proper rw_tryupgrade
[mirror_spl.git] / module / spl / spl-rwlock.c
CommitLineData
716154c5
BB
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>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
715f6251 10 *
716154c5
BB
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.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Reader/Writer Lock Implementation.
25\*****************************************************************************/
715f6251 26
f4b37741 27#include <sys/rwlock.h>
f1ca4da6 28
937879f1 29#ifdef DEBUG_SUBSYSTEM
30#undef DEBUG_SUBSYSTEM
31#endif
32
33#define DEBUG_SUBSYSTEM S_RWLOCK
34
f58040c0
CC
35#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
36static int
37__rwsem_tryupgrade(struct rw_semaphore *rwsem)
38{
39 int ret = 0;
40 unsigned long flags;
41 spl_rwsem_lock_irqsave(&rwsem->wait_lock, flags);
42 if (RWSEM_COUNT(rwsem) == SPL_RWSEM_SINGLE_READER_VALUE &&
43 list_empty(&rwsem->wait_list)) {
44 ret = 1;
45 RWSEM_COUNT(rwsem) = SPL_RWSEM_SINGLE_WRITER_VALUE;
46 }
47 spl_rwsem_unlock_irqrestore(&rwsem->wait_lock, flags);
48 return (ret);
49}
50#else
51static int
52__rwsem_tryupgrade(struct rw_semaphore *rwsem)
53{
54 typeof (rwsem->count) val;
55 val = cmpxchg(&rwsem->count, SPL_RWSEM_SINGLE_READER_VALUE,
56 SPL_RWSEM_SINGLE_WRITER_VALUE);
57 return (val == SPL_RWSEM_SINGLE_READER_VALUE);
58}
59#endif
60
61int
62rwsem_tryupgrade(struct rw_semaphore *rwsem)
63{
64 if (__rwsem_tryupgrade(rwsem)) {
65 rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
66 rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
67#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
68 rwsem->owner = current;
69#endif
70 return (1);
71 }
72 return (0);
73}
74EXPORT_SYMBOL(rwsem_tryupgrade);
75
d28db80f
BB
76int spl_rw_init(void) { return 0; }
77void spl_rw_fini(void) { }