]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-rwlock.c
9e96c4f27ec01593ccf8d348b590ee095a189dd7
[mirror_spl-debian.git] / module / spl / spl-rwlock.c
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>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
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
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
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Reader/Writer Lock Implementation.
25 \*****************************************************************************/
26
27 #include <sys/rwlock.h>
28
29 #ifdef DEBUG_SUBSYSTEM
30 #undef DEBUG_SUBSYSTEM
31 #endif
32
33 #define DEBUG_SUBSYSTEM S_RWLOCK
34
35 #if defined(CONFIG_PREEMPT_RT_FULL)
36
37 #include <linux/rtmutex.h>
38
39 static int
40 __rwsem_tryupgrade(struct rw_semaphore *rwsem)
41 {
42 ASSERT(rt_mutex_owner(&rwsem->lock) == current);
43
44 /*
45 * Under the realtime patch series, rwsem is implemented as a
46 * single mutex held by readers and writers alike. However,
47 * this implementation would prevent a thread from taking a
48 * read lock twice, as the mutex would already be locked on
49 * the second attempt. Therefore the implementation allows a
50 * single thread to take a rwsem as read lock multiple times
51 * tracking that nesting as read_depth counter.
52 */
53 if (rwsem->read_depth <= 1) {
54 /*
55 * In case, the current thread has not taken the lock
56 * more than once as read lock, we can allow an
57 * upgrade to a write lock. rwsem_rt.h implements
58 * write locks as read_depth == 0.
59 */
60 rwsem->read_depth = 0;
61 return (1);
62 }
63 return (0);
64 }
65 #elif defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
66 static int
67 __rwsem_tryupgrade(struct rw_semaphore *rwsem)
68 {
69 int ret = 0;
70 unsigned long flags;
71 spl_rwsem_lock_irqsave(&rwsem->wait_lock, flags);
72 if (RWSEM_COUNT(rwsem) == SPL_RWSEM_SINGLE_READER_VALUE &&
73 list_empty(&rwsem->wait_list)) {
74 ret = 1;
75 RWSEM_COUNT(rwsem) = SPL_RWSEM_SINGLE_WRITER_VALUE;
76 }
77 spl_rwsem_unlock_irqrestore(&rwsem->wait_lock, flags);
78 return (ret);
79 }
80 #elif defined(HAVE_RWSEM_ATOMIC_LONG_COUNT)
81 static int
82 __rwsem_tryupgrade(struct rw_semaphore *rwsem)
83 {
84 long val;
85 val = atomic_long_cmpxchg(&rwsem->count, SPL_RWSEM_SINGLE_READER_VALUE,
86 SPL_RWSEM_SINGLE_WRITER_VALUE);
87 return (val == SPL_RWSEM_SINGLE_READER_VALUE);
88 }
89 #else
90 static int
91 __rwsem_tryupgrade(struct rw_semaphore *rwsem)
92 {
93 typeof (rwsem->count) val;
94 val = cmpxchg(&rwsem->count, SPL_RWSEM_SINGLE_READER_VALUE,
95 SPL_RWSEM_SINGLE_WRITER_VALUE);
96 return (val == SPL_RWSEM_SINGLE_READER_VALUE);
97 }
98 #endif
99
100 int
101 rwsem_tryupgrade(struct rw_semaphore *rwsem)
102 {
103 if (__rwsem_tryupgrade(rwsem)) {
104 rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
105 rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
106 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
107 rwsem->owner = current;
108 #endif
109 return (1);
110 }
111 return (0);
112 }
113 EXPORT_SYMBOL(rwsem_tryupgrade);
114
115 int spl_rw_init(void) { return 0; }
116 void spl_rw_fini(void) { }