semaphore.h 3.0 KB

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