cleanup
[linux-2.4.21-pre4.git] / lib / brlock.c
1 /*
2  *
3  * linux/lib/brlock.c
4  *
5  * 'Big Reader' read-write spinlocks.  See linux/brlock.h for details.
6  *
7  * Copyright 2000, Ingo Molnar <mingo@redhat.com>
8  * Copyright 2000, David S. Miller <davem@redhat.com>
9  */
10
11 #include <linux/config.h>
12
13 #ifdef CONFIG_SMP
14
15 #include <linux/sched.h>
16 #include <linux/brlock.h>
17 #include <linux/delay.h>
18
19 #ifdef __BRLOCK_USE_ATOMICS
20
21 brlock_read_lock_t __brlock_array[NR_CPUS][__BR_IDX_MAX] =
22    { [0 ... NR_CPUS-1] = { [0 ... __BR_IDX_MAX-1] = RW_LOCK_UNLOCKED } };
23
24 void __br_write_lock (enum brlock_indices idx)
25 {
26         int i;
27
28         for (i = 0; i < smp_num_cpus; i++)
29                 write_lock(&__brlock_array[cpu_logical_map(i)][idx]);
30 }
31
32 void __br_write_unlock (enum brlock_indices idx)
33 {
34         int i;
35
36         for (i = 0; i < smp_num_cpus; i++)
37                 write_unlock(&__brlock_array[cpu_logical_map(i)][idx]);
38 }
39
40 #else /* ! __BRLOCK_USE_ATOMICS */
41
42 brlock_read_lock_t __brlock_array[NR_CPUS][__BR_IDX_MAX] =
43    { [0 ... NR_CPUS-1] = { [0 ... __BR_IDX_MAX-1] = 0 } };
44
45 struct br_wrlock __br_write_locks[__BR_IDX_MAX] =
46    { [0 ... __BR_IDX_MAX-1] = { SPIN_LOCK_UNLOCKED } };
47
48 void __br_write_lock (enum brlock_indices idx)
49 {
50         int i;
51
52 again:
53         spin_lock(&__br_write_locks[idx].lock);
54         for (i = 0; i < smp_num_cpus; i++)
55                 if (__brlock_array[cpu_logical_map(i)][idx] != 0) {
56                         spin_unlock(&__br_write_locks[idx].lock);
57                         barrier();
58                         cpu_relax();
59                         goto again;
60                 }
61 }
62
63 void __br_write_unlock (enum brlock_indices idx)
64 {
65         spin_unlock(&__br_write_locks[idx].lock);
66 }
67
68 #endif /* __BRLOCK_USE_ATOMICS */
69
70 #endif /* CONFIG_SMP */