2 * linux/include/asm-xtensa/semaphore.h
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
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
11 #ifndef _XTENSA_SEMAPHORE_H
12 #define _XTENSA_SEMAPHORE_H
14 #include <asm/atomic.h>
15 #include <asm/system.h>
16 #include <linux/wait.h>
17 #include <linux/rwsem.h>
22 wait_queue_head_t wait
;
25 #define __SEMAPHORE_INITIALIZER(name,n) \
27 .count = ATOMIC_INIT(n), \
29 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
32 #define __MUTEX_INITIALIZER(name) \
33 __SEMAPHORE_INITIALIZER(name, 1)
35 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
36 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
38 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
41 static inline void sema_init (struct semaphore
*sem
, int val
)
43 atomic_set(&sem
->count
, val
);
44 init_waitqueue_head(&sem
->wait
);
47 static inline void init_MUTEX (struct semaphore
*sem
)
52 static inline void init_MUTEX_LOCKED (struct semaphore
*sem
)
57 asmlinkage
void __down(struct semaphore
* sem
);
58 asmlinkage
int __down_interruptible(struct semaphore
* sem
);
59 asmlinkage
int __down_trylock(struct semaphore
* sem
);
60 asmlinkage
void __up(struct semaphore
* sem
);
62 extern spinlock_t semaphore_wake_lock
;
64 static inline void down(struct semaphore
* sem
)
68 if (atomic_sub_return(1, &sem
->count
) < 0)
72 static inline int down_interruptible(struct semaphore
* sem
)
78 if (atomic_sub_return(1, &sem
->count
) < 0)
79 ret
= __down_interruptible(sem
);
83 static inline int down_trylock(struct semaphore
* sem
)
87 if (atomic_sub_return(1, &sem
->count
) < 0)
88 ret
= __down_trylock(sem
);
93 * Note! This is subtle. We jump to wake people up only if
94 * the semaphore was negative (== somebody was waiting on it).
96 static inline void up(struct semaphore
* sem
)
98 if (atomic_add_return(1, &sem
->count
) <= 0)
102 #endif /* _XTENSA_SEMAPHORE_H */