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 __MUTEX_INITIALIZER(name) \
30 __SEMAPHORE_INITIALIZER(name,1)
32 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
33 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
35 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
36 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
38 static inline void sema_init(struct semaphore *sem, int val)
42 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
43 * except that gcc produces better initializing by parts yet.
46 atomic_set(&sem->count, val);
47 init_waitqueue_head(&sem->wait);
50 static inline void init_MUTEX (struct semaphore *sem)
55 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
60 extern void down(struct semaphore *);
61 extern void __down_failed(struct semaphore *);
62 extern int down_interruptible(struct semaphore *);
63 extern int __down_failed_interruptible(struct semaphore *);
64 extern int down_trylock(struct semaphore *);
65 extern void up(struct semaphore *);
66 extern void __up_wakeup(struct semaphore *);
69 * Hidden out of line code is fun, but extremely messy. Rely on newer
70 * compilers to do a respectable job with this. The contention cases
71 * are handled out of line in arch/alpha/kernel/semaphore.c.
74 static inline void __down(struct semaphore *sem)
78 count = atomic_dec_return(&sem->count);
79 if (unlikely(count < 0))
83 static inline int __down_interruptible(struct semaphore *sem)
87 count = atomic_dec_return(&sem->count);
88 if (unlikely(count < 0))
89 return __down_failed_interruptible(sem);
94 * down_trylock returns 0 on success, 1 if we failed to get the lock.
97 static inline int __down_trylock(struct semaphore *sem)
111 __asm__ __volatile__(
122 : "=&r" (ret), "=m" (sem->count)
128 static inline void __up(struct semaphore *sem)
130 if (unlikely(atomic_inc_return(&sem->count) <= 0))
134 #if !defined(CONFIG_DEBUG_SEMAPHORE)
135 extern inline void down(struct semaphore *sem)
139 extern inline int down_interruptible(struct semaphore *sem)
141 return __down_interruptible(sem);
143 extern inline int down_trylock(struct semaphore *sem)
145 return __down_trylock(sem);
147 extern inline void up(struct semaphore *sem)