semaphore.h 2.4 KB

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