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
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
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>
22 wait_queue_head_t wait;
29 # define __SEM_DEBUG_INIT(name) \
30 , (int)&(name).__magic
32 # define __SEM_DEBUG_INIT(name)
35 #define __SEMAPHORE_INITIALIZER(name,count) \
36 { ATOMIC_INIT(count), \
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 static 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);
61 sem->__magic = (int)&sem->__magic;
65 static inline void init_MUTEX (struct semaphore *sem)
70 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
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 static inline void down(struct semaphore * sem)
85 CHECK_MAGIC(sem->__magic);
88 if (atomic_sub_return(1, &sem->count) < 0)
92 static inline int down_interruptible(struct semaphore * sem)
96 CHECK_MAGIC(sem->__magic);
99 if (atomic_sub_return(1, &sem->count) < 0)
100 ret = __down_interruptible(sem);
104 static inline int down_trylock(struct semaphore * sem)
108 CHECK_MAGIC(sem->__magic);
111 if (atomic_sub_return(1, &sem->count) < 0)
112 ret = __down_trylock(sem);
117 * Note! This is subtle. We jump to wake people up only if
118 * the semaphore was negative (== somebody was waiting on it).
120 static inline void up(struct semaphore * sem)
123 CHECK_MAGIC(sem->__magic);
125 if (atomic_add_return(1, &sem->count) <= 0)
129 #endif /* _XTENSA_SEMAPHORE_H */