1 #ifndef _ALPHA_SEMAPHORE_H
2 #define _ALPHA_SEMAPHORE_H
5 * SMP- and interrupt-safe semaphores..
7 * (C) Copyright 1996 Linus Torvalds
8 * (C) Copyright 1996, 2000 Richard Henderson
11 #include <asm/current.h>
12 #include <asm/system.h>
13 #include <asm/atomic.h>
14 #include <linux/compiler.h>
15 #include <linux/wait.h>
16 #include <linux/rwsem.h>
19 /* Careful, inline assembly knows about the position of these two. */
20 atomic_t count __attribute__((aligned(8)));
21 atomic_t waking; /* biased by -1 */
23 wait_queue_head_t wait;
30 # define __SEM_DEBUG_INIT(name) , (long)&(name).__magic
32 # define __SEM_DEBUG_INIT(name)
35 #define __SEMAPHORE_INITIALIZER(name,count) \
36 { ATOMIC_INIT(count), ATOMIC_INIT(-1), \
37 __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
38 __SEM_DEBUG_INIT(name) }
40 #define __MUTEX_INITIALIZER(name) \
41 __SEMAPHORE_INITIALIZER(name,1)
43 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
44 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
46 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
47 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
49 static inline void sema_init(struct semaphore *sem, int val)
53 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
54 * except that gcc produces better initializing by parts yet.
57 atomic_set(&sem->count, val);
58 atomic_set(&sem->waking, -1);
59 init_waitqueue_head(&sem->wait);
61 sem->__magic = (long)&sem->__magic;
65 static inline void init_MUTEX (struct semaphore *sem)
70 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
75 extern void down(struct semaphore *);
76 extern void __down_failed(struct semaphore *);
77 extern int down_interruptible(struct semaphore *);
78 extern int __down_failed_interruptible(struct semaphore *);
79 extern int down_trylock(struct semaphore *);
80 extern void up(struct semaphore *);
81 extern void __up_wakeup(struct semaphore *);
83 static inline int sem_getcount(struct semaphore *sem)
85 return atomic_read(&sem->count);
89 * Hidden out of line code is fun, but extremely messy. Rely on newer
90 * compilers to do a respectable job with this. The contention cases
91 * are handled out of line in arch/alpha/kernel/semaphore.c.
94 static inline void __down(struct semaphore *sem)
96 long count = atomic_dec_return(&sem->count);
97 if (unlikely(count < 0))
101 static inline int __down_interruptible(struct semaphore *sem)
103 long count = atomic_dec_return(&sem->count);
104 if (unlikely(count < 0))
105 return __down_failed_interruptible(sem);
110 * down_trylock returns 0 on success, 1 if we failed to get the lock.
112 * We must manipulate count and waking simultaneously and atomically.
113 * Do this by using ll/sc on the pair of 32-bit words.
116 static inline int __down_trylock(struct semaphore * sem)
118 long ret, tmp, tmp2, sub;
120 /* "Equivalent" C. Note that we have to do this all without
121 (taken) branches in order to be a valid ll/sc sequence.
125 sub = 0x0000000100000000;
126 ret = ((int)tmp <= 0); // count <= 0 ?
127 // Note that if count=0, the decrement overflows into
128 // waking, so cancel the 1 loaded above. Also cancel
129 // it if the lock was already free.
130 if ((int)tmp >= 0) sub = 0; // count >= 0 ?
131 ret &= ((long)tmp < 0); // waking < 0 ?
139 __asm__ __volatile__(
157 : "=&r"(ret), "=&r"(tmp), "=&r"(tmp2), "=&r"(sub)
164 static inline void __up(struct semaphore *sem)
166 long ret, tmp, tmp2, tmp3;
168 /* We must manipulate count and waking simultaneously and atomically.
169 Otherwise we have races between up and __down_failed_interruptible
170 waking up on a signal.
172 "Equivalent" C. Note that we have to do this all without
173 (taken) branches in order to be a valid ll/sc sequence.
177 ret = (int)tmp + 1; // count += 1;
178 tmp2 = tmp & 0xffffffff00000000; // extract waking
179 if (ret <= 0) // still sleepers?
180 tmp2 += 0x0000000100000000; // waking += 1;
181 tmp = ret & 0x00000000ffffffff; // insert count
182 tmp |= tmp2; // insert waking;
187 __asm__ __volatile__(
191 " zapnot %1,0xf0,%2\n"
194 " zapnot %0,0x0f,%1\n"
202 : "=&r"(ret), "=&r"(tmp), "=&r"(tmp2), "=&r"(tmp3)
203 : "m"(*sem), "r"(0x0000000100000000)
206 if (unlikely(ret <= 0))
210 #if !WAITQUEUE_DEBUG && !defined(CONFIG_DEBUG_SEMAPHORE)
211 extern inline void down(struct semaphore *sem)
215 extern inline int down_interruptible(struct semaphore *sem)
217 return __down_interruptible(sem);
219 extern inline int down_trylock(struct semaphore *sem)
221 return __down_trylock(sem);
223 extern inline void up(struct semaphore *sem)