Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / include / asm-alpha / semaphore.h
blobeb2cbd97d40434ba72ce57cdd364bac92ae49f4d
1 #ifndef _ALPHA_SEMAPHORE_H
2 #define _ALPHA_SEMAPHORE_H
4 /*
5 * SMP- and interrupt-safe semaphores..
7 * (C) Copyright 1996 Linus Torvalds
8 * (C) Copyright 1996, 2000 Richard Henderson
9 */
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>
18 struct semaphore {
19 atomic_t count;
20 wait_queue_head_t wait;
23 #define __SEMAPHORE_INITIALIZER(name, n) \
24 { \
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)
41 * Logically,
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)
52 sema_init(sem, 1);
55 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
57 sema_init(sem, 0);
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)
76 long count;
77 might_sleep();
78 count = atomic_dec_return(&sem->count);
79 if (unlikely(count < 0))
80 __down_failed(sem);
83 static inline int __down_interruptible(struct semaphore *sem)
85 long count;
86 might_sleep();
87 count = atomic_dec_return(&sem->count);
88 if (unlikely(count < 0))
89 return __down_failed_interruptible(sem);
90 return 0;
94 * down_trylock returns 0 on success, 1 if we failed to get the lock.
97 static inline int __down_trylock(struct semaphore *sem)
99 long ret;
101 /* "Equivalent" C:
103 do {
104 ret = ldl_l;
105 --ret;
106 if (ret < 0)
107 break;
108 ret = stl_c = ret;
109 } while (ret == 0);
111 __asm__ __volatile__(
112 "1: ldl_l %0,%1\n"
113 " subl %0,1,%0\n"
114 " blt %0,2f\n"
115 " stl_c %0,%1\n"
116 " beq %0,3f\n"
117 " mb\n"
118 "2:\n"
119 ".subsection 2\n"
120 "3: br 1b\n"
121 ".previous"
122 : "=&r" (ret), "=m" (sem->count)
123 : "m" (sem->count));
125 return ret < 0;
128 static inline void __up(struct semaphore *sem)
130 if (unlikely(atomic_inc_return(&sem->count) <= 0))
131 __up_wakeup(sem);
134 #if !defined(CONFIG_DEBUG_SEMAPHORE)
135 extern inline void down(struct semaphore *sem)
137 __down(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)
149 __up(sem);
151 #endif
153 #endif