semaphore.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * License.  See the file "COPYING" in the main directory of this archive
  3.  * for more details.
  4.  *
  5.  * Copyright (C) 1996  Linus Torvalds
  6.  * Copyright (C) 1998, 1999, 2000, 2001  Ralf Baechle
  7.  * Copyright (C) 1999, 2000, 2001  Silicon Graphics, Inc.
  8.  */
  9. #ifndef _ASM_SEMAPHORE_H
  10. #define _ASM_SEMAPHORE_H
  11. #include <asm/system.h>
  12. #include <asm/atomic.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/wait.h>
  15. #include <linux/rwsem.h>
  16. struct semaphore {
  17. #ifdef __MIPSEB__
  18. atomic_t count;
  19. atomic_t waking;
  20. #else
  21. atomic_t waking;
  22. atomic_t count;
  23. #endif
  24. wait_queue_head_t wait;
  25. #if WAITQUEUE_DEBUG
  26. long __magic;
  27. #endif
  28. };
  29. #if WAITQUEUE_DEBUG
  30. # define __SEM_DEBUG_INIT(name) 
  31. , (long)&(name).__magic
  32. #else
  33. # define __SEM_DEBUG_INIT(name)
  34. #endif
  35. #ifdef __MIPSEB__
  36. #define __SEMAPHORE_INITIALIZER(name,count) 
  37. { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) 
  38. __SEM_DEBUG_INIT(name) }
  39. #else
  40. #define __SEMAPHORE_INITIALIZER(name,count) 
  41. { ATOMIC_INIT(0), ATOMIC_INIT(count), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) 
  42. __SEM_DEBUG_INIT(name) }
  43. #endif
  44. #define __MUTEX_INITIALIZER(name) 
  45. __SEMAPHORE_INITIALIZER(name,1)
  46. #define __DECLARE_SEMAPHORE_GENERIC(name,count) 
  47. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  48. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  49. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  50. static inline void sema_init (struct semaphore *sem, int val)
  51. {
  52. atomic_set(&sem->count, val);
  53. atomic_set(&sem->waking, 0);
  54. init_waitqueue_head(&sem->wait);
  55. #if WAITQUEUE_DEBUG
  56. sem->__magic = (long)&sem->__magic;
  57. #endif
  58. }
  59. static inline void init_MUTEX (struct semaphore *sem)
  60. {
  61. sema_init(sem, 1);
  62. }
  63. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  64. {
  65. sema_init(sem, 0);
  66. }
  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. static inline void down(struct semaphore * sem)
  72. {
  73. #if WAITQUEUE_DEBUG
  74. CHECK_MAGIC(sem->__magic);
  75. #endif
  76. if (atomic_dec_return(&sem->count) < 0)
  77. __down(sem);
  78. }
  79. /*
  80.  * Interruptible try to acquire a semaphore.  If we obtained
  81.  * it, return zero.  If we were interrupted, returns -EINTR
  82.  */
  83. static inline int down_interruptible(struct semaphore * sem)
  84. {
  85. int ret = 0;
  86. #if WAITQUEUE_DEBUG
  87. CHECK_MAGIC(sem->__magic);
  88. #endif
  89. if (atomic_dec_return(&sem->count) < 0)
  90. ret = __down_interruptible(sem);
  91. return ret;
  92. }
  93. /*
  94.  * down_trylock returns 0 on success, 1 if we failed to get the lock.
  95.  *
  96.  * We must manipulate count and waking simultaneously and atomically.
  97.  * Here, we do this by using lld/scd on the pair of 32-bit words.
  98.  *
  99.  * Pseudocode:
  100.  *
  101.  *   Decrement(sem->count)
  102.  *   If(sem->count >=0) {
  103.  * Return(SUCCESS) // resource is free
  104.  *   } else {
  105.  * If(sem->waking <= 0) { // if no wakeup pending
  106.  *    Increment(sem->count) // undo decrement
  107.  *    Return(FAILURE)
  108.  *      } else {
  109.  *    Decrement(sem->waking) // otherwise "steal" wakeup
  110.  *    Return(SUCCESS)
  111.  * }
  112.  *   }
  113.  */
  114. static inline int down_trylock(struct semaphore * sem)
  115. {
  116. long ret, tmp, tmp2, sub;
  117. #if WAITQUEUE_DEBUG
  118. CHECK_MAGIC(sem->__magic);
  119. #endif
  120. __asm__ __volatile__(
  121. ".settmips3ttt# down_trylockn"
  122. "0:tlldt%1, %4nt"
  123. "tdlit%3, 0x0000000100000000nt"
  124. "tdsubut%1, %3nt"
  125. "tlit%0, 0nt"
  126. "tbgezt%1, 2fnt"
  127. "tsllt%2, %1, 0nt"
  128. "tblezt%2, 1fnt"
  129. "tdaddiut%1, %1, -1nt"
  130. "tbt2fn"
  131. "1:tdaddut%1, %1, %3nt"
  132. "tlit%0, 1n"
  133. "2:tscdt%1, %4nt"
  134. "tbeqzt%1, 0bnt"
  135. "t.settmips0"
  136. : "=&r"(ret), "=&r"(tmp), "=&r"(tmp2), "=&r"(sub)
  137. : "m"(*sem)
  138. : "memory");
  139. return ret;
  140. }
  141. /*
  142.  * Note! This is subtle. We jump to wake people up only if
  143.  * the semaphore was negative (== somebody was waiting on it).
  144.  */
  145. static inline void up(struct semaphore * sem)
  146. {
  147. #if WAITQUEUE_DEBUG
  148. CHECK_MAGIC(sem->__magic);
  149. #endif
  150. if (atomic_inc_return(&sem->count) <= 0)
  151. __up(sem);
  152. }
  153. static inline int sem_getcount(struct semaphore *sem)
  154. {
  155. return atomic_read(&sem->count);
  156. }
  157. #endif /* _ASM_SEMAPHORE_H */