Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/include/asm-xtensa/semaphore.h |
| 3 | * |
| 4 | * This file is subject to the terms and conditions of the GNU General Public |
| 5 | * License. See the file "COPYING" in the main directory of this archive |
| 6 | * for more details. |
| 7 | * |
| 8 | * Copyright (C) 2001 - 2005 Tensilica Inc. |
| 9 | */ |
| 10 | |
| 11 | #ifndef _XTENSA_SEMAPHORE_H |
| 12 | #define _XTENSA_SEMAPHORE_H |
| 13 | |
| 14 | #include <asm/atomic.h> |
| 15 | #include <asm/system.h> |
| 16 | #include <linux/wait.h> |
| 17 | #include <linux/rwsem.h> |
| 18 | |
| 19 | struct semaphore { |
| 20 | atomic_t count; |
| 21 | int sleepers; |
| 22 | wait_queue_head_t wait; |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Chris Zankel | 288a60c | 2005-09-22 21:44:23 -0700 | [diff] [blame] | 25 | #define __SEMAPHORE_INITIALIZER(name,n) \ |
| 26 | { \ |
| 27 | .count = ATOMIC_INIT(n), \ |
| 28 | .sleepers = 0, \ |
| 29 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ |
| 30 | } |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 31 | |
Chris Zankel | 288a60c | 2005-09-22 21:44:23 -0700 | [diff] [blame] | 32 | #define __DECLARE_SEMAPHORE_GENERIC(name,count) \ |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 33 | struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) |
| 34 | |
| 35 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 36 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 37 | static inline void sema_init (struct semaphore *sem, int val) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 38 | { |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 39 | atomic_set(&sem->count, val); |
Arthur Othieno | aa3a6f4 | 2005-11-07 00:59:01 -0800 | [diff] [blame] | 40 | sem->sleepers = 0; |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 41 | init_waitqueue_head(&sem->wait); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static inline void init_MUTEX (struct semaphore *sem) |
| 45 | { |
| 46 | sema_init(sem, 1); |
| 47 | } |
| 48 | |
| 49 | static inline void init_MUTEX_LOCKED (struct semaphore *sem) |
| 50 | { |
| 51 | sema_init(sem, 0); |
| 52 | } |
| 53 | |
| 54 | asmlinkage void __down(struct semaphore * sem); |
| 55 | asmlinkage int __down_interruptible(struct semaphore * sem); |
| 56 | asmlinkage int __down_trylock(struct semaphore * sem); |
| 57 | asmlinkage void __up(struct semaphore * sem); |
| 58 | |
| 59 | extern spinlock_t semaphore_wake_lock; |
| 60 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 61 | static inline void down(struct semaphore * sem) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 62 | { |
Chris Zankel | 288a60c | 2005-09-22 21:44:23 -0700 | [diff] [blame] | 63 | might_sleep(); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 64 | |
| 65 | if (atomic_sub_return(1, &sem->count) < 0) |
| 66 | __down(sem); |
| 67 | } |
| 68 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 69 | static inline int down_interruptible(struct semaphore * sem) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 70 | { |
| 71 | int ret = 0; |
Chris Zankel | 288a60c | 2005-09-22 21:44:23 -0700 | [diff] [blame] | 72 | |
| 73 | might_sleep(); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 74 | |
| 75 | if (atomic_sub_return(1, &sem->count) < 0) |
| 76 | ret = __down_interruptible(sem); |
| 77 | return ret; |
| 78 | } |
| 79 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 80 | static inline int down_trylock(struct semaphore * sem) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 81 | { |
| 82 | int ret = 0; |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 83 | |
| 84 | if (atomic_sub_return(1, &sem->count) < 0) |
| 85 | ret = __down_trylock(sem); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Note! This is subtle. We jump to wake people up only if |
| 91 | * the semaphore was negative (== somebody was waiting on it). |
| 92 | */ |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 93 | static inline void up(struct semaphore * sem) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 94 | { |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 95 | if (atomic_add_return(1, &sem->count) <= 0) |
| 96 | __up(sem); |
| 97 | } |
| 98 | |
| 99 | #endif /* _XTENSA_SEMAPHORE_H */ |