semaphore.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <linux/linkage.h>
  16. #include <asm/system.h>
  17. #include <asm/atomic.h>
  18. #include <linux/wait.h>
  19. #include <linux/rwsem.h>
  20. struct semaphore {
  21. atomic_t count;
  22. int sleepers;
  23. wait_queue_head_t wait;
  24. };
  25. #define __SEMAPHORE_INITIALIZER(name, n) \
  26. { \
  27. .count = ATOMIC_INIT(n), \
  28. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  29. }
  30. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  31. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  32. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  33. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  34. static inline void sema_init (struct semaphore *sem, int val)
  35. {
  36. atomic_set(&sem->count, val);
  37. sem->sleepers = 0;
  38. init_waitqueue_head(&sem->wait);
  39. }
  40. static inline void init_MUTEX (struct semaphore *sem)
  41. {
  42. sema_init(sem, 1);
  43. }
  44. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  45. {
  46. sema_init(sem, 0);
  47. }
  48. void __down(struct semaphore * sem);
  49. int __down_interruptible(struct semaphore * sem);
  50. void __up(struct semaphore * sem);
  51. /*
  52. * This is ugly, but we want the default case to fall through.
  53. * "__down_failed" is a special asm handler that calls the C
  54. * routine that actually waits. See arch/i386/kernel/semaphore.c
  55. */
  56. static inline void down(struct semaphore * sem)
  57. {
  58. might_sleep();
  59. if (unlikely(atomic_dec_return (&sem->count) < 0))
  60. __down (sem);
  61. }
  62. /*
  63. * Interruptible try to acquire a semaphore. If we obtained
  64. * it, return zero. If we were interrupted, returns -EINTR
  65. */
  66. static inline int down_interruptible(struct semaphore * sem)
  67. {
  68. int ret = 0;
  69. might_sleep();
  70. if (unlikely(atomic_dec_return (&sem->count) < 0))
  71. ret = __down_interruptible (sem);
  72. return ret;
  73. }
  74. /*
  75. * Non-blockingly attempt to down() a semaphore.
  76. * Returns zero if we acquired it
  77. */
  78. static inline int down_trylock(struct semaphore * sem)
  79. {
  80. return atomic_dec_if_positive(&sem->count) < 0;
  81. }
  82. /*
  83. * Note! This is subtle. We jump to wake people up only if
  84. * the semaphore was negative (== somebody was waiting on it).
  85. * The default case (no contention) will result in NO
  86. * jumps for both down() and up().
  87. */
  88. static inline void up(struct semaphore * sem)
  89. {
  90. if (unlikely(atomic_inc_return (&sem->count) <= 0))
  91. __up (sem);
  92. }
  93. #endif /*__ASM_AVR32_SEMAPHORE_H */