[PATCH] kexec: s390 support
[linux/fpc-iii.git] / include / asm-xtensa / semaphore.h
blobc8a7574a9a57bc00e570e3910b325f21d06ef33b
1 /*
2 * linux/include/asm-xtensa/semaphore.h
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
9 */
11 #ifndef _XTENSA_SEMAPHORE_H
12 #define _XTENSA_SEMAPHORE_H
14 #include <asm/atomic.h>
15 #include <asm/system.h>
16 #include <linux/wait.h>
17 #include <linux/rwsem.h>
19 struct semaphore {
20 atomic_t count;
21 int sleepers;
22 wait_queue_head_t wait;
23 #if WAITQUEUE_DEBUG
24 long __magic;
25 #endif
28 #if WAITQUEUE_DEBUG
29 # define __SEM_DEBUG_INIT(name) \
30 , (int)&(name).__magic
31 #else
32 # define __SEM_DEBUG_INIT(name)
33 #endif
35 #define __SEMAPHORE_INITIALIZER(name,count) \
36 { ATOMIC_INIT(count), \
37 0, \
38 __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
39 __SEM_DEBUG_INIT(name) }
41 #define __MUTEX_INITIALIZER(name) \
42 __SEMAPHORE_INITIALIZER(name, 1)
44 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
45 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
47 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
48 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
50 extern inline void sema_init (struct semaphore *sem, int val)
53 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
55 * i'd rather use the more flexible initialization above, but sadly
56 * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
58 atomic_set(&sem->count, val);
59 init_waitqueue_head(&sem->wait);
60 #if WAITQUEUE_DEBUG
61 sem->__magic = (int)&sem->__magic;
62 #endif
65 static inline void init_MUTEX (struct semaphore *sem)
67 sema_init(sem, 1);
70 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
72 sema_init(sem, 0);
75 asmlinkage void __down(struct semaphore * sem);
76 asmlinkage int __down_interruptible(struct semaphore * sem);
77 asmlinkage int __down_trylock(struct semaphore * sem);
78 asmlinkage void __up(struct semaphore * sem);
80 extern spinlock_t semaphore_wake_lock;
82 extern __inline__ void down(struct semaphore * sem)
84 #if WAITQUEUE_DEBUG
85 CHECK_MAGIC(sem->__magic);
86 #endif
88 if (atomic_sub_return(1, &sem->count) < 0)
89 __down(sem);
92 extern __inline__ int down_interruptible(struct semaphore * sem)
94 int ret = 0;
95 #if WAITQUEUE_DEBUG
96 CHECK_MAGIC(sem->__magic);
97 #endif
99 if (atomic_sub_return(1, &sem->count) < 0)
100 ret = __down_interruptible(sem);
101 return ret;
104 extern __inline__ int down_trylock(struct semaphore * sem)
106 int ret = 0;
107 #if WAITQUEUE_DEBUG
108 CHECK_MAGIC(sem->__magic);
109 #endif
111 if (atomic_sub_return(1, &sem->count) < 0)
112 ret = __down_trylock(sem);
113 return ret;
117 * Note! This is subtle. We jump to wake people up only if
118 * the semaphore was negative (== somebody was waiting on it).
120 extern __inline__ void up(struct semaphore * sem)
122 #if WAITQUEUE_DEBUG
123 CHECK_MAGIC(sem->__magic);
124 #endif
125 if (atomic_add_return(1, &sem->count) <= 0)
126 __up(sem);
129 #endif /* _XTENSA_SEMAPHORE_H */