]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-rwlock.c
Reimplement rwlocks for Linux lock profiling/analysis.
[mirror_spl-debian.git] / module / spl / spl-rwlock.c
1 /*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
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.
16 *
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
20 * for more details.
21 *
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.
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 #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
36
37 /*
38 * From lib/rwsem-spinlock.c but modified such that the caller is
39 * responsible for acquiring and dropping the sem->wait_lock.
40 */
41 struct rwsem_waiter {
42 struct list_head list;
43 struct task_struct *task;
44 unsigned int flags;
45 #define RWSEM_WAITING_FOR_READ 0x00000001
46 #define RWSEM_WAITING_FOR_WRITE 0x00000002
47 };
48
49 /* wake a single writer */
50 static struct rw_semaphore *
51 __rwsem_wake_one_writer_locked(struct rw_semaphore *sem)
52 {
53 struct rwsem_waiter *waiter;
54 struct task_struct *tsk;
55
56 sem->activity = -1;
57
58 waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
59 list_del(&waiter->list);
60
61 tsk = waiter->task;
62 smp_mb();
63 waiter->task = NULL;
64 wake_up_process(tsk);
65 put_task_struct(tsk);
66 return sem;
67 }
68
69 /* release a read lock on the semaphore */
70 void
71 __up_read_locked(struct rw_semaphore *sem)
72 {
73 if (--sem->activity == 0 && !list_empty(&sem->wait_list))
74 (void)__rwsem_wake_one_writer_locked(sem);
75 }
76 EXPORT_SYMBOL(__up_read_locked);
77
78 /* trylock for writing -- returns 1 if successful, 0 if contention */
79 int
80 __down_write_trylock_locked(struct rw_semaphore *sem)
81 {
82 int ret = 0;
83
84 if (sem->activity == 0 && list_empty(&sem->wait_list)) {
85 sem->activity = -1;
86 ret = 1;
87 }
88
89 return ret;
90 }
91 EXPORT_SYMBOL(__down_write_trylock_locked);
92
93 #endif