aboutsummaryrefslogtreecommitdiffstats
path: root/include/asm-mips/semaphore.h
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /include/asm-mips/semaphore.h
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'include/asm-mips/semaphore.h')
-rw-r--r--include/asm-mips/semaphore.h112
1 files changed, 112 insertions, 0 deletions
diff --git a/include/asm-mips/semaphore.h b/include/asm-mips/semaphore.h
new file mode 100644
index 000000000000..c2c97dec661b
--- /dev/null
+++ b/include/asm-mips/semaphore.h
@@ -0,0 +1,112 @@
1/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1996 Linus Torvalds
7 * Copyright (C) 1998, 99, 2000, 01, 04 Ralf Baechle
8 * Copyright (C) 1999, 2000, 01 Silicon Graphics, Inc.
9 * Copyright (C) 2000, 01 MIPS Technologies, Inc.
10 *
11 * In all honesty, little of the old MIPS code left - the PPC64 variant was
12 * just looking nice and portable so I ripped it. Credits to whoever wrote
13 * it.
14 */
15#ifndef __ASM_SEMAPHORE_H
16#define __ASM_SEMAPHORE_H
17
18/*
19 * Remove spinlock-based RW semaphores; RW semaphore definitions are
20 * now in rwsem.h and we use the generic lib/rwsem.c implementation.
21 * Rework semaphores to use atomic_dec_if_positive.
22 * -- Paul Mackerras (paulus@samba.org)
23 */
24
25#ifdef __KERNEL__
26
27#include <asm/atomic.h>
28#include <asm/system.h>
29#include <linux/wait.h>
30#include <linux/rwsem.h>
31
32struct semaphore {
33 /*
34 * Note that any negative value of count is equivalent to 0,
35 * but additionally indicates that some process(es) might be
36 * sleeping on `wait'.
37 */
38 atomic_t count;
39 wait_queue_head_t wait;
40};
41
42#define __SEMAPHORE_INITIALIZER(name, n) \
43{ \
44 .count = ATOMIC_INIT(n), \
45 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
46}
47
48#define __MUTEX_INITIALIZER(name) \
49 __SEMAPHORE_INITIALIZER(name, 1)
50
51#define __DECLARE_SEMAPHORE_GENERIC(name, count) \
52 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
53
54#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
55#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
56
57static inline void sema_init (struct semaphore *sem, int val)
58{
59 atomic_set(&sem->count, val);
60 init_waitqueue_head(&sem->wait);
61}
62
63static inline void init_MUTEX (struct semaphore *sem)
64{
65 sema_init(sem, 1);
66}
67
68static inline void init_MUTEX_LOCKED (struct semaphore *sem)
69{
70 sema_init(sem, 0);
71}
72
73extern void __down(struct semaphore * sem);
74extern int __down_interruptible(struct semaphore * sem);
75extern void __up(struct semaphore * sem);
76
77static inline void down(struct semaphore * sem)
78{
79 might_sleep();
80
81 /*
82 * Try to get the semaphore, take the slow path if we fail.
83 */
84 if (unlikely(atomic_dec_return(&sem->count) < 0))
85 __down(sem);
86}
87
88static inline int down_interruptible(struct semaphore * sem)
89{
90 int ret = 0;
91
92 might_sleep();
93
94 if (unlikely(atomic_dec_return(&sem->count) < 0))
95 ret = __down_interruptible(sem);
96 return ret;
97}
98
99static inline int down_trylock(struct semaphore * sem)
100{
101 return atomic_dec_if_positive(&sem->count) < 0;
102}
103
104static inline void up(struct semaphore * sem)
105{
106 if (unlikely(atomic_inc_return(&sem->count) <= 0))
107 __up(sem);
108}
109
110#endif /* __KERNEL__ */
111
112#endif /* __ASM_SEMAPHORE_H */