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 
19 #include <asm/system.h>
20 #include <asm/atomic.h>
21 
22 struct semaphore {
23 	atomic_t count;
24 	int sleepers;
25 	wait_queue_head_t wait;
26 #if WAITQUEUE_DEBUG
27 	long __magic;
28 #endif
29 };
30 
31 #if WAITQUEUE_DEBUG
32 # define __SEM_DEBUG_INIT(name) \
33 		, (int)&(name).__magic
34 #else
35 # define __SEM_DEBUG_INIT(name)
36 #endif
37 
38 #define __SEMAPHORE_INITIALIZER(name,count) \
39 { ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
40 	__SEM_DEBUG_INIT(name) }
41 
42 #define __MUTEX_INITIALIZER(name) \
43 	__SEMAPHORE_INITIALIZER(name,1)
44 
45 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
46 	struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
47 
48 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
49 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
50 
sema_init(struct semaphore * sem,int val)51 static inline void sema_init (struct semaphore *sem, int val)
52 {
53 /*
54  *	*sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
55  *
56  * i'd rather use the more flexible initialization above, but sadly
57  * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
58  */
59 	atomic_set(&sem->count, val);
60 	sem->sleepers = 0;
61 	init_waitqueue_head(&sem->wait);
62 #if WAITQUEUE_DEBUG
63 	sem->__magic = (int)&sem->__magic;
64 #endif
65 }
66 
init_MUTEX(struct semaphore * sem)67 static inline void init_MUTEX (struct semaphore *sem)
68 {
69 	sema_init(sem, 1);
70 }
71 
init_MUTEX_LOCKED(struct semaphore * sem)72 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
73 {
74 	sema_init(sem, 0);
75 }
76 
77 #if 0
78 asmlinkage void __down_failed(void /* special register calling convention */);
79 asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
80 asmlinkage int  __down_failed_trylock(void  /* params in registers */);
81 asmlinkage void __up_wakeup(void /* special register calling convention */);
82 #endif
83 
84 asmlinkage void __down(struct semaphore * sem);
85 asmlinkage int  __down_interruptible(struct semaphore * sem);
86 asmlinkage int  __down_trylock(struct semaphore * sem);
87 asmlinkage void __up(struct semaphore * sem);
88 
89 extern spinlock_t semaphore_wake_lock;
90 
down(struct semaphore * sem)91 static inline void down(struct semaphore * sem)
92 {
93 #if WAITQUEUE_DEBUG
94 	CHECK_MAGIC(sem->__magic);
95 #endif
96 
97 	if (atomic_dec_return(&sem->count) < 0)
98 		__down(sem);
99 }
100 
down_interruptible(struct semaphore * sem)101 static inline int down_interruptible(struct semaphore * sem)
102 {
103 	int ret = 0;
104 #if WAITQUEUE_DEBUG
105 	CHECK_MAGIC(sem->__magic);
106 #endif
107 
108 	if (atomic_dec_return(&sem->count) < 0)
109 		ret = __down_interruptible(sem);
110 	return ret;
111 }
112 
down_trylock(struct semaphore * sem)113 static inline int down_trylock(struct semaphore * sem)
114 {
115 	int ret = 0;
116 #if WAITQUEUE_DEBUG
117 	CHECK_MAGIC(sem->__magic);
118 #endif
119 
120 	if (atomic_dec_return(&sem->count) < 0)
121 		ret = __down_trylock(sem);
122 	return ret;
123 }
124 
125 /*
126  * Note! This is subtle. We jump to wake people up only if
127  * the semaphore was negative (== somebody was waiting on it).
128  */
up(struct semaphore * sem)129 static inline void up(struct semaphore * sem)
130 {
131 #if WAITQUEUE_DEBUG
132 	CHECK_MAGIC(sem->__magic);
133 #endif
134 	if (atomic_inc_return(&sem->count) <= 0)
135 		__up(sem);
136 }
137 
138 #endif
139 #endif /* __ASM_SH_SEMAPHORE_H */
140