Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | struct semaphore { |
| 22 | atomic_t count; |
| 23 | atomic_t waking; |
| 24 | wait_queue_head_t wait; |
| 25 | }; |
| 26 | |
| 27 | #define __SEMAPHORE_INITIALIZER(name, n) \ |
| 28 | { \ |
| 29 | .count = ATOMIC_INIT(n), \ |
| 30 | .waking = ATOMIC_INIT(0), \ |
| 31 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ |
| 32 | } |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define __DECLARE_SEMAPHORE_GENERIC(name,count) \ |
| 35 | struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) |
| 36 | |
| 37 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1) |
| 38 | #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0) |
| 39 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 40 | static inline void sema_init(struct semaphore *sem, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val); |
| 43 | } |
| 44 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 45 | static inline void init_MUTEX (struct semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | sema_init(sem, 1); |
| 48 | } |
| 49 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 50 | static inline void init_MUTEX_LOCKED (struct semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | sema_init(sem, 0); |
| 53 | } |
| 54 | |
| 55 | extern void __down(struct semaphore * sem); |
| 56 | extern int __down_interruptible(struct semaphore * sem); |
| 57 | extern int __down_trylock(struct semaphore * sem); |
| 58 | extern void __up(struct semaphore * sem); |
| 59 | |
| 60 | /* notice - we probably can do cli/sti here instead of saving */ |
| 61 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 62 | static inline void down(struct semaphore * sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | unsigned long flags; |
| 65 | int failed; |
| 66 | |
| 67 | might_sleep(); |
| 68 | |
| 69 | /* atomically decrement the semaphores count, and if its negative, we wait */ |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 70 | cris_atomic_save(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | failed = --(sem->count.counter) < 0; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 72 | cris_atomic_restore(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | if(failed) { |
| 74 | __down(sem); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * This version waits in interruptible state so that the waiting |
| 80 | * process can be killed. The down_interruptible routine |
| 81 | * returns negative for signalled and zero for semaphore acquired. |
| 82 | */ |
| 83 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 84 | static inline int down_interruptible(struct semaphore * sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | unsigned long flags; |
| 87 | int failed; |
| 88 | |
| 89 | might_sleep(); |
| 90 | |
| 91 | /* atomically decrement the semaphores count, and if its negative, we wait */ |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 92 | cris_atomic_save(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | failed = --(sem->count.counter) < 0; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 94 | cris_atomic_restore(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if(failed) |
| 96 | failed = __down_interruptible(sem); |
| 97 | return(failed); |
| 98 | } |
| 99 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 100 | static inline int down_trylock(struct semaphore * sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | unsigned long flags; |
| 103 | int failed; |
| 104 | |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 105 | cris_atomic_save(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | failed = --(sem->count.counter) < 0; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 107 | cris_atomic_restore(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if(failed) |
| 109 | failed = __down_trylock(sem); |
| 110 | return(failed); |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Note! This is subtle. We jump to wake people up only if |
| 116 | * the semaphore was negative (== somebody was waiting on it). |
| 117 | * The default case (no contention) will result in NO |
| 118 | * jumps for both down() and up(). |
| 119 | */ |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 120 | static inline void up(struct semaphore * sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | unsigned long flags; |
| 123 | int wakeup; |
| 124 | |
| 125 | /* atomically increment the semaphores count, and if it was negative, we wake people */ |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 126 | cris_atomic_save(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | wakeup = ++(sem->count.counter) <= 0; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 128 | cris_atomic_restore(sem, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if(wakeup) { |
| 130 | __up(sem); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #endif |