diff options
author | Matthew Wilcox <matthew@wil.cx> | 2008-03-07 21:55:58 -0500 |
---|---|---|
committer | Matthew Wilcox <willy@linux.intel.com> | 2008-04-17 10:42:34 -0400 |
commit | 64ac24e738823161693bf791f87adc802cf529ff (patch) | |
tree | 19c0b0cf314d4394ca580c05b86cdf874ce0a167 /include/asm-cris | |
parent | e48b3deee475134585eed03e7afebe4bf9e0dba9 (diff) |
Generic semaphore implementation
Semaphores are no longer performance-critical, so a generic C
implementation is better for maintainability, debuggability and
extensibility. Thanks to Peter Zijlstra for fixing the lockdep
warning. Thanks to Harvey Harrison for pointing out that the
unlikely() was unnecessary.
Signed-off-by: Matthew Wilcox <willy@linux.intel.com>
Acked-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'include/asm-cris')
-rw-r--r-- | include/asm-cris/semaphore-helper.h | 78 | ||||
-rw-r--r-- | include/asm-cris/semaphore.h | 134 |
2 files changed, 1 insertions, 211 deletions
diff --git a/include/asm-cris/semaphore-helper.h b/include/asm-cris/semaphore-helper.h deleted file mode 100644 index 27bfeca1b981..000000000000 --- a/include/asm-cris/semaphore-helper.h +++ /dev/null | |||
@@ -1,78 +0,0 @@ | |||
1 | /* $Id: semaphore-helper.h,v 1.3 2001/03/26 15:00:33 orjanf Exp $ | ||
2 | * | ||
3 | * SMP- and interrupt-safe semaphores helper functions. Generic versions, no | ||
4 | * optimizations whatsoever... | ||
5 | * | ||
6 | */ | ||
7 | |||
8 | #ifndef _ASM_SEMAPHORE_HELPER_H | ||
9 | #define _ASM_SEMAPHORE_HELPER_H | ||
10 | |||
11 | #include <asm/atomic.h> | ||
12 | #include <linux/errno.h> | ||
13 | |||
14 | #define read(a) ((a)->counter) | ||
15 | #define inc(a) (((a)->counter)++) | ||
16 | #define dec(a) (((a)->counter)--) | ||
17 | |||
18 | #define count_inc(a) ((*(a))++) | ||
19 | |||
20 | /* | ||
21 | * These two _must_ execute atomically wrt each other. | ||
22 | */ | ||
23 | static inline void wake_one_more(struct semaphore * sem) | ||
24 | { | ||
25 | atomic_inc(&sem->waking); | ||
26 | } | ||
27 | |||
28 | static inline int waking_non_zero(struct semaphore *sem) | ||
29 | { | ||
30 | unsigned long flags; | ||
31 | int ret = 0; | ||
32 | |||
33 | local_irq_save(flags); | ||
34 | if (read(&sem->waking) > 0) { | ||
35 | dec(&sem->waking); | ||
36 | ret = 1; | ||
37 | } | ||
38 | local_irq_restore(flags); | ||
39 | return ret; | ||
40 | } | ||
41 | |||
42 | static inline int waking_non_zero_interruptible(struct semaphore *sem, | ||
43 | struct task_struct *tsk) | ||
44 | { | ||
45 | int ret = 0; | ||
46 | unsigned long flags; | ||
47 | |||
48 | local_irq_save(flags); | ||
49 | if (read(&sem->waking) > 0) { | ||
50 | dec(&sem->waking); | ||
51 | ret = 1; | ||
52 | } else if (signal_pending(tsk)) { | ||
53 | inc(&sem->count); | ||
54 | ret = -EINTR; | ||
55 | } | ||
56 | local_irq_restore(flags); | ||
57 | return ret; | ||
58 | } | ||
59 | |||
60 | static inline int waking_non_zero_trylock(struct semaphore *sem) | ||
61 | { | ||
62 | int ret = 1; | ||
63 | unsigned long flags; | ||
64 | |||
65 | local_irq_save(flags); | ||
66 | if (read(&sem->waking) <= 0) | ||
67 | inc(&sem->count); | ||
68 | else { | ||
69 | dec(&sem->waking); | ||
70 | ret = 0; | ||
71 | } | ||
72 | local_irq_restore(flags); | ||
73 | return ret; | ||
74 | } | ||
75 | |||
76 | #endif /* _ASM_SEMAPHORE_HELPER_H */ | ||
77 | |||
78 | |||
diff --git a/include/asm-cris/semaphore.h b/include/asm-cris/semaphore.h index 31a4ac448195..d9b2034ed1d2 100644 --- a/include/asm-cris/semaphore.h +++ b/include/asm-cris/semaphore.h | |||
@@ -1,133 +1 @@ | |||
1 | /* $Id: semaphore.h,v 1.3 2001/05/08 13:54:09 bjornw Exp $ */ | #include <linux/semaphore.h> | |
2 | |||
3 | /* On the i386 these are coded in asm, perhaps we should as well. Later.. */ | ||
4 | |||
5 | #ifndef _CRIS_SEMAPHORE_H | ||
6 | #define _CRIS_SEMAPHORE_H | ||
7 | |||
8 | #define RW_LOCK_BIAS 0x01000000 | ||
9 | |||
10 | #include <linux/wait.h> | ||
11 | #include <linux/spinlock.h> | ||
12 | #include <linux/rwsem.h> | ||
13 | |||
14 | #include <asm/system.h> | ||
15 | #include <asm/atomic.h> | ||
16 | |||
17 | /* | ||
18 | * CRIS semaphores, implemented in C-only so far. | ||
19 | */ | ||
20 | |||
21 | struct semaphore { | ||
22 | atomic_t count; | ||
23 | atomic_t waking; | ||
24 | wait_queue_head_t wait; | ||
25 | }; | ||
26 | |||
27 | #define __SEMAPHORE_INITIALIZER(name, n) \ | ||
28 | { \ | ||
29 | .count = ATOMIC_INIT(n), \ | ||
30 | .waking = ATOMIC_INIT(0), \ | ||
31 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ | ||
32 | } | ||
33 | |||
34 | #define __DECLARE_SEMAPHORE_GENERIC(name,count) \ | ||
35 | struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) | ||
36 | |||
37 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1) | ||
38 | |||
39 | static inline void sema_init(struct semaphore *sem, int val) | ||
40 | { | ||
41 | *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val); | ||
42 | } | ||
43 | |||
44 | static inline void init_MUTEX (struct semaphore *sem) | ||
45 | { | ||
46 | sema_init(sem, 1); | ||
47 | } | ||
48 | |||
49 | static inline void init_MUTEX_LOCKED (struct semaphore *sem) | ||
50 | { | ||
51 | sema_init(sem, 0); | ||
52 | } | ||
53 | |||
54 | extern void __down(struct semaphore * sem); | ||
55 | extern int __down_interruptible(struct semaphore * sem); | ||
56 | extern int __down_trylock(struct semaphore * sem); | ||
57 | extern void __up(struct semaphore * sem); | ||
58 | |||
59 | /* notice - we probably can do cli/sti here instead of saving */ | ||
60 | |||
61 | static inline void down(struct semaphore * sem) | ||
62 | { | ||
63 | unsigned long flags; | ||
64 | int failed; | ||
65 | |||
66 | might_sleep(); | ||
67 | |||
68 | /* atomically decrement the semaphores count, and if its negative, we wait */ | ||
69 | cris_atomic_save(sem, flags); | ||
70 | failed = --(sem->count.counter) < 0; | ||
71 | cris_atomic_restore(sem, flags); | ||
72 | if(failed) { | ||
73 | __down(sem); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | /* | ||
78 | * This version waits in interruptible state so that the waiting | ||
79 | * process can be killed. The down_interruptible routine | ||
80 | * returns negative for signalled and zero for semaphore acquired. | ||
81 | */ | ||
82 | |||
83 | static inline int down_interruptible(struct semaphore * sem) | ||
84 | { | ||
85 | unsigned long flags; | ||
86 | int failed; | ||
87 | |||
88 | might_sleep(); | ||
89 | |||
90 | /* atomically decrement the semaphores count, and if its negative, we wait */ | ||
91 | cris_atomic_save(sem, flags); | ||
92 | failed = --(sem->count.counter) < 0; | ||
93 | cris_atomic_restore(sem, flags); | ||
94 | if(failed) | ||
95 | failed = __down_interruptible(sem); | ||
96 | return(failed); | ||
97 | } | ||
98 | |||
99 | static inline int down_trylock(struct semaphore * sem) | ||
100 | { | ||
101 | unsigned long flags; | ||
102 | int failed; | ||
103 | |||
104 | cris_atomic_save(sem, flags); | ||
105 | failed = --(sem->count.counter) < 0; | ||
106 | cris_atomic_restore(sem, flags); | ||
107 | if(failed) | ||
108 | failed = __down_trylock(sem); | ||
109 | return(failed); | ||
110 | |||
111 | } | ||
112 | |||
113 | /* | ||
114 | * Note! This is subtle. We jump to wake people up only if | ||
115 | * the semaphore was negative (== somebody was waiting on it). | ||
116 | * The default case (no contention) will result in NO | ||
117 | * jumps for both down() and up(). | ||
118 | */ | ||
119 | static inline void up(struct semaphore * sem) | ||
120 | { | ||
121 | unsigned long flags; | ||
122 | int wakeup; | ||
123 | |||
124 | /* atomically increment the semaphores count, and if it was negative, we wake people */ | ||
125 | cris_atomic_save(sem, flags); | ||
126 | wakeup = ++(sem->count.counter) <= 0; | ||
127 | cris_atomic_restore(sem, flags); | ||
128 | if(wakeup) { | ||
129 | __up(sem); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | #endif | ||