semaphore.h 2.6 KB

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