blob: 27bfeca1b981a5b8fa343a76796aa38ef8f8c90e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: semaphore-helper.h,v 1.3 2001/03/26 15:00:33 orjanf Exp $
2 *
3 * SMP- and interrupt-safe semaphores helper functions. Generic versions, no
4 * optimizations whatsoever...
5 *
6 */
7
8#ifndef _ASM_SEMAPHORE_HELPER_H
9#define _ASM_SEMAPHORE_HELPER_H
10
11#include <asm/atomic.h>
12#include <linux/errno.h>
13
14#define read(a) ((a)->counter)
15#define inc(a) (((a)->counter)++)
16#define dec(a) (((a)->counter)--)
17
18#define count_inc(a) ((*(a))++)
19
20/*
21 * These two _must_ execute atomically wrt each other.
22 */
Adrian Bunkbb8cc642006-12-06 20:40:21 -080023static inline void wake_one_more(struct semaphore * sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 atomic_inc(&sem->waking);
26}
27
Adrian Bunkbb8cc642006-12-06 20:40:21 -080028static inline int waking_non_zero(struct semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 unsigned long flags;
31 int ret = 0;
32
Jiri Kosina047c7c42007-02-10 01:43:50 -080033 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 if (read(&sem->waking) > 0) {
35 dec(&sem->waking);
36 ret = 1;
37 }
38 local_irq_restore(flags);
39 return ret;
40}
41
Adrian Bunkbb8cc642006-12-06 20:40:21 -080042static inline int waking_non_zero_interruptible(struct semaphore *sem,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct task_struct *tsk)
44{
45 int ret = 0;
46 unsigned long flags;
47
Jiri Kosina047c7c42007-02-10 01:43:50 -080048 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (read(&sem->waking) > 0) {
50 dec(&sem->waking);
51 ret = 1;
52 } else if (signal_pending(tsk)) {
53 inc(&sem->count);
54 ret = -EINTR;
55 }
56 local_irq_restore(flags);
57 return ret;
58}
59
Adrian Bunkbb8cc642006-12-06 20:40:21 -080060static inline int waking_non_zero_trylock(struct semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 int ret = 1;
63 unsigned long flags;
64
Jiri Kosina047c7c42007-02-10 01:43:50 -080065 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (read(&sem->waking) <= 0)
67 inc(&sem->count);
68 else {
69 dec(&sem->waking);
70 ret = 0;
71 }
72 local_irq_restore(flags);
73 return ret;
74}
75
76#endif /* _ASM_SEMAPHORE_HELPER_H */
77
78