1 #ifndef _ASM_M32R_SEMAPHORE_H
2 #define _ASM_M32R_SEMAPHORE_H
4 #include <linux/linkage.h>
9 * SMP- and interrupt-safe semaphores..
11 * Copyright (C) 1996 Linus Torvalds
12 * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org>
15 #include <linux/wait.h>
16 #include <linux/rwsem.h>
17 #include <asm/assembler.h>
18 #include <asm/system.h>
19 #include <asm/atomic.h>
24 wait_queue_head_t wait
;
27 #define __SEMAPHORE_INITIALIZER(name, n) \
29 .count = ATOMIC_INIT(n), \
31 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
34 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
35 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
37 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39 static inline void sema_init (struct semaphore
*sem
, int val
)
42 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
44 * i'd rather use the more flexible initialization above, but sadly
45 * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
47 atomic_set(&sem
->count
, val
);
49 init_waitqueue_head(&sem
->wait
);
52 static inline void init_MUTEX (struct semaphore
*sem
)
57 static inline void init_MUTEX_LOCKED (struct semaphore
*sem
)
62 asmlinkage
void __down_failed(void /* special register calling convention */);
63 asmlinkage
int __down_failed_interruptible(void /* params in registers */);
64 asmlinkage
int __down_failed_trylock(void /* params in registers */);
65 asmlinkage
void __up_wakeup(void /* special register calling convention */);
67 asmlinkage
void __down(struct semaphore
* sem
);
68 asmlinkage
int __down_interruptible(struct semaphore
* sem
);
69 asmlinkage
int __down_trylock(struct semaphore
* sem
);
70 asmlinkage
void __up(struct semaphore
* sem
);
73 * Atomically decrement the semaphore's count. If it goes negative,
74 * block the calling thread in the TASK_UNINTERRUPTIBLE state.
76 static inline void down(struct semaphore
* sem
)
79 if (unlikely(atomic_dec_return(&sem
->count
) < 0))
84 * Interruptible try to acquire a semaphore. If we obtained
85 * it, return zero. If we were interrupted, returns -EINTR
87 static inline int down_interruptible(struct semaphore
* sem
)
92 if (unlikely(atomic_dec_return(&sem
->count
) < 0))
93 result
= __down_interruptible(sem
);
99 * Non-blockingly attempt to down() a semaphore.
100 * Returns zero if we acquired it
102 static inline int down_trylock(struct semaphore
* sem
)
108 local_irq_save(flags
);
109 __asm__
__volatile__ (
110 "# down_trylock \n\t"
111 DCACHE_CLEAR("%0", "r4", "%1")
112 M32R_LOCK
" %0, @%1; \n\t"
114 M32R_UNLOCK
" %0, @%1; \n\t"
118 #ifdef CONFIG_CHIP_M32700_TS1
120 #endif /* CONFIG_CHIP_M32700_TS1 */
122 local_irq_restore(flags
);
124 if (unlikely(count
< 0))
125 result
= __down_trylock(sem
);
131 * Note! This is subtle. We jump to wake people up only if
132 * the semaphore was negative (== somebody was waiting on it).
133 * The default case (no contention) will result in NO
134 * jumps for both down() and up().
136 static inline void up(struct semaphore
* sem
)
138 if (unlikely(atomic_inc_return(&sem
->count
) <= 0))
142 #endif /* __KERNEL__ */
144 #endif /* _ASM_M32R_SEMAPHORE_H */