semaphore.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * linux/include/asm-arm/semaphore.h
  3. */
  4. #ifndef __ASM_ARM_SEMAPHORE_H
  5. #define __ASM_ARM_SEMAPHORE_H
  6. #include <linux/linkage.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/wait.h>
  9. #include <linux/rwsem.h>
  10. #include <asm/atomic.h>
  11. #include <asm/locks.h>
  12. struct semaphore {
  13. atomic_t count;
  14. int sleepers;
  15. wait_queue_head_t wait;
  16. };
  17. #define __SEMAPHORE_INIT(name, n) \
  18. { \
  19. .count = ATOMIC_INIT(n), \
  20. .sleepers = 0, \
  21. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \
  22. }
  23. #define __MUTEX_INITIALIZER(name) \
  24. __SEMAPHORE_INIT(name,1)
  25. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  26. struct semaphore name = __SEMAPHORE_INIT(name,count)
  27. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  28. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  29. static inline void sema_init(struct semaphore *sem, int val)
  30. {
  31. atomic_set(&sem->count, val);
  32. sem->sleepers = 0;
  33. init_waitqueue_head(&sem->wait);
  34. }
  35. static inline void init_MUTEX(struct semaphore *sem)
  36. {
  37. sema_init(sem, 1);
  38. }
  39. static inline void init_MUTEX_LOCKED(struct semaphore *sem)
  40. {
  41. sema_init(sem, 0);
  42. }
  43. /*
  44. * special register calling convention
  45. */
  46. asmlinkage void __down_failed(void);
  47. asmlinkage int __down_interruptible_failed(void);
  48. asmlinkage int __down_trylock_failed(void);
  49. asmlinkage void __up_wakeup(void);
  50. extern void __down(struct semaphore * sem);
  51. extern int __down_interruptible(struct semaphore * sem);
  52. extern int __down_trylock(struct semaphore * sem);
  53. extern void __up(struct semaphore * sem);
  54. /*
  55. * This is ugly, but we want the default case to fall through.
  56. * "__down" is the actual routine that waits...
  57. */
  58. static inline void down(struct semaphore * sem)
  59. {
  60. might_sleep();
  61. __down_op(sem, __down_failed);
  62. }
  63. /*
  64. * This is ugly, but we want the default case to fall through.
  65. * "__down_interruptible" is the actual routine that waits...
  66. */
  67. static inline int down_interruptible (struct semaphore * sem)
  68. {
  69. might_sleep();
  70. return __down_op_ret(sem, __down_interruptible_failed);
  71. }
  72. static inline int down_trylock(struct semaphore *sem)
  73. {
  74. return __down_op_ret(sem, __down_trylock_failed);
  75. }
  76. /*
  77. * Note! This is subtle. We jump to wake people up only if
  78. * the semaphore was negative (== somebody was waiting on it).
  79. * The default case (no contention) will result in NO
  80. * jumps for both down() and up().
  81. */
  82. static inline void up(struct semaphore * sem)
  83. {
  84. __up_op(sem, __up_wakeup);
  85. }
  86. #endif