bit_spinlock.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef __LINUX_BIT_SPINLOCK_H
  2. #define __LINUX_BIT_SPINLOCK_H
  3. /*
  4. * bit-based spin_lock()
  5. *
  6. * Don't use this unless you really need to: spin_lock() and spin_unlock()
  7. * are significantly faster.
  8. */
  9. static inline void bit_spin_lock(int bitnum, unsigned long *addr)
  10. {
  11. /*
  12. * Assuming the lock is uncontended, this never enters
  13. * the body of the outer loop. If it is contended, then
  14. * within the inner loop a non-atomic test is used to
  15. * busywait with less bus contention for a good time to
  16. * attempt to acquire the lock bit.
  17. */
  18. preempt_disable();
  19. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  20. while (unlikely(test_and_set_bit_lock(bitnum, addr))) {
  21. while (test_bit(bitnum, addr)) {
  22. preempt_enable();
  23. cpu_relax();
  24. preempt_disable();
  25. }
  26. }
  27. #endif
  28. __acquire(bitlock);
  29. }
  30. /*
  31. * Return true if it was acquired
  32. */
  33. static inline int bit_spin_trylock(int bitnum, unsigned long *addr)
  34. {
  35. preempt_disable();
  36. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  37. if (unlikely(test_and_set_bit_lock(bitnum, addr))) {
  38. preempt_enable();
  39. return 0;
  40. }
  41. #endif
  42. __acquire(bitlock);
  43. return 1;
  44. }
  45. /*
  46. * bit-based spin_unlock()
  47. */
  48. static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
  49. {
  50. #ifdef CONFIG_DEBUG_SPINLOCK
  51. BUG_ON(!test_bit(bitnum, addr));
  52. #endif
  53. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  54. clear_bit_unlock(bitnum, addr);
  55. #endif
  56. preempt_enable();
  57. __release(bitlock);
  58. }
  59. /*
  60. * bit-based spin_unlock()
  61. * non-atomic version, which can be used eg. if the bit lock itself is
  62. * protecting the rest of the flags in the word.
  63. */
  64. static inline void __bit_spin_unlock(int bitnum, unsigned long *addr)
  65. {
  66. #ifdef CONFIG_DEBUG_SPINLOCK
  67. BUG_ON(!test_bit(bitnum, addr));
  68. #endif
  69. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  70. __clear_bit_unlock(bitnum, addr);
  71. #endif
  72. preempt_enable();
  73. __release(bitlock);
  74. }
  75. /*
  76. * Return true if the lock is held.
  77. */
  78. static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
  79. {
  80. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  81. return test_bit(bitnum, addr);
  82. #elif defined CONFIG_PREEMPT
  83. return preempt_count();
  84. #else
  85. return 1;
  86. #endif
  87. }
  88. #endif /* __LINUX_BIT_SPINLOCK_H */