semaphore.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /*
  41. * special register calling convention
  42. */
  43. asmlinkage void __down_failed(void);
  44. asmlinkage int __down_interruptible_failed(void);
  45. asmlinkage int __down_trylock_failed(void);
  46. asmlinkage void __up_wakeup(void);
  47. extern void __down(struct semaphore * sem);
  48. extern int __down_interruptible(struct semaphore * sem);
  49. extern int __down_trylock(struct semaphore * sem);
  50. extern void __up(struct semaphore * sem);
  51. /*
  52. * This is ugly, but we want the default case to fall through.
  53. * "__down" is the actual routine that waits...
  54. */
  55. static inline void down(struct semaphore * sem)
  56. {
  57. might_sleep();
  58. __down_op(sem, __down_failed);
  59. }
  60. /*
  61. * This is ugly, but we want the default case to fall through.
  62. * "__down_interruptible" is the actual routine that waits...
  63. */
  64. static inline int down_interruptible (struct semaphore * sem)
  65. {
  66. might_sleep();
  67. return __down_op_ret(sem, __down_interruptible_failed);
  68. }
  69. static inline int down_trylock(struct semaphore *sem)
  70. {
  71. return __down_op_ret(sem, __down_trylock_failed);
  72. }
  73. /*
  74. * Note! This is subtle. We jump to wake people up only if
  75. * the semaphore was negative (== somebody was waiting on it).
  76. * The default case (no contention) will result in NO
  77. * jumps for both down() and up().
  78. */
  79. static inline void up(struct semaphore * sem)
  80. {
  81. __up_op(sem, __up_wakeup);
  82. }
  83. #endif