semaphore.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef __ASM_SH_SEMAPHORE_H
  2. #define __ASM_SH_SEMAPHORE_H
  3. #include <linux/linkage.h>
  4. #ifdef __KERNEL__
  5. /*
  6. * SMP- and interrupt-safe semaphores.
  7. *
  8. * (C) Copyright 1996 Linus Torvalds
  9. *
  10. * SuperH verison by Niibe Yutaka
  11. * (Currently no asm implementation but generic C code...)
  12. */
  13. #include <linux/spinlock.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/wait.h>
  16. #include <asm/system.h>
  17. #include <asm/atomic.h>
  18. struct semaphore {
  19. atomic_t count;
  20. int sleepers;
  21. wait_queue_head_t wait;
  22. };
  23. #define __SEMAPHORE_INITIALIZER(name, n) \
  24. { \
  25. .count = ATOMIC_INIT(n), \
  26. .sleepers = 0, \
  27. .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  28. }
  29. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  30. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  31. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  32. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  33. static inline void sema_init (struct semaphore *sem, int val)
  34. {
  35. /*
  36. * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
  37. *
  38. * i'd rather use the more flexible initialization above, but sadly
  39. * GCC 2.7.2.3 emits a bogus warning. EGCS doesn't. Oh well.
  40. */
  41. atomic_set(&sem->count, val);
  42. sem->sleepers = 0;
  43. init_waitqueue_head(&sem->wait);
  44. }
  45. static inline void init_MUTEX (struct semaphore *sem)
  46. {
  47. sema_init(sem, 1);
  48. }
  49. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  50. {
  51. sema_init(sem, 0);
  52. }
  53. #if 0
  54. asmlinkage void __down_failed(void /* special register calling convention */);
  55. asmlinkage int __down_failed_interruptible(void /* params in registers */);
  56. asmlinkage int __down_failed_trylock(void /* params in registers */);
  57. asmlinkage void __up_wakeup(void /* special register calling convention */);
  58. #endif
  59. asmlinkage void __down(struct semaphore * sem);
  60. asmlinkage int __down_interruptible(struct semaphore * sem);
  61. asmlinkage int __down_trylock(struct semaphore * sem);
  62. asmlinkage void __up(struct semaphore * sem);
  63. extern spinlock_t semaphore_wake_lock;
  64. static inline void down(struct semaphore * sem)
  65. {
  66. might_sleep();
  67. if (atomic_dec_return(&sem->count) < 0)
  68. __down(sem);
  69. }
  70. static inline int down_interruptible(struct semaphore * sem)
  71. {
  72. int ret = 0;
  73. might_sleep();
  74. if (atomic_dec_return(&sem->count) < 0)
  75. ret = __down_interruptible(sem);
  76. return ret;
  77. }
  78. static inline int down_trylock(struct semaphore * sem)
  79. {
  80. int ret = 0;
  81. if (atomic_dec_return(&sem->count) < 0)
  82. ret = __down_trylock(sem);
  83. return ret;
  84. }
  85. /*
  86. * Note! This is subtle. We jump to wake people up only if
  87. * the semaphore was negative (== somebody was waiting on it).
  88. */
  89. static inline void up(struct semaphore * sem)
  90. {
  91. if (atomic_inc_return(&sem->count) <= 0)
  92. __up(sem);
  93. }
  94. #endif
  95. #endif /* __ASM_SH_SEMAPHORE_H */