semaphore.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. static inline void sema_init (struct semaphore *sem, int val)
  33. {
  34. /*
  35. * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
  36. *
  37. * i'd rather use the more flexible initialization above, but sadly
  38. * GCC 2.7.2.3 emits a bogus warning. EGCS doesn't. Oh well.
  39. */
  40. atomic_set(&sem->count, val);
  41. sem->sleepers = 0;
  42. init_waitqueue_head(&sem->wait);
  43. }
  44. static inline void init_MUTEX (struct semaphore *sem)
  45. {
  46. sema_init(sem, 1);
  47. }
  48. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  49. {
  50. sema_init(sem, 0);
  51. }
  52. #if 0
  53. asmlinkage void __down_failed(void /* special register calling convention */);
  54. asmlinkage int __down_failed_interruptible(void /* params in registers */);
  55. asmlinkage int __down_failed_trylock(void /* params in registers */);
  56. asmlinkage void __up_wakeup(void /* special register calling convention */);
  57. #endif
  58. asmlinkage void __down(struct semaphore * sem);
  59. asmlinkage int __down_interruptible(struct semaphore * sem);
  60. asmlinkage int __down_trylock(struct semaphore * sem);
  61. asmlinkage void __up(struct semaphore * sem);
  62. extern spinlock_t semaphore_wake_lock;
  63. static inline void down(struct semaphore * sem)
  64. {
  65. might_sleep();
  66. if (atomic_dec_return(&sem->count) < 0)
  67. __down(sem);
  68. }
  69. static inline int down_interruptible(struct semaphore * sem)
  70. {
  71. int ret = 0;
  72. might_sleep();
  73. if (atomic_dec_return(&sem->count) < 0)
  74. ret = __down_interruptible(sem);
  75. return ret;
  76. }
  77. static inline int down_trylock(struct semaphore * sem)
  78. {
  79. int ret = 0;
  80. if (atomic_dec_return(&sem->count) < 0)
  81. ret = __down_trylock(sem);
  82. return ret;
  83. }
  84. /*
  85. * Note! This is subtle. We jump to wake people up only if
  86. * the semaphore was negative (== somebody was waiting on it).
  87. */
  88. static inline void up(struct semaphore * sem)
  89. {
  90. if (atomic_inc_return(&sem->count) <= 0)
  91. __up(sem);
  92. }
  93. #endif
  94. #endif /* __ASM_SH_SEMAPHORE_H */