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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for x86_64+
  2.  *
  3.  * Written by David Howells (dhowells@redhat.com).
  4.  * Ported by Andi Kleen <ak@suse.de> to x86-64.
  5.  *
  6.  * Derived from asm-i386/semaphore.h and asm-i386/rwsem.h
  7.  *
  8.  * Trylock by Brian Watson (Brian.J.Watson@compaq.com).
  9.  *
  10.  * The MSW of the count is the negated number of active writers and waiting
  11.  * lockers, and the LSW is the total number of active locks
  12.  *
  13.  * The lock count is initialized to 0 (no active and no waiting lockers).
  14.  *
  15.  * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
  16.  * uncontended lock. This can be determined because XADD returns the old value.
  17.  * Readers increment by 1 and see a positive value when uncontended, negative
  18.  * if there are writers (and maybe) readers waiting (in which case it goes to
  19.  * sleep).
  20.  *
  21.  * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
  22.  * be extended to 65534 by manually checking the whole MSW rather than relying
  23.  * on the S flag.
  24.  *
  25.  * The value of ACTIVE_BIAS supports up to 65535 active processes.
  26.  *
  27.  * This should be totally fair - if anything is waiting, a process that wants a
  28.  * lock will go to the back of the queue. When the currently active lock is
  29.  * released, if there's a writer at the front of the queue, then that and only
  30.  * that will be woken up; if there's a bunch of consequtive readers at the
  31.  * front, then they'll all be woken up, but no other readers will be.
  32.  */
  33. #ifndef _X8664_RWSEM_H
  34. #define _X8664_RWSEM_H
  35. #ifndef _LINUX_RWSEM_H
  36. #error please dont include asm/rwsem.h directly, use linux/rwsem.h instead
  37. #endif
  38. #ifdef __KERNEL__
  39. #include <linux/list.h>
  40. #include <linux/spinlock.h>
  41. struct rwsem_waiter;
  42. extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
  43. extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
  44. extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
  45. /*
  46.  * the semaphore definition
  47.  */
  48. struct rw_semaphore {
  49. signed int count;
  50. #define RWSEM_UNLOCKED_VALUE 0x00000000
  51. #define RWSEM_ACTIVE_BIAS 0x00000001
  52. #define RWSEM_ACTIVE_MASK 0x0000ffff
  53. #define RWSEM_WAITING_BIAS (-0x00010000)
  54. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  55. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  56. spinlock_t wait_lock;
  57. struct list_head wait_list;
  58. #if RWSEM_DEBUG
  59. int debug;
  60. #endif
  61. };
  62. /*
  63.  * initialisation
  64.  */
  65. #if RWSEM_DEBUG
  66. #define __RWSEM_DEBUG_INIT      , 0
  67. #else
  68. #define __RWSEM_DEBUG_INIT /* */
  69. #endif
  70. #define __RWSEM_INITIALIZER(name) 
  71. { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) 
  72. __RWSEM_DEBUG_INIT }
  73. #define DECLARE_RWSEM(name) 
  74. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  75. static inline void init_rwsem(struct rw_semaphore *sem)
  76. {
  77. sem->count = RWSEM_UNLOCKED_VALUE;
  78. spin_lock_init(&sem->wait_lock);
  79. INIT_LIST_HEAD(&sem->wait_list);
  80. #if RWSEM_DEBUG
  81. sem->debug = 0;
  82. #endif
  83. }
  84. /*
  85.  * lock for reading
  86.  */
  87. static inline void __down_read(struct rw_semaphore *sem)
  88. {
  89. __asm__ __volatile__(
  90. "# beginning down_readnt"
  91. LOCK_PREFIX "  incl      (%%rdi)nt" /* adds 0x00000001, returns the old value */
  92. "  js        2fnt" /* jump if we weren't granted the lock */
  93. "1:nt"
  94. LOCK_SECTION_START("") 
  95. "2:nt"
  96. "  call      rwsem_down_read_failed_thunknt"
  97. "  jmp       1bn"
  98. LOCK_SECTION_END 
  99. "# ending down_readnt"
  100. : "+m"(sem->count)
  101. : "D"(sem)
  102. : "memory", "cc");
  103. }
  104. /*
  105.  * trylock for reading -- returns 1 if successful, 0 if contention
  106.  */
  107. static inline int __down_read_trylock(struct rw_semaphore *sem)
  108. {
  109. __s32 result, tmp;
  110. __asm__ __volatile__(
  111. "# beginning __down_read_trylocknt"
  112. "  movl      %0,%1nt"
  113. "1:nt"
  114. "  movl      %1,%2nt"
  115. "  addl      %3,%2nt"
  116. "  jle      2fnt"
  117. LOCK_PREFIX "  cmpxchgl  %2,%0nt"
  118. "  jnz      1bnt"
  119. "2:nt"
  120. "# ending __down_read_trylocknt"
  121. : "+m"(sem->count), "=&a"(result), "=&r"(tmp)
  122. : "i"(RWSEM_ACTIVE_READ_BIAS)
  123. : "memory", "cc");
  124. return result>=0 ? 1 : 0;
  125. }
  126. /*
  127.  * lock for writing
  128.  */
  129. static inline void __down_write(struct rw_semaphore *sem)
  130. {
  131. int tmp;
  132. tmp = RWSEM_ACTIVE_WRITE_BIAS;
  133. __asm__ __volatile__(
  134. "# beginning down_writent"
  135. LOCK_PREFIX "  xaddl      %0,(%%rdi)nt" /* subtract 0x0000ffff, returns the old value */
  136. "  testl     %0,%0nt" /* was the count 0 before? */
  137. "  jnz       2fnt" /* jump if we weren't granted the lock */
  138. "1:nt"
  139. LOCK_SECTION_START("")
  140. "2:nt"
  141. "  call      rwsem_down_write_failed_thunknt"
  142. "  jmp       1bn"
  143. LOCK_SECTION_END
  144. "# ending down_write"
  145. : "=&r" (tmp) 
  146. : "0"(tmp), "D"(sem)
  147. : "memory", "cc");
  148. }
  149. /*
  150.  * trylock for writing -- returns 1 if successful, 0 if contention
  151.  */
  152. static inline int __down_write_trylock(struct rw_semaphore *sem)
  153. {
  154. signed long ret = cmpxchg(&sem->count,
  155.   RWSEM_UNLOCKED_VALUE, 
  156.   RWSEM_ACTIVE_WRITE_BIAS);
  157. if (ret == RWSEM_UNLOCKED_VALUE)
  158. return 1;
  159. return 0;
  160. }
  161. /*
  162.  * unlock after reading
  163.  */
  164. static inline void __up_read(struct rw_semaphore *sem)
  165. {
  166. __s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
  167. __asm__ __volatile__(
  168. "# beginning __up_readnt"
  169. LOCK_PREFIX "  xaddl      %%edx,(%%rdi)nt" /* subtracts 1, returns the old value */
  170. "  js        2fnt" /* jump if the lock is being waited upon */
  171. "1:nt"
  172. LOCK_SECTION_START("")
  173. "2:nt"
  174. "  decw      %%dxnt" /* do nothing if still outstanding active readers */
  175. "  jnz       1bnt"
  176. "  call      rwsem_wake_thunknt"
  177. "  jmp       1bn"
  178. LOCK_SECTION_END
  179. "# ending __up_readn"
  180. : "+m"(sem->count), "+d"(tmp)
  181. : "D"(sem)
  182. : "memory", "cc");
  183. }
  184. /*
  185.  * unlock after writing
  186.  */
  187. static inline void __up_write(struct rw_semaphore *sem)
  188. {
  189. __asm__ __volatile__(
  190. "# beginning __up_writent"
  191. "  movl      %2,%%edxnt"
  192. LOCK_PREFIX "  xaddl     %%edx,(%%rdi)nt" /* tries to transition 0xffff0001 -> 0x00000000 */
  193. "  jnz       2fnt" /* jump if the lock is being waited upon */
  194. "1:nt"
  195. LOCK_SECTION_START("")
  196. "2:nt"
  197. "  decw      %%dxnt" /* did the active count reduce to 0? */
  198. "  jnz       1bnt" /* jump back if not */
  199. "  call      rwsem_wake_thunknt"
  200. "  jmp       1bn"
  201. LOCK_SECTION_END
  202. "# ending __up_writen"
  203. : "+m"(sem->count)
  204. : "D"(sem), "i"(-RWSEM_ACTIVE_WRITE_BIAS)
  205. : "memory", "cc", "rdx");
  206. }
  207. /*
  208.  * implement atomic add functionality
  209.  */
  210. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  211. {
  212. __asm__ __volatile__(
  213. LOCK_PREFIX "addl %1,%0"
  214. :"=m"(sem->count)
  215. :"ir"(delta), "m"(sem->count));
  216. }
  217. /*
  218.  * implement exchange and add functionality
  219.  */
  220. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  221. {
  222. int tmp = delta;
  223. __asm__ __volatile__(
  224. LOCK_PREFIX "xaddl %0,(%2)"
  225. : "=r"(tmp), "=m"(sem->count)
  226. : "r"(sem), "m"(sem->count), "0" (tmp)
  227. : "memory");
  228. return tmp+delta;
  229. }
  230. #endif /* __KERNEL__ */
  231. #endif /* _X8664_RWSEM_H */