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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _SPARC64_SEMAPHORE_H
  2. #define _SPARC64_SEMAPHORE_H
  3. /* These are actually reasonable on the V9.
  4.  *
  5.  * See asm-ppc/semaphore.h for implementation commentary,
  6.  * only sparc64 specific issues are commented here.
  7.  */
  8. #ifdef __KERNEL__
  9. #include <asm/atomic.h>
  10. #include <asm/system.h>
  11. #include <linux/wait.h>
  12. #include <linux/rwsem.h>
  13. struct semaphore {
  14. atomic_t count;
  15. wait_queue_head_t wait;
  16. };
  17. #define __SEMAPHORE_INITIALIZER(name, count) 
  18. { ATOMIC_INIT(count), 
  19.   __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) }
  20. #define __MUTEX_INITIALIZER(name) 
  21. __SEMAPHORE_INITIALIZER(name, 1)
  22. #define __DECLARE_SEMAPHORE_GENERIC(name, count) 
  23. struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  24. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
  25. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
  26. static inline void sema_init (struct semaphore *sem, int val)
  27. {
  28. atomic_set(&sem->count, val);
  29. init_waitqueue_head(&sem->wait);
  30. }
  31. static inline void init_MUTEX (struct semaphore *sem)
  32. {
  33. sema_init(sem, 1);
  34. }
  35. static inline void init_MUTEX_LOCKED (struct semaphore *sem)
  36. {
  37. sema_init(sem, 0);
  38. }
  39. extern void __down(struct semaphore * sem);
  40. extern int  __down_interruptible(struct semaphore * sem);
  41. extern void __up(struct semaphore * sem);
  42. static __inline__ void down(struct semaphore * sem)
  43. {
  44. /* This atomically does:
  45.  *  old_val = sem->count;
  46.  * new_val = sem->count - 1;
  47.  * sem->count = new_val;
  48.  * if (old_val < 1)
  49.  * __down(sem);
  50.  *
  51.  * The (old_val < 1) test is equivalent to
  52.  * the more straightforward (new_val < 0),
  53.  * but it is easier to test the former because
  54.  * of how the CAS instruction works.
  55.  */
  56. __asm__ __volatile__("n"
  57. " ! down sem(%0)n"
  58. "1: lduw [%0], %%g5n"
  59. " sub %%g5, 1, %%g7n"
  60. " cas [%0], %%g5, %%g7n"
  61. " cmp %%g5, %%g7n"
  62. " bne,pn %%icc, 1bn"
  63. "  cmp %%g7, 1n"
  64. " bl,pn %%icc, 3fn"
  65. "  membar #StoreLoad | #StoreStoren"
  66. "2:n"
  67. " .subsection 2n"
  68. "3: mov %0, %%g5n"
  69. " save %%sp, -160, %%spn"
  70. " mov %%g1, %%l1n"
  71. " mov %%g2, %%l2n"
  72. " mov %%g3, %%l3n"
  73. " call %1n"
  74. "  mov %%g5, %%o0n"
  75. " mov %%l1, %%g1n"
  76. " mov %%l2, %%g2n"
  77. " ba,pt %%xcc, 2bn"
  78. "  restore %%l3, %%g0, %%g3n"
  79. " .previousn"
  80. : : "r" (sem), "i" (__down)
  81. : "g5", "g7", "memory", "cc");
  82. }
  83. static __inline__ int down_interruptible(struct semaphore *sem)
  84. {
  85. int ret = 0;
  86. /* This atomically does:
  87.  *  old_val = sem->count;
  88.  * new_val = sem->count - 1;
  89.  * sem->count = new_val;
  90.  * if (old_val < 1)
  91.  * ret = __down_interruptible(sem);
  92.  *
  93.  * The (old_val < 1) test is equivalent to
  94.  * the more straightforward (new_val < 0),
  95.  * but it is easier to test the former because
  96.  * of how the CAS instruction works.
  97.  */
  98. __asm__ __volatile__("n"
  99. " ! down_interruptible sem(%2) ret(%0)n"
  100. "1: lduw [%2], %%g5n"
  101. " sub %%g5, 1, %%g7n"
  102. " cas [%2], %%g5, %%g7n"
  103. " cmp %%g5, %%g7n"
  104. " bne,pn %%icc, 1bn"
  105. "  cmp %%g7, 1n"
  106. " bl,pn %%icc, 3fn"
  107. "  membar #StoreLoad | #StoreStoren"
  108. "2:n"
  109. " .subsection 2n"
  110. "3: mov %2, %%g5n"
  111. " save %%sp, -160, %%spn"
  112. " mov %%g1, %%l1n"
  113. " mov %%g2, %%l2n"
  114. " mov %%g3, %%l3n"
  115. " call %3n"
  116. "  mov %%g5, %%o0n"
  117. " mov %%l1, %%g1n"
  118. " mov %%l2, %%g2n"
  119. " mov %%l3, %%g3n"
  120. " ba,pt %%xcc, 2bn"
  121. "  restore %%o0, %%g0, %0n"
  122. " .previousn"
  123. : "=r" (ret)
  124. : "0" (ret), "r" (sem), "i" (__down_interruptible)
  125. : "g5", "g7", "memory", "cc");
  126. return ret;
  127. }
  128. static __inline__ int down_trylock(struct semaphore *sem)
  129. {
  130. int ret;
  131. /* This atomically does:
  132.  *  old_val = sem->count;
  133.  * new_val = sem->count - 1;
  134.  * if (old_val < 1) {
  135.  * ret = 1;
  136.  * } else {
  137.  * sem->count = new_val;
  138.  * ret = 0;
  139.  * }
  140.  *
  141.  * The (old_val < 1) test is equivalent to
  142.  * the more straightforward (new_val < 0),
  143.  * but it is easier to test the former because
  144.  * of how the CAS instruction works.
  145.  */
  146. __asm__ __volatile__("n"
  147. " ! down_trylock sem(%1) ret(%0)n"
  148. "1: lduw [%1], %%g5n"
  149. " sub %%g5, 1, %%g7n"
  150. " cmp %%g5, 1n"
  151. " bl,pn %%icc, 2fn"
  152. "  mov 1, %0n"
  153. " cas [%1], %%g5, %%g7n"
  154. " cmp %%g5, %%g7n"
  155. " bne,pn %%icc, 1bn"
  156. "  mov 0, %0n"
  157. " membar #StoreLoad | #StoreStoren"
  158. "2:n"
  159. : "=&r" (ret)
  160. : "r" (sem)
  161. : "g5", "g7", "memory", "cc");
  162. return ret;
  163. }
  164. static __inline__ void up(struct semaphore * sem)
  165. {
  166. /* This atomically does:
  167.  *  old_val = sem->count;
  168.  * new_val = sem->count + 1;
  169.  * sem->count = new_val;
  170.  * if (old_val < 0)
  171.  * __up(sem);
  172.  *
  173.  * The (old_val < 0) test is equivalent to
  174.  * the more straightforward (new_val <= 0),
  175.  * but it is easier to test the former because
  176.  * of how the CAS instruction works.
  177.  */
  178. __asm__ __volatile__("n"
  179. " ! up sem(%0)n"
  180. " membar #StoreLoad | #LoadLoadn"
  181. "1: lduw [%0], %%g5n"
  182. " add %%g5, 1, %%g7n"
  183. " cas [%0], %%g5, %%g7n"
  184. " cmp %%g5, %%g7n"
  185. " bne,pn %%icc, 1bn"
  186. "  addcc %%g7, 1, %%g0n"
  187. " ble,pn %%icc, 3fn"
  188. "  membar #StoreLoad | #StoreStoren"
  189. "2:n"
  190. " .subsection 2n"
  191. "3: mov %0, %%g5n"
  192. " save %%sp, -160, %%spn"
  193. " mov %%g1, %%l1n"
  194. " mov %%g2, %%l2n"
  195. " mov %%g3, %%l3n"
  196. " call %1n"
  197. "  mov %%g5, %%o0n"
  198. " mov %%l1, %%g1n"
  199. " mov %%l2, %%g2n"
  200. " ba,pt %%xcc, 2bn"
  201. "  restore %%l3, %%g0, %%g3n"
  202. " .previousn"
  203. : : "r" (sem), "i" (__up)
  204. : "g5", "g7", "memory", "cc");
  205. }
  206. static inline int sem_getcount(struct semaphore *sem)
  207. {
  208. return atomic_read(&sem->count);
  209. }
  210. #endif /* __KERNEL__ */
  211. #endif /* !(_SPARC64_SEMAPHORE_H) */