semaphore.h 2.9 KB

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