bit_spinlock.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 (test_and_set_bit(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 (test_and_set_bit(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. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  51. BUG_ON(!test_bit(bitnum, addr));
  52. smp_mb__before_clear_bit();
  53. clear_bit(bitnum, addr);
  54. #endif
  55. preempt_enable();
  56. __release(bitlock);
  57. }
  58. /*
  59. * Return true if the lock is held.
  60. */
  61. static inline int bit_spin_is_locked(int bitnum, unsigned long *addr)
  62. {
  63. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  64. return test_bit(bitnum, addr);
  65. #elif defined CONFIG_PREEMPT
  66. return preempt_count();
  67. #else
  68. return 1;
  69. #endif
  70. }
  71. #endif /* __LINUX_BIT_SPINLOCK_H */