blob: 71ca7d412687f9bb0e1a2c2e61a8857e96cec6f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/include/asm-arm/semaphore.h
3 */
4#ifndef __ASM_ARM_SEMAPHORE_H
5#define __ASM_ARM_SEMAPHORE_H
6
7#include <linux/linkage.h>
8#include <linux/spinlock.h>
9#include <linux/wait.h>
10#include <linux/rwsem.h>
11
12#include <asm/atomic.h>
13#include <asm/locks.h>
14
15struct semaphore {
16 atomic_t count;
17 int sleepers;
18 wait_queue_head_t wait;
19};
20
21#define __SEMAPHORE_INIT(name, cnt) \
22{ \
23 .count = ATOMIC_INIT(cnt), \
24 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \
25}
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
28 struct semaphore name = __SEMAPHORE_INIT(name,count)
29
30#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
31#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
32
33static inline void sema_init(struct semaphore *sem, int val)
34{
35 atomic_set(&sem->count, val);
36 sem->sleepers = 0;
37 init_waitqueue_head(&sem->wait);
38}
39
40static inline void init_MUTEX(struct semaphore *sem)
41{
42 sema_init(sem, 1);
43}
44
45static inline void init_MUTEX_LOCKED(struct semaphore *sem)
46{
47 sema_init(sem, 0);
48}
49
50static inline int sema_count(struct semaphore *sem)
51{
52 return atomic_read(&sem->count);
53}
54
55/*
56 * special register calling convention
57 */
58asmlinkage void __down_failed(void);
59asmlinkage int __down_interruptible_failed(void);
60asmlinkage int __down_trylock_failed(void);
61asmlinkage void __up_wakeup(void);
62
63extern void __down(struct semaphore * sem);
64extern int __down_interruptible(struct semaphore * sem);
65extern int __down_trylock(struct semaphore * sem);
66extern void __up(struct semaphore * sem);
67
68/*
69 * This is ugly, but we want the default case to fall through.
70 * "__down" is the actual routine that waits...
71 */
72static inline void down(struct semaphore * sem)
73{
74 might_sleep();
75 __down_op(sem, __down_failed);
76}
77
78/*
79 * This is ugly, but we want the default case to fall through.
80 * "__down_interruptible" is the actual routine that waits...
81 */
82static inline int down_interruptible (struct semaphore * sem)
83{
84 might_sleep();
85 return __down_op_ret(sem, __down_interruptible_failed);
86}
87
88static inline int down_trylock(struct semaphore *sem)
89{
90 return __down_op_ret(sem, __down_trylock_failed);
91}
92
93/*
94 * Note! This is subtle. We jump to wake people up only if
95 * the semaphore was negative (== somebody was waiting on it).
96 * The default case (no contention) will result in NO
97 * jumps for both down() and up().
98 */
99static inline void up(struct semaphore * sem)
100{
101 __up_op(sem, __up_wakeup);
102}
103
104#endif