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

嵌入式Linux

开发平台:

Unix_Linux

  1. #ifndef _ALPHA_SPINLOCK_H
  2. #define _ALPHA_SPINLOCK_H
  3. #include <linux/config.h>
  4. #include <asm/system.h>
  5. #include <linux/kernel.h>
  6. #include <asm/current.h>
  7. /*
  8.  * Simple spin lock operations.  There are two variants, one clears IRQ's
  9.  * on the local processor, one does not.
  10.  *
  11.  * We make no fairness assumptions. They have a cost.
  12.  */
  13. typedef struct {
  14. volatile unsigned int lock /*__attribute__((aligned(32))) */;
  15. #if CONFIG_DEBUG_SPINLOCK
  16. int on_cpu;
  17. int line_no;
  18. void *previous;
  19. struct task_struct * task;
  20. const char *base_file;
  21. #endif
  22. } spinlock_t;
  23. #if CONFIG_DEBUG_SPINLOCK
  24. #define SPIN_LOCK_UNLOCKED (spinlock_t) {0, -1, 0, 0, 0, 0}
  25. #define spin_lock_init(x)
  26. ((x)->lock = 0, (x)->on_cpu = -1, (x)->previous = 0, (x)->task = 0)
  27. #else
  28. #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
  29. #define spin_lock_init(x) ((x)->lock = 0)
  30. #endif
  31. #define spin_is_locked(x) ((x)->lock != 0)
  32. #define spin_unlock_wait(x) ({ do { barrier(); } while ((x)->lock); })
  33. #if CONFIG_DEBUG_SPINLOCK
  34. extern void spin_unlock(spinlock_t * lock);
  35. extern void debug_spin_lock(spinlock_t * lock, const char *, int);
  36. extern int debug_spin_trylock(spinlock_t * lock, const char *, int);
  37. #define spin_lock(LOCK) debug_spin_lock(LOCK, __BASE_FILE__, __LINE__)
  38. #define spin_trylock(LOCK) debug_spin_trylock(LOCK, __BASE_FILE__, __LINE__)
  39. #define spin_lock_own(LOCK, LOCATION)
  40. do {
  41. if (!((LOCK)->lock && (LOCK)->on_cpu == smp_processor_id()))
  42. printk("%s: called on %d from %p but lock %s on %dn",
  43.        LOCATION, smp_processor_id(),
  44.        __builtin_return_address(0),
  45.        (LOCK)->lock ? "taken" : "freed", (LOCK)->on_cpu); 
  46. } while (0)
  47. #else
  48. static inline void spin_unlock(spinlock_t * lock)
  49. {
  50. mb();
  51. lock->lock = 0;
  52. }
  53. static inline void spin_lock(spinlock_t * lock)
  54. {
  55. long tmp;
  56. /* Use sub-sections to put the actual loop at the end
  57.    of this object file's text section so as to perfect
  58.    branch prediction.  */
  59. __asm__ __volatile__(
  60. "1: ldl_l %0,%1n"
  61. " blbs %0,2fn"
  62. " or %0,1,%0n"
  63. " stl_c %0,%1n"
  64. " beq %0,2fn"
  65. " mbn"
  66. ".subsection 2n"
  67. "2: ldl %0,%1n"
  68. " blbs %0,2bn"
  69. " br 1bn"
  70. ".previous"
  71. : "=&r" (tmp), "=m" (lock->lock)
  72. : "m"(lock->lock) : "memory");
  73. }
  74. #define spin_trylock(lock) (!test_and_set_bit(0,(lock)))
  75. #define spin_lock_own(LOCK, LOCATION) ((void)0)
  76. #endif /* CONFIG_DEBUG_SPINLOCK */
  77. /***********************************************************/
  78. typedef struct {
  79. volatile int write_lock:1, read_counter:31;
  80. } /*__attribute__((aligned(32)))*/ rwlock_t;
  81. #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
  82. #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
  83. #if CONFIG_DEBUG_RWLOCK
  84. extern void write_lock(rwlock_t * lock);
  85. extern void read_lock(rwlock_t * lock);
  86. #else
  87. static inline void write_lock(rwlock_t * lock)
  88. {
  89. long regx;
  90. __asm__ __volatile__(
  91. "1: ldl_l %1,%0n"
  92. " bne %1,6fn"
  93. " or $31,1,%1n"
  94. " stl_c %1,%0n"
  95. " beq %1,6fn"
  96. " mbn"
  97. ".subsection 2n"
  98. "6: ldl %1,%0n"
  99. " bne %1,6bn"
  100. " br 1bn"
  101. ".previous"
  102. : "=m" (*(volatile int *)lock), "=&r" (regx)
  103. : "0" (*(volatile int *)lock) : "memory");
  104. }
  105. static inline void read_lock(rwlock_t * lock)
  106. {
  107. long regx;
  108. __asm__ __volatile__(
  109. "1: ldl_l %1,%0n"
  110. " blbs %1,6fn"
  111. " subl %1,2,%1n"
  112. " stl_c %1,%0n"
  113. " beq %1,6fn"
  114. "4: mbn"
  115. ".subsection 2n"
  116. "6: ldl %1,%0n"
  117. " blbs %1,6bn"
  118. " br 1bn"
  119. ".previous"
  120. : "=m" (*(volatile int *)lock), "=&r" (regx)
  121. : "m" (*(volatile int *)lock) : "memory");
  122. }
  123. #endif /* CONFIG_DEBUG_RWLOCK */
  124. static inline void write_unlock(rwlock_t * lock)
  125. {
  126. mb();
  127. *(volatile int *)lock = 0;
  128. }
  129. static inline void read_unlock(rwlock_t * lock)
  130. {
  131. long regx;
  132. __asm__ __volatile__(
  133. " mbn"
  134. "1: ldl_l %1,%0n"
  135. " addl %1,2,%1n"
  136. " stl_c %1,%0n"
  137. " beq %1,6fn"
  138. ".subsection 2n"
  139. "6: br 1bn"
  140. ".previous"
  141. : "=m" (*(volatile int *)lock), "=&r" (regx)
  142. : "m" (*(volatile int *)lock) : "memory");
  143. }
  144. #endif /* _ALPHA_SPINLOCK_H */