semaphore.h 2.8 KB

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