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>
20 wait_queue_head_t wait;
23 #define __SEMAPHORE_INITIALIZER(name, n) \
25 .count = ATOMIC_INIT(n), \
26 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \
29 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
30 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
32 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
33 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
35 static inline void sema_init(struct semaphore *sem, int val)
39 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
40 * except that gcc produces better initializing by parts yet.
43 atomic_set(&sem->count, val);
44 init_waitqueue_head(&sem->wait);
47 static inline void init_MUTEX (struct semaphore *sem)
52 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
57 extern void down(struct semaphore *);
58 extern void __down_failed(struct semaphore *);
59 extern int down_interruptible(struct semaphore *);
60 extern int __down_failed_interruptible(struct semaphore *);
61 extern int down_trylock(struct semaphore *);
62 extern void up(struct semaphore *);
63 extern void __up_wakeup(struct semaphore *);
66 * Hidden out of line code is fun, but extremely messy. Rely on newer
67 * compilers to do a respectable job with this. The contention cases
68 * are handled out of line in arch/alpha/kernel/semaphore.c.
71 static inline void __down(struct semaphore *sem)
75 count = atomic_dec_return(&sem->count);
76 if (unlikely(count < 0))
80 static inline int __down_interruptible(struct semaphore *sem)
84 count = atomic_dec_return(&sem->count);
85 if (unlikely(count < 0))
86 return __down_failed_interruptible(sem);
91 * down_trylock returns 0 on success, 1 if we failed to get the lock.
94 static inline int __down_trylock(struct semaphore *sem)
108 __asm__ __volatile__(
119 : "=&r" (ret), "=m" (sem->count)
125 static inline void __up(struct semaphore *sem)
127 if (unlikely(atomic_inc_return(&sem->count) <= 0))
131 #if !defined(CONFIG_DEBUG_SEMAPHORE)
132 extern inline void down(struct semaphore *sem)
136 extern inline int down_interruptible(struct semaphore *sem)
138 return __down_interruptible(sem);
140 extern inline int down_trylock(struct semaphore *sem)
142 return __down_trylock(sem);
144 extern inline void up(struct semaphore *sem)