special usb hub handling, IDE disks, and retries all over the place
[linux-2.4.git] / include / asm-sh64 / semaphore-helper.h
1 #ifndef __ASM_SH64_SEMAPHORE_HELPER_H
2 #define __ASM_SH64_SEMAPHORE_HELPER_H
3
4 /*
5  * This file is subject to the terms and conditions of the GNU General Public
6  * License.  See the file "COPYING" in the main directory of this archive
7  * for more details.
8  *
9  * include/asm-sh64/semaphore-helper.h
10  *
11  * Copyright (C) 2000, 2001  Paolo Alberelli
12  *
13  */
14
15 /*
16  * SMP- and interrupt-safe semaphores helper functions.
17  *
18  * (C) Copyright 1996 Linus Torvalds
19  * (C) Copyright 1999 Andrea Arcangeli
20  */
21
22 /*
23  * These two _must_ execute atomically wrt each other.
24  *
25  * This is trivially done with load_locked/store_cond,
26  * which we have.  Let the rest of the losers suck eggs.
27  */
28 static __inline__ void wake_one_more(struct semaphore * sem)
29 {
30         atomic_inc((atomic_t *)&sem->sleepers);
31 }
32
33 static __inline__ int waking_non_zero(struct semaphore *sem)
34 {
35         unsigned long flags;
36         int ret = 0;
37
38         spin_lock_irqsave(&semaphore_wake_lock, flags);
39         if (sem->sleepers > 0) {
40                 sem->sleepers--;
41                 ret = 1;
42         }
43         spin_unlock_irqrestore(&semaphore_wake_lock, flags);
44         return ret;
45 }
46
47 /*
48  * waking_non_zero_interruptible:
49  *      1       got the lock
50  *      0       go to sleep
51  *      -EINTR  interrupted
52  *
53  * We must undo the sem->count down_interruptible() increment while we are
54  * protected by the spinlock in order to make atomic this atomic_inc() with the
55  * atomic_read() in wake_one_more(), otherwise we can race. -arca
56  */
57 static __inline__ int waking_non_zero_interruptible(struct semaphore *sem,
58                                                 struct task_struct *tsk)
59 {
60         unsigned long flags;
61         int ret = 0;
62
63         spin_lock_irqsave(&semaphore_wake_lock, flags);
64         if (sem->sleepers > 0) {
65                 sem->sleepers--;
66                 ret = 1;
67         } else if (signal_pending(tsk)) {
68                 atomic_inc(&sem->count);
69                 ret = -EINTR;
70         }
71         spin_unlock_irqrestore(&semaphore_wake_lock, flags);
72         return ret;
73 }
74
75 /*
76  * waking_non_zero_trylock:
77  *      1       failed to lock
78  *      0       got the lock
79  *
80  * We must undo the sem->count down_trylock() increment while we are
81  * protected by the spinlock in order to make atomic this atomic_inc() with the
82  * atomic_read() in wake_one_more(), otherwise we can race. -arca
83  */
84 static __inline__ int waking_non_zero_trylock(struct semaphore *sem)
85 {
86         unsigned long flags;
87         int ret = 1;
88
89         spin_lock_irqsave(&semaphore_wake_lock, flags);
90         if (sem->sleepers <= 0)
91                 atomic_inc(&sem->count);
92         else {
93                 sem->sleepers--;
94                 ret = 0;
95         }
96         spin_unlock_irqrestore(&semaphore_wake_lock, flags);
97         return ret;
98 }
99
100 #endif /* __ASM_SH64_SEMAPHORE_HELPER_H */