blob: 09e89ab3eb61ef9a5b189509cae8f201ac1fe032 [file] [log] [blame]
Chris Zankel9a8fd552005-06-23 22:01:26 -07001/*
2 * linux/include/asm-xtensa/semaphore.h
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
7 *
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
9 */
10
11#ifndef _XTENSA_SEMAPHORE_H
12#define _XTENSA_SEMAPHORE_H
13
14#include <asm/atomic.h>
15#include <asm/system.h>
16#include <linux/wait.h>
17#include <linux/rwsem.h>
18
19struct semaphore {
20 atomic_t count;
21 int sleepers;
22 wait_queue_head_t wait;
Chris Zankel9a8fd552005-06-23 22:01:26 -070023};
24
Chris Zankel288a60c2005-09-22 21:44:23 -070025#define __SEMAPHORE_INITIALIZER(name,n) \
26{ \
27 .count = ATOMIC_INIT(n), \
28 .sleepers = 0, \
29 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
30}
Chris Zankel9a8fd552005-06-23 22:01:26 -070031
Chris Zankel288a60c2005-09-22 21:44:23 -070032#define __MUTEX_INITIALIZER(name) \
Chris Zankel9a8fd552005-06-23 22:01:26 -070033 __SEMAPHORE_INITIALIZER(name, 1)
34
Chris Zankel288a60c2005-09-22 21:44:23 -070035#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
Chris Zankel9a8fd552005-06-23 22:01:26 -070036 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
37
38#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
40
Adrian Bunkd99cf712005-09-03 15:57:53 -070041static inline void sema_init (struct semaphore *sem, int val)
Chris Zankel9a8fd552005-06-23 22:01:26 -070042{
Chris Zankel9a8fd552005-06-23 22:01:26 -070043 atomic_set(&sem->count, val);
44 init_waitqueue_head(&sem->wait);
Chris Zankel9a8fd552005-06-23 22:01:26 -070045}
46
47static inline void init_MUTEX (struct semaphore *sem)
48{
49 sema_init(sem, 1);
50}
51
52static inline void init_MUTEX_LOCKED (struct semaphore *sem)
53{
54 sema_init(sem, 0);
55}
56
57asmlinkage void __down(struct semaphore * sem);
58asmlinkage int __down_interruptible(struct semaphore * sem);
59asmlinkage int __down_trylock(struct semaphore * sem);
60asmlinkage void __up(struct semaphore * sem);
61
62extern spinlock_t semaphore_wake_lock;
63
Adrian Bunkd99cf712005-09-03 15:57:53 -070064static inline void down(struct semaphore * sem)
Chris Zankel9a8fd552005-06-23 22:01:26 -070065{
Chris Zankel288a60c2005-09-22 21:44:23 -070066 might_sleep();
Chris Zankel9a8fd552005-06-23 22:01:26 -070067
68 if (atomic_sub_return(1, &sem->count) < 0)
69 __down(sem);
70}
71
Adrian Bunkd99cf712005-09-03 15:57:53 -070072static inline int down_interruptible(struct semaphore * sem)
Chris Zankel9a8fd552005-06-23 22:01:26 -070073{
74 int ret = 0;
Chris Zankel288a60c2005-09-22 21:44:23 -070075
76 might_sleep();
Chris Zankel9a8fd552005-06-23 22:01:26 -070077
78 if (atomic_sub_return(1, &sem->count) < 0)
79 ret = __down_interruptible(sem);
80 return ret;
81}
82
Adrian Bunkd99cf712005-09-03 15:57:53 -070083static inline int down_trylock(struct semaphore * sem)
Chris Zankel9a8fd552005-06-23 22:01:26 -070084{
85 int ret = 0;
Chris Zankel9a8fd552005-06-23 22:01:26 -070086
87 if (atomic_sub_return(1, &sem->count) < 0)
88 ret = __down_trylock(sem);
89 return ret;
90}
91
92/*
93 * Note! This is subtle. We jump to wake people up only if
94 * the semaphore was negative (== somebody was waiting on it).
95 */
Adrian Bunkd99cf712005-09-03 15:57:53 -070096static inline void up(struct semaphore * sem)
Chris Zankel9a8fd552005-06-23 22:01:26 -070097{
Chris Zankel9a8fd552005-06-23 22:01:26 -070098 if (atomic_add_return(1, &sem->count) <= 0)
99 __up(sem);
100}
101
102#endif /* _XTENSA_SEMAPHORE_H */