semaphore.h 2.2 KB

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