semaphore.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * linux/include/asm-xtensa/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. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_SEMAPHORE_H
  11. #define _XTENSA_SEMAPHORE_H
  12. #include <asm/atomic.h>
  13. #include <asm/system.h>
  14. #include <linux/wait.h>
  15. #include <linux/rwsem.h>
  16. struct semaphore {
  17. atomic_t count;
  18. int sleepers;
  19. wait_queue_head_t wait;
  20. #if WAITQUEUE_DEBUG
  21. long __magic;
  22. #endif
  23. };
  24. #if WAITQUEUE_DEBUG
  25. # define __SEM_DEBUG_INIT(name) \
  26. , (int)&(name).__magic
  27. #else
  28. # define __SEM_DEBUG_INIT(name)
  29. #endif
  30. #define __SEMAPHORE_INITIALIZER(name,count) \
  31. { ATOMIC_INIT(count), \
  32. 0, \
  33. __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  34. __SEM_DEBUG_INIT(name) }
  35. #define __MUTEX_INITIALIZER(name) \
  36. __SEMAPHORE_INITIALIZER(name, 1)
  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. init_waitqueue_head(&sem->wait);
  51. #if WAITQUEUE_DEBUG
  52. sem->__magic = (int)&sem->__magic;
  53. #endif
  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. asmlinkage void __down(struct semaphore * sem);
  64. asmlinkage int __down_interruptible(struct semaphore * sem);
  65. asmlinkage int __down_trylock(struct semaphore * sem);
  66. asmlinkage void __up(struct semaphore * sem);
  67. extern spinlock_t semaphore_wake_lock;
  68. static inline void down(struct semaphore * sem)
  69. {
  70. #if WAITQUEUE_DEBUG
  71. CHECK_MAGIC(sem->__magic);
  72. #endif
  73. if (atomic_sub_return(1, &sem->count) < 0)
  74. __down(sem);
  75. }
  76. static inline int down_interruptible(struct semaphore * sem)
  77. {
  78. int ret = 0;
  79. #if WAITQUEUE_DEBUG
  80. CHECK_MAGIC(sem->__magic);
  81. #endif
  82. if (atomic_sub_return(1, &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 WAITQUEUE_DEBUG
  90. CHECK_MAGIC(sem->__magic);
  91. #endif
  92. if (atomic_sub_return(1, &sem->count) < 0)
  93. ret = __down_trylock(sem);
  94. return ret;
  95. }
  96. /*
  97. * Note! This is subtle. We jump to wake people up only if
  98. * the semaphore was negative (== somebody was waiting on it).
  99. */
  100. static inline void up(struct semaphore * sem)
  101. {
  102. #if WAITQUEUE_DEBUG
  103. CHECK_MAGIC(sem->__magic);
  104. #endif
  105. if (atomic_add_return(1, &sem->count) <= 0)
  106. __up(sem);
  107. }
  108. #endif /* _XTENSA_SEMAPHORE_H */