blob: 29e0fbf1acb70a7f75c4d8dee3058ef2f99abfba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _H8300_SEMAPHORE_HELPER_H
2#define _H8300_SEMAPHORE_HELPER_H
3
4/*
5 * SMP- and interrupt-safe semaphores helper functions.
6 *
7 * (C) Copyright 1996 Linus Torvalds
8 *
9 * based on
10 * m68k version by Andreas Schwab
11 */
12
13#include <linux/config.h>
14#include <linux/errno.h>
15
16/*
17 * These two _must_ execute atomically wrt each other.
18 */
19static inline void wake_one_more(struct semaphore * sem)
20{
21 atomic_inc((atomic_t *)&sem->sleepers);
22}
23
24static inline int waking_non_zero(struct semaphore *sem)
25{
26 int ret;
27 unsigned long flags;
28
29 spin_lock_irqsave(&semaphore_wake_lock, flags);
30 ret = 0;
31 if (sem->sleepers > 0) {
32 sem->sleepers--;
33 ret = 1;
34 }
35 spin_unlock_irqrestore(&semaphore_wake_lock, flags);
36 return ret;
37}
38
39/*
40 * waking_non_zero_interruptible:
41 * 1 got the lock
42 * 0 go to sleep
43 * -EINTR interrupted
44 */
45static inline int waking_non_zero_interruptible(struct semaphore *sem,
46 struct task_struct *tsk)
47{
48 int ret;
49 unsigned long flags;
50
51 spin_lock_irqsave(&semaphore_wake_lock, flags);
52 ret = 0;
53 if (sem->sleepers > 0) {
54 sem->sleepers--;
55 ret = 1;
56 } else if (signal_pending(tsk)) {
57 atomic_inc(&sem->count);
58 ret = -EINTR;
59 }
60 spin_unlock_irqrestore(&semaphore_wake_lock, flags);
61 return ret;
62}
63
64/*
65 * waking_non_zero_trylock:
66 * 1 failed to lock
67 * 0 got the lock
68 */
69static inline int waking_non_zero_trylock(struct semaphore *sem)
70{
71 int ret;
72 unsigned long flags;
73
74 spin_lock_irqsave(&semaphore_wake_lock, flags);
75 ret = 1;
76 if (sem->sleepers <= 0)
77 atomic_inc(&sem->count);
78 else {
79 sem->sleepers--;
80 ret = 0;
81 }
82 spin_unlock_irqrestore(&semaphore_wake_lock, flags);
83 return ret;
84}
85
86#endif