ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-ia64 / semaphore.h
1 #ifndef _ASM_IA64_SEMAPHORE_H
2 #define _ASM_IA64_SEMAPHORE_H
3
4 /*
5  * Copyright (C) 1998-2000 Hewlett-Packard Co
6  * Copyright (C) 1998-2000 David Mosberger-Tang <davidm@hpl.hp.com>
7  */
8
9 #include <linux/wait.h>
10 #include <linux/rwsem.h>
11
12 #include <asm/atomic.h>
13
14 struct semaphore {
15         atomic_t count;
16         int sleepers;
17         wait_queue_head_t wait;
18 #if WAITQUEUE_DEBUG
19         long __magic;           /* initialized by __SEM_DEBUG_INIT() */
20 #endif
21 };
22
23 #if WAITQUEUE_DEBUG
24 # define __SEM_DEBUG_INIT(name)         , (long) &(name).__magic
25 #else
26 # define __SEM_DEBUG_INIT(name)
27 #endif
28
29 #define __SEMAPHORE_INITIALIZER(name,count)                                     \
30 {                                                                               \
31         ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)       \
32         __SEM_DEBUG_INIT(name)                                                  \
33 }
34
35 #define __MUTEX_INITIALIZER(name)       __SEMAPHORE_INITIALIZER(name,1)
36
37 #define __DECLARE_SEMAPHORE_GENERIC(name,count)                                 \
38         struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
39
40 #define DECLARE_MUTEX(name)             __DECLARE_SEMAPHORE_GENERIC(name, 1)
41 #define DECLARE_MUTEX_LOCKED(name)      __DECLARE_SEMAPHORE_GENERIC(name, 0)
42
43 static inline void
44 sema_init (struct semaphore *sem, int val)
45 {
46         *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
47 }
48
49 static inline void
50 init_MUTEX (struct semaphore *sem)
51 {
52         sema_init(sem, 1);
53 }
54
55 static inline void
56 init_MUTEX_LOCKED (struct semaphore *sem)
57 {
58         sema_init(sem, 0);
59 }
60
61 extern void __down (struct semaphore * sem);
62 extern int  __down_interruptible (struct semaphore * sem);
63 extern int  __down_trylock (struct semaphore * sem);
64 extern void __up (struct semaphore * sem);
65
66 /*
67  * Atomically decrement the semaphore's count.  If it goes negative,
68  * block the calling thread in the TASK_UNINTERRUPTIBLE state.
69  */
70 static inline void
71 down (struct semaphore *sem)
72 {
73 #if WAITQUEUE_DEBUG
74         CHECK_MAGIC(sem->__magic);
75 #endif
76         might_sleep();
77         if (atomic_dec_return(&sem->count) < 0)
78                 __down(sem);
79 }
80
81 /*
82  * Atomically decrement the semaphore's count.  If it goes negative,
83  * block the calling thread in the TASK_INTERRUPTIBLE state.
84  */
85 static inline int
86 down_interruptible (struct semaphore * sem)
87 {
88         int ret = 0;
89
90 #if WAITQUEUE_DEBUG
91         CHECK_MAGIC(sem->__magic);
92 #endif
93         might_sleep();
94         if (atomic_dec_return(&sem->count) < 0)
95                 ret = __down_interruptible(sem);
96         return ret;
97 }
98
99 static inline int
100 down_trylock (struct semaphore *sem)
101 {
102         int ret = 0;
103
104 #if WAITQUEUE_DEBUG
105         CHECK_MAGIC(sem->__magic);
106 #endif
107         if (atomic_dec_return(&sem->count) < 0)
108                 ret = __down_trylock(sem);
109         return ret;
110 }
111
112 static inline void
113 up (struct semaphore * sem)
114 {
115 #if WAITQUEUE_DEBUG
116         CHECK_MAGIC(sem->__magic);
117 #endif
118         if (atomic_inc_return(&sem->count) <= 0)
119                 __up(sem);
120 }
121
122 #endif /* _ASM_IA64_SEMAPHORE_H */