blob: 39faf69bcf766523a7ad34e019b9ce63e40de2c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: semaphore.h,v 1.3 2001/05/08 13:54:09 bjornw Exp $ */
2
3/* On the i386 these are coded in asm, perhaps we should as well. Later.. */
4
5#ifndef _CRIS_SEMAPHORE_H
6#define _CRIS_SEMAPHORE_H
7
8#define RW_LOCK_BIAS 0x01000000
9
10#include <linux/wait.h>
11#include <linux/spinlock.h>
12#include <linux/rwsem.h>
13
14#include <asm/system.h>
15#include <asm/atomic.h>
16
17/*
18 * CRIS semaphores, implemented in C-only so far.
19 */
20
21int printk(const char *fmt, ...);
22
23struct semaphore {
24 atomic_t count;
25 atomic_t waking;
26 wait_queue_head_t wait;
27};
28
29#define __SEMAPHORE_INITIALIZER(name, n) \
30{ \
31 .count = ATOMIC_INIT(n), \
32 .waking = ATOMIC_INIT(0), \
33 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
34}
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
37 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
38
39#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
40#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
41
42extern inline void sema_init(struct semaphore *sem, int val)
43{
44 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
45}
46
47extern inline void init_MUTEX (struct semaphore *sem)
48{
49 sema_init(sem, 1);
50}
51
52extern inline void init_MUTEX_LOCKED (struct semaphore *sem)
53{
54 sema_init(sem, 0);
55}
56
57extern void __down(struct semaphore * sem);
58extern int __down_interruptible(struct semaphore * sem);
59extern int __down_trylock(struct semaphore * sem);
60extern void __up(struct semaphore * sem);
61
62/* notice - we probably can do cli/sti here instead of saving */
63
64extern inline void down(struct semaphore * sem)
65{
66 unsigned long flags;
67 int failed;
68
69 might_sleep();
70
71 /* atomically decrement the semaphores count, and if its negative, we wait */
Mikael Starvik8d20a542005-07-27 11:44:42 -070072 cris_atomic_save(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 failed = --(sem->count.counter) < 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -070074 cris_atomic_restore(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if(failed) {
76 __down(sem);
77 }
78}
79
80/*
81 * This version waits in interruptible state so that the waiting
82 * process can be killed. The down_interruptible routine
83 * returns negative for signalled and zero for semaphore acquired.
84 */
85
86extern inline int down_interruptible(struct semaphore * sem)
87{
88 unsigned long flags;
89 int failed;
90
91 might_sleep();
92
93 /* atomically decrement the semaphores count, and if its negative, we wait */
Mikael Starvik8d20a542005-07-27 11:44:42 -070094 cris_atomic_save(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 failed = --(sem->count.counter) < 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -070096 cris_atomic_restore(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if(failed)
98 failed = __down_interruptible(sem);
99 return(failed);
100}
101
102extern inline int down_trylock(struct semaphore * sem)
103{
104 unsigned long flags;
105 int failed;
106
Mikael Starvik8d20a542005-07-27 11:44:42 -0700107 cris_atomic_save(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 failed = --(sem->count.counter) < 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700109 cris_atomic_restore(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if(failed)
111 failed = __down_trylock(sem);
112 return(failed);
Mikael Starvik8d20a542005-07-27 11:44:42 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * Note! This is subtle. We jump to wake people up only if
118 * the semaphore was negative (== somebody was waiting on it).
119 * The default case (no contention) will result in NO
120 * jumps for both down() and up().
121 */
122extern inline void up(struct semaphore * sem)
123{
124 unsigned long flags;
125 int wakeup;
126
127 /* atomically increment the semaphores count, and if it was negative, we wake people */
Mikael Starvik8d20a542005-07-27 11:44:42 -0700128 cris_atomic_save(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 wakeup = ++(sem->count.counter) <= 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700130 cris_atomic_restore(sem, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if(wakeup) {
132 __up(sem);
133 }
134}
135
136#endif