Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _ASM_IA64_SEMAPHORE_H |
| 2 | #define _ASM_IA64_SEMAPHORE_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 1998-2000 Hewlett-Packard Co |
| 6 | * Copyright (C) 1998-2000 David Mosberger-Tang <davidm@hpl.hp.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/wait.h> |
| 10 | #include <linux/rwsem.h> |
| 11 | |
| 12 | #include <asm/atomic.h> |
| 13 | |
| 14 | struct semaphore { |
| 15 | atomic_t count; |
| 16 | int sleepers; |
| 17 | wait_queue_head_t wait; |
| 18 | }; |
| 19 | |
| 20 | #define __SEMAPHORE_INITIALIZER(name, n) \ |
| 21 | { \ |
| 22 | .count = ATOMIC_INIT(n), \ |
| 23 | .sleepers = 0, \ |
| 24 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ |
| 25 | } |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #define __DECLARE_SEMAPHORE_GENERIC(name,count) \ |
| 28 | struct semaphore name = __SEMAPHORE_INITIALIZER(name, count) |
| 29 | |
| 30 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | static inline void |
| 33 | sema_init (struct semaphore *sem, int val) |
| 34 | { |
| 35 | *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val); |
| 36 | } |
| 37 | |
| 38 | static inline void |
| 39 | init_MUTEX (struct semaphore *sem) |
| 40 | { |
| 41 | sema_init(sem, 1); |
| 42 | } |
| 43 | |
| 44 | static inline void |
| 45 | init_MUTEX_LOCKED (struct semaphore *sem) |
| 46 | { |
| 47 | sema_init(sem, 0); |
| 48 | } |
| 49 | |
| 50 | extern void __down (struct semaphore * sem); |
| 51 | extern int __down_interruptible (struct semaphore * sem); |
| 52 | extern int __down_trylock (struct semaphore * sem); |
| 53 | extern void __up (struct semaphore * sem); |
| 54 | |
| 55 | /* |
| 56 | * Atomically decrement the semaphore's count. If it goes negative, |
| 57 | * block the calling thread in the TASK_UNINTERRUPTIBLE state. |
| 58 | */ |
| 59 | static inline void |
| 60 | down (struct semaphore *sem) |
| 61 | { |
| 62 | might_sleep(); |
Zoltan Menyhart | 4b16bfb | 2006-01-13 17:25:23 +0100 | [diff] [blame] | 63 | if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | __down(sem); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Atomically decrement the semaphore's count. If it goes negative, |
| 69 | * block the calling thread in the TASK_INTERRUPTIBLE state. |
| 70 | */ |
| 71 | static inline int |
| 72 | down_interruptible (struct semaphore * sem) |
| 73 | { |
| 74 | int ret = 0; |
| 75 | |
| 76 | might_sleep(); |
Zoltan Menyhart | 4b16bfb | 2006-01-13 17:25:23 +0100 | [diff] [blame] | 77 | if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | ret = __down_interruptible(sem); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | static inline int |
| 83 | down_trylock (struct semaphore *sem) |
| 84 | { |
| 85 | int ret = 0; |
| 86 | |
Zoltan Menyhart | 4b16bfb | 2006-01-13 17:25:23 +0100 | [diff] [blame] | 87 | if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | ret = __down_trylock(sem); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static inline void |
| 93 | up (struct semaphore * sem) |
| 94 | { |
Zoltan Menyhart | 4b16bfb | 2006-01-13 17:25:23 +0100 | [diff] [blame] | 95 | if (ia64_fetchadd(1, &sem->count.counter, rel) <= -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | __up(sem); |
| 97 | } |
| 98 | |
| 99 | #endif /* _ASM_IA64_SEMAPHORE_H */ |