semaphore.h 2.3 KB

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