Linux 2.6.21.1
[linux/fpc-iii.git] / include / asm-ia64 / semaphore.h
blobf483eeb95dd1dabfd248803492bb0a6a7561b6ec
1 #ifndef _ASM_IA64_SEMAPHORE_H
2 #define _ASM_IA64_SEMAPHORE_H
4 /*
5 * Copyright (C) 1998-2000 Hewlett-Packard Co
6 * Copyright (C) 1998-2000 David Mosberger-Tang <davidm@hpl.hp.com>
7 */
9 #include <linux/wait.h>
10 #include <linux/rwsem.h>
12 #include <asm/atomic.h>
14 struct semaphore {
15 atomic_t count;
16 int sleepers;
17 wait_queue_head_t wait;
20 #define __SEMAPHORE_INITIALIZER(name, n) \
21 { \
22 .count = ATOMIC_INIT(n), \
23 .sleepers = 0, \
24 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
27 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
28 struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
30 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
31 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
33 static inline void
34 sema_init (struct semaphore *sem, int val)
36 *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
39 static inline void
40 init_MUTEX (struct semaphore *sem)
42 sema_init(sem, 1);
45 static inline void
46 init_MUTEX_LOCKED (struct semaphore *sem)
48 sema_init(sem, 0);
51 extern void __down (struct semaphore * sem);
52 extern int __down_interruptible (struct semaphore * sem);
53 extern int __down_trylock (struct semaphore * sem);
54 extern void __up (struct semaphore * sem);
57 * Atomically decrement the semaphore's count. If it goes negative,
58 * block the calling thread in the TASK_UNINTERRUPTIBLE state.
60 static inline void
61 down (struct semaphore *sem)
63 might_sleep();
64 if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1)
65 __down(sem);
69 * Atomically decrement the semaphore's count. If it goes negative,
70 * block the calling thread in the TASK_INTERRUPTIBLE state.
72 static inline int
73 down_interruptible (struct semaphore * sem)
75 int ret = 0;
77 might_sleep();
78 if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1)
79 ret = __down_interruptible(sem);
80 return ret;
83 static inline int
84 down_trylock (struct semaphore *sem)
86 int ret = 0;
88 if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1)
89 ret = __down_trylock(sem);
90 return ret;
93 static inline void
94 up (struct semaphore * sem)
96 if (ia64_fetchadd(1, &sem->count.counter, rel) <= -1)
97 __up(sem);
100 #endif /* _ASM_IA64_SEMAPHORE_H */