2 * SMP- and interrupt-safe semaphores.
4 * Copyright (C) 2006 Atmel Corporation
6 * Based on include/asm-i386/semaphore.h
7 * Copyright (C) 1996 Linus Torvalds
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.
13 #ifndef __ASM_AVR32_SEMAPHORE_H
14 #define __ASM_AVR32_SEMAPHORE_H
16 #include <linux/linkage.h>
18 #include <asm/system.h>
19 #include <asm/atomic.h>
20 #include <linux/wait.h>
21 #include <linux/rwsem.h>
26 wait_queue_head_t wait;
29 #define __SEMAPHORE_INITIALIZER(name, n) \
31 .count = ATOMIC_INIT(n), \
32 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
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)
40 static inline void sema_init (struct semaphore *sem, int val)
42 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 void __down(struct semaphore * sem);
58 int __down_interruptible(struct semaphore * sem);
59 void __up(struct semaphore * sem);
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
66 static inline void down(struct semaphore * sem)
69 if (unlikely(atomic_dec_return (&sem->count) < 0))
74 * Interruptible try to acquire a semaphore. If we obtained
75 * it, return zero. If we were interrupted, returns -EINTR
77 static inline int down_interruptible(struct semaphore * sem)
82 if (unlikely(atomic_dec_return (&sem->count) < 0))
83 ret = __down_interruptible (sem);
88 * Non-blockingly attempt to down() a semaphore.
89 * Returns zero if we acquired it
91 static inline int down_trylock(struct semaphore * sem)
93 return atomic_dec_if_positive(&sem->count) < 0;
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().
102 static inline void up(struct semaphore * sem)
104 if (unlikely(atomic_inc_return (&sem->count) <= 0))
108 #endif /*__ASM_AVR32_SEMAPHORE_H */