kill DECLARE_MUTEX_LOCKED
[sfrench/cifs-2.6.git] / include / asm-avr32 / semaphore.h
1 /*
2  * SMP- and interrupt-safe semaphores.
3  *
4  * Copyright (C) 2006 Atmel Corporation
5  *
6  * Based on include/asm-i386/semaphore.h
7  *   Copyright (C) 1996 Linus Torvalds
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #ifndef __ASM_AVR32_SEMAPHORE_H
14 #define __ASM_AVR32_SEMAPHORE_H
15
16 #include <linux/linkage.h>
17
18 #include <asm/system.h>
19 #include <asm/atomic.h>
20 #include <linux/wait.h>
21 #include <linux/rwsem.h>
22
23 struct semaphore {
24         atomic_t count;
25         int sleepers;
26         wait_queue_head_t wait;
27 };
28
29 #define __SEMAPHORE_INITIALIZER(name, n)                                \
30 {                                                                       \
31         .count          = ATOMIC_INIT(n),                               \
32         .wait           = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)    \
33 }
34
35 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
36         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
37
38 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39
40 static inline void sema_init (struct semaphore *sem, int val)
41 {
42         atomic_set(&sem->count, val);
43         sem->sleepers = 0;
44         init_waitqueue_head(&sem->wait);
45 }
46
47 static inline void init_MUTEX (struct semaphore *sem)
48 {
49         sema_init(sem, 1);
50 }
51
52 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
53 {
54         sema_init(sem, 0);
55 }
56
57 void __down(struct semaphore * sem);
58 int  __down_interruptible(struct semaphore * sem);
59 void __up(struct semaphore * sem);
60
61 /*
62  * This is ugly, but we want the default case to fall through.
63  * "__down_failed" is a special asm handler that calls the C
64  * routine that actually waits. See arch/i386/kernel/semaphore.c
65  */
66 static inline void down(struct semaphore * sem)
67 {
68         might_sleep();
69         if (unlikely(atomic_dec_return (&sem->count) < 0))
70                 __down (sem);
71 }
72
73 /*
74  * Interruptible try to acquire a semaphore.  If we obtained
75  * it, return zero.  If we were interrupted, returns -EINTR
76  */
77 static inline int down_interruptible(struct semaphore * sem)
78 {
79         int ret = 0;
80
81         might_sleep();
82         if (unlikely(atomic_dec_return (&sem->count) < 0))
83                 ret = __down_interruptible (sem);
84         return ret;
85 }
86
87 /*
88  * Non-blockingly attempt to down() a semaphore.
89  * Returns zero if we acquired it
90  */
91 static inline int down_trylock(struct semaphore * sem)
92 {
93         return atomic_dec_if_positive(&sem->count) < 0;
94 }
95
96 /*
97  * Note! This is subtle. We jump to wake people up only if
98  * the semaphore was negative (== somebody was waiting on it).
99  * The default case (no contention) will result in NO
100  * jumps for both down() and up().
101  */
102 static inline void up(struct semaphore * sem)
103 {
104         if (unlikely(atomic_inc_return (&sem->count) <= 0))
105                 __up (sem);
106 }
107
108 #endif /*__ASM_AVR32_SEMAPHORE_H */