1 #ifndef _ASM_PARISC_SEMAPHORE_H
2 #define _ASM_PARISC_SEMAPHORE_H
5 * SMP- and interrupt-safe semaphores.
7 * (C) Copyright 1996 Linus Torvalds
9 * PA-RISC version by Matthew Wilcox
13 #include <linux/spinlock.h>
14 #include <linux/wait.h>
15 #include <linux/rwsem.h>
17 #include <asm/system.h>
20 * The `count' is initialised to the number of people who are allowed to
21 * take the lock. (Normally we want a mutex, so this is `1'). if
22 * `count' is positive, the lock can be taken. if it's 0, no-one is
23 * waiting on it. if it's -1, at least one task is waiting.
28 wait_queue_head_t wait;
35 # define __SEM_DEBUG_INIT(name) \
36 , (long)&(name).__magic
38 # define __SEM_DEBUG_INIT(name)
41 #define __SEMAPHORE_INITIALIZER(name,count) \
42 { SPIN_LOCK_UNLOCKED, count, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
43 __SEM_DEBUG_INIT(name) }
45 #define __MUTEX_INITIALIZER(name) \
46 __SEMAPHORE_INITIALIZER(name,1)
48 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
49 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
51 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
52 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
54 extern inline void sema_init (struct semaphore *sem, int val)
56 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
59 static inline void init_MUTEX (struct semaphore *sem)
64 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
69 static inline int sem_getcount(struct semaphore *sem)
74 asmlinkage void __down(struct semaphore * sem);
75 asmlinkage int __down_interruptible(struct semaphore * sem);
76 asmlinkage void __up(struct semaphore * sem);
78 /* Semaphores can be `tried' from irq context. So we have to disable
79 * interrupts while we're messing with the semaphore. Sorry.
82 extern __inline__ void down(struct semaphore * sem)
85 CHECK_MAGIC(sem->__magic);
88 spin_lock_irq(&sem->sentry);
94 spin_unlock_irq(&sem->sentry);
97 extern __inline__ int down_interruptible(struct semaphore * sem)
101 CHECK_MAGIC(sem->__magic);
104 spin_lock_irq(&sem->sentry);
105 if (sem->count > 0) {
108 ret = __down_interruptible(sem);
110 spin_unlock_irq(&sem->sentry);
115 * down_trylock returns 0 on success, 1 if we failed to get the lock.
116 * May not sleep, but must preserve irq state
118 extern __inline__ int down_trylock(struct semaphore * sem)
122 CHECK_MAGIC(sem->__magic);
125 spin_lock_irqsave(&sem->sentry, flags);
126 count = sem->count - 1;
129 spin_unlock_irqrestore(&sem->sentry, flags);
134 * Note! This is subtle. We jump to wake people up only if
135 * the semaphore was negative (== somebody was waiting on it).
137 extern __inline__ void up(struct semaphore * sem)
141 CHECK_MAGIC(sem->__magic);
143 spin_lock_irqsave(&sem->sentry, flags);
144 if (sem->count < 0) {
149 spin_unlock_irqrestore(&sem->sentry, flags);
152 #endif /* _ASM_PARISC_SEMAPHORE_H */