spinlock.h
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:5k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* spinlock.h: 64-bit Sparc spinlock support.
  2.  *
  3.  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  4.  */
  5. #ifndef __SPARC64_SPINLOCK_H
  6. #define __SPARC64_SPINLOCK_H
  7. #include <linux/config.h>
  8. #ifndef __ASSEMBLY__
  9. /* To get debugging spinlocks which detect and catch
  10.  * deadlock situations, set CONFIG_DEBUG_SPINLOCK
  11.  * and rebuild your kernel.
  12.  */
  13. /* All of these locking primitives are expected to work properly
  14.  * even in an RMO memory model, which currently is what the kernel
  15.  * runs in.
  16.  *
  17.  * There is another issue.  Because we play games to save cycles
  18.  * in the non-contention case, we need to be extra careful about
  19.  * branch targets into the "spinning" code.  They live in their
  20.  * own section, but the newer V9 branches have a shorter range
  21.  * than the traditional 32-bit sparc branch variants.  The rule
  22.  * is that the branches that go into and out of the spinner sections
  23.  * must be pre-V9 branches.
  24.  */
  25. #ifndef CONFIG_DEBUG_SPINLOCK
  26. typedef unsigned char spinlock_t;
  27. #define SPIN_LOCK_UNLOCKED 0
  28. #define spin_lock_init(lock) (*((unsigned char *)(lock)) = 0)
  29. #define spin_is_locked(lock) (*((volatile unsigned char *)(lock)) != 0)
  30. #define spin_unlock_wait(lock)
  31. do { membar("#LoadLoad");
  32. } while(*((volatile unsigned char *)lock))
  33. extern __inline__ void spin_lock(spinlock_t *lock)
  34. {
  35. __asm__ __volatile__(
  36. "1: ldstub [%0], %%g7n"
  37. " brnz,pn %%g7, 2fn"
  38. "  membar #StoreLoad | #StoreStoren"
  39. " .subsection 2n"
  40. "2: ldub [%0], %%g7n"
  41. " brnz,pt %%g7, 2bn"
  42. "  membar #LoadLoadn"
  43. " b,a,pt %%xcc, 1bn"
  44. " .previousn"
  45. : /* no outputs */
  46. : "r" (lock)
  47. : "g7", "memory");
  48. }
  49. extern __inline__ int spin_trylock(spinlock_t *lock)
  50. {
  51. unsigned int result;
  52. __asm__ __volatile__("ldstub [%1], %0nt"
  53.      "membar #StoreLoad | #StoreStore"
  54.      : "=r" (result)
  55.      : "r" (lock)
  56.      : "memory");
  57. return (result == 0);
  58. }
  59. extern __inline__ void spin_unlock(spinlock_t *lock)
  60. {
  61. __asm__ __volatile__("membar #StoreStore | #LoadStorent"
  62.      "stb %%g0, [%0]"
  63.      : /* No outputs */
  64.      : "r" (lock)
  65.      : "memory");
  66. }
  67. #else /* !(CONFIG_DEBUG_SPINLOCK) */
  68. typedef struct {
  69. unsigned char lock;
  70. unsigned int owner_pc, owner_cpu;
  71. } spinlock_t;
  72. #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 0, 0xff }
  73. #define spin_lock_init(__lock)
  74. do { (__lock)->lock = 0; 
  75. (__lock)->owner_pc = 0; 
  76. (__lock)->owner_cpu = 0xff; 
  77. } while(0)
  78. #define spin_is_locked(__lock) (*((volatile unsigned char *)(&((__lock)->lock))) != 0)
  79. #define spin_unlock_wait(__lock)
  80. do { 
  81. membar("#LoadLoad"); 
  82. } while(*((volatile unsigned char *)(&((__lock)->lock))))
  83. extern void _do_spin_lock (spinlock_t *lock, char *str);
  84. extern void _do_spin_unlock (spinlock_t *lock);
  85. extern int _spin_trylock (spinlock_t *lock);
  86. #define spin_trylock(lp) _spin_trylock(lp)
  87. #define spin_lock(lock) _do_spin_lock(lock, "spin_lock")
  88. #define spin_unlock(lock) _do_spin_unlock(lock)
  89. #endif /* CONFIG_DEBUG_SPINLOCK */
  90. /* Multi-reader locks, these are much saner than the 32-bit Sparc ones... */
  91. #ifndef CONFIG_DEBUG_SPINLOCK
  92. typedef unsigned int rwlock_t;
  93. #define RW_LOCK_UNLOCKED 0
  94. #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
  95. extern void __read_lock(rwlock_t *);
  96. extern void __read_unlock(rwlock_t *);
  97. extern void __write_lock(rwlock_t *);
  98. extern void __write_unlock(rwlock_t *);
  99. #define read_lock(p) __read_lock(p)
  100. #define read_unlock(p) __read_unlock(p)
  101. #define write_lock(p) __write_lock(p)
  102. #define write_unlock(p) __write_unlock(p)
  103. #else /* !(CONFIG_DEBUG_SPINLOCK) */
  104. typedef struct {
  105. unsigned long lock;
  106. unsigned int writer_pc, writer_cpu;
  107. unsigned int reader_pc[4];
  108. } rwlock_t;
  109. #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0, 0xff, { 0, 0, 0, 0 } }
  110. #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
  111. extern void _do_read_lock(rwlock_t *rw, char *str);
  112. extern void _do_read_unlock(rwlock_t *rw, char *str);
  113. extern void _do_write_lock(rwlock_t *rw, char *str);
  114. extern void _do_write_unlock(rwlock_t *rw);
  115. #define read_lock(lock)
  116. do { unsigned long flags; 
  117. __save_and_cli(flags); 
  118. _do_read_lock(lock, "read_lock"); 
  119. __restore_flags(flags); 
  120. } while(0)
  121. #define read_unlock(lock) 
  122. do { unsigned long flags; 
  123. __save_and_cli(flags); 
  124. _do_read_unlock(lock, "read_unlock"); 
  125. __restore_flags(flags); 
  126. } while(0)
  127. #define write_lock(lock) 
  128. do { unsigned long flags; 
  129. __save_and_cli(flags); 
  130. _do_write_lock(lock, "write_lock"); 
  131. __restore_flags(flags); 
  132. } while(0)
  133. #define write_unlock(lock) 
  134. do { unsigned long flags; 
  135. __save_and_cli(flags); 
  136. _do_write_unlock(lock); 
  137. __restore_flags(flags); 
  138. } while(0)
  139. #endif /* CONFIG_DEBUG_SPINLOCK */
  140. #endif /* !(__ASSEMBLY__) */
  141. #endif /* !(__SPARC64_SPINLOCK_H) */