aboutsummaryrefslogtreecommitdiffstats
path: root/include/asm-sh/semaphore.h
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /include/asm-sh/semaphore.h
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'include/asm-sh/semaphore.h')
-rw-r--r--include/asm-sh/semaphore.h119
1 files changed, 119 insertions, 0 deletions
diff --git a/include/asm-sh/semaphore.h b/include/asm-sh/semaphore.h
new file mode 100644
index 000000000000..b923a77a8a7e
--- /dev/null
+++ b/include/asm-sh/semaphore.h
@@ -0,0 +1,119 @@
1#ifndef __ASM_SH_SEMAPHORE_H
2#define __ASM_SH_SEMAPHORE_H
3
4#include <linux/linkage.h>
5
6#ifdef __KERNEL__
7/*
8 * SMP- and interrupt-safe semaphores.
9 *
10 * (C) Copyright 1996 Linus Torvalds
11 *
12 * SuperH verison by Niibe Yutaka
13 * (Currently no asm implementation but generic C code...)
14 */
15
16#include <linux/spinlock.h>
17#include <linux/rwsem.h>
18#include <linux/wait.h>
19
20#include <asm/system.h>
21#include <asm/atomic.h>
22
23struct semaphore {
24 atomic_t count;
25 int sleepers;
26 wait_queue_head_t wait;
27};
28
29#define __SEMAPHORE_INITIALIZER(name, n) \
30{ \
31 .count = ATOMIC_INIT(n), \
32 .sleepers = 0, \
33 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
34}
35
36#define __MUTEX_INITIALIZER(name) \
37 __SEMAPHORE_INITIALIZER(name,1)
38
39#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
40 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
41
42#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
43#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
44
45static inline void sema_init (struct semaphore *sem, int val)
46{
47/*
48 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
49 *
50 * i'd rather use the more flexible initialization above, but sadly
51 * GCC 2.7.2.3 emits a bogus warning. EGCS doesn't. Oh well.
52 */
53 atomic_set(&sem->count, val);
54 sem->sleepers = 0;
55 init_waitqueue_head(&sem->wait);
56}
57
58static inline void init_MUTEX (struct semaphore *sem)
59{
60 sema_init(sem, 1);
61}
62
63static inline void init_MUTEX_LOCKED (struct semaphore *sem)
64{
65 sema_init(sem, 0);
66}
67
68#if 0
69asmlinkage void __down_failed(void /* special register calling convention */);
70asmlinkage int __down_failed_interruptible(void /* params in registers */);
71asmlinkage int __down_failed_trylock(void /* params in registers */);
72asmlinkage void __up_wakeup(void /* special register calling convention */);
73#endif
74
75asmlinkage void __down(struct semaphore * sem);
76asmlinkage int __down_interruptible(struct semaphore * sem);
77asmlinkage int __down_trylock(struct semaphore * sem);
78asmlinkage void __up(struct semaphore * sem);
79
80extern spinlock_t semaphore_wake_lock;
81
82static inline void down(struct semaphore * sem)
83{
84 might_sleep();
85 if (atomic_dec_return(&sem->count) < 0)
86 __down(sem);
87}
88
89static inline int down_interruptible(struct semaphore * sem)
90{
91 int ret = 0;
92
93 might_sleep();
94 if (atomic_dec_return(&sem->count) < 0)
95 ret = __down_interruptible(sem);
96 return ret;
97}
98
99static inline int down_trylock(struct semaphore * sem)
100{
101 int ret = 0;
102
103 if (atomic_dec_return(&sem->count) < 0)
104 ret = __down_trylock(sem);
105 return ret;
106}
107
108/*
109 * Note! This is subtle. We jump to wake people up only if
110 * the semaphore was negative (== somebody was waiting on it).
111 */
112static inline void up(struct semaphore * sem)
113{
114 if (atomic_inc_return(&sem->count) <= 0)
115 __up(sem);
116}
117
118#endif
119#endif /* __ASM_SH_SEMAPHORE_H */