1 #ifndef __ASM_SH64_SEMAPHORE_H
2 #define __ASM_SH64_SEMAPHORE_H
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
9 * include/asm-sh64/semaphore.h
11 * Copyright (C) 2000, 2001 Paolo Alberelli
13 * SMP- and interrupt-safe semaphores.
15 * (C) Copyright 1996 Linus Torvalds
17 * SuperH verison by Niibe Yutaka
18 * (Currently no asm implementation but generic C code...)
22 #include <linux/linkage.h>
23 #include <linux/spinlock.h>
24 #include <linux/wait.h>
25 #include <linux/rwsem.h>
27 #include <asm/system.h>
28 #include <asm/atomic.h>
33 wait_queue_head_t wait;
34 #ifdef WAITQUEUE_DEBUG
39 #ifdef WAITQUEUE_DEBUG
40 # define __SEM_DEBUG_INIT(name) \
41 , (int)&(name).__magic
43 # define __SEM_DEBUG_INIT(name)
46 #define __SEMAPHORE_INITIALIZER(name,count) \
47 { ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
48 __SEM_DEBUG_INIT(name) }
50 #define __MUTEX_INITIALIZER(name) \
51 __SEMAPHORE_INITIALIZER(name,1)
53 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
54 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
56 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
57 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
59 static inline void sema_init (struct semaphore *sem, int val)
62 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
64 * i'd rather use the more flexible initialization above, but sadly
65 * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
67 atomic_set(&sem->count, val);
69 init_waitqueue_head(&sem->wait);
70 #ifdef WAITQUEUE_DEBUG
71 sem->__magic = (int)&sem->__magic;
75 static inline void init_MUTEX (struct semaphore *sem)
80 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
86 asmlinkage void __down_failed(void /* special register calling convention */);
87 asmlinkage int __down_failed_interruptible(void /* params in registers */);
88 asmlinkage int __down_failed_trylock(void /* params in registers */);
89 asmlinkage void __up_wakeup(void /* special register calling convention */);
92 asmlinkage void __down(struct semaphore * sem);
93 asmlinkage int __down_interruptible(struct semaphore * sem);
94 asmlinkage int __down_trylock(struct semaphore * sem);
95 asmlinkage void __up(struct semaphore * sem);
97 extern spinlock_t semaphore_wake_lock;
99 static inline void down(struct semaphore * sem)
101 #ifdef WAITQUEUE_DEBUG
102 CHECK_MAGIC(sem->__magic);
105 if (atomic_dec_return(&sem->count) < 0)
109 static inline int down_interruptible(struct semaphore * sem)
112 #ifdef WAITQUEUE_DEBUG
113 CHECK_MAGIC(sem->__magic);
116 if (atomic_dec_return(&sem->count) < 0)
117 ret = __down_interruptible(sem);
121 static inline int down_trylock(struct semaphore * sem)
124 #ifdef WAITQUEUE_DEBUG
125 CHECK_MAGIC(sem->__magic);
128 if (atomic_dec_return(&sem->count) < 0)
129 ret = __down_trylock(sem);
134 * Note! This is subtle. We jump to wake people up only if
135 * the semaphore was negative (== somebody was waiting on it).
137 static inline void up(struct semaphore * sem)
139 #ifdef WAITQUEUE_DEBUG
140 CHECK_MAGIC(sem->__magic);
142 if (atomic_inc_return(&sem->count) <= 0)
146 #endif /* __ASM_SH64_SEMAPHORE_H */