mutex-xchg.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * asm-generic/mutex-xchg.h
  3. *
  4. * Generic implementation of the mutex fastpath, based on xchg().
  5. *
  6. * NOTE: An xchg based implementation is less optimal than an atomic
  7. * decrement/increment based implementation. If your architecture
  8. * has a reasonable atomic dec/inc then you should probably use
  9. * asm-generic/mutex-dec.h instead, or you could open-code an
  10. * optimized version in asm/mutex.h.
  11. */
  12. #ifndef _ASM_GENERIC_MUTEX_XCHG_H
  13. #define _ASM_GENERIC_MUTEX_XCHG_H
  14. /**
  15. * __mutex_fastpath_lock - try to take the lock by moving the count
  16. * from 1 to a 0 value
  17. * @count: pointer of type atomic_t
  18. * @fail_fn: function to call if the original value was not 1
  19. *
  20. * Change the count from 1 to a value lower than 1, and call <fail_fn> if it
  21. * wasn't 1 originally. This function MUST leave the value lower than 1
  22. * even when the "1" assertion wasn't true.
  23. */
  24. #define __mutex_fastpath_lock(count, fail_fn) \
  25. do { \
  26. if (unlikely(atomic_xchg(count, 0) != 1)) \
  27. fail_fn(count); \
  28. else \
  29. smp_mb(); \
  30. } while (0)
  31. /**
  32. * __mutex_fastpath_lock_retval - try to take the lock by moving the count
  33. * from 1 to a 0 value
  34. * @count: pointer of type atomic_t
  35. * @fail_fn: function to call if the original value was not 1
  36. *
  37. * Change the count from 1 to a value lower than 1, and call <fail_fn> if it
  38. * wasn't 1 originally. This function returns 0 if the fastpath succeeds,
  39. * or anything the slow path function returns
  40. */
  41. static inline int
  42. __mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
  43. {
  44. if (unlikely(atomic_xchg(count, 0) != 1))
  45. return fail_fn(count);
  46. else {
  47. smp_mb();
  48. return 0;
  49. }
  50. }
  51. /**
  52. * __mutex_fastpath_unlock - try to promote the mutex from 0 to 1
  53. * @count: pointer of type atomic_t
  54. * @fail_fn: function to call if the original value was not 0
  55. *
  56. * try to promote the mutex from 0 to 1. if it wasn't 0, call <function>
  57. * In the failure case, this function is allowed to either set the value to
  58. * 1, or to set it to a value lower than one.
  59. * If the implementation sets it to a value of lower than one, the
  60. * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
  61. * to return 0 otherwise.
  62. */
  63. #define __mutex_fastpath_unlock(count, fail_fn) \
  64. do { \
  65. smp_mb(); \
  66. if (unlikely(atomic_xchg(count, 1) != 0)) \
  67. fail_fn(count); \
  68. } while (0)
  69. #define __mutex_slowpath_needs_to_unlock() 0
  70. /**
  71. * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
  72. *
  73. * @count: pointer of type atomic_t
  74. * @fail_fn: spinlock based trylock implementation
  75. *
  76. * Change the count from 1 to a value lower than 1, and return 0 (failure)
  77. * if it wasn't 1 originally, or return 1 (success) otherwise. This function
  78. * MUST leave the value lower than 1 even when the "1" assertion wasn't true.
  79. * Additionally, if the value was < 0 originally, this function must not leave
  80. * it to 0 on failure.
  81. *
  82. * If the architecture has no effective trylock variant, it should call the
  83. * <fail_fn> spinlock-based trylock variant unconditionally.
  84. */
  85. static inline int
  86. __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *))
  87. {
  88. int prev = atomic_xchg(count, 0);
  89. if (unlikely(prev < 0)) {
  90. /*
  91. * The lock was marked contended so we must restore that
  92. * state. If while doing so we get back a prev value of 1
  93. * then we just own it.
  94. *
  95. * [ In the rare case of the mutex going to 1, to 0, to -1
  96. * and then back to 0 in this few-instructions window,
  97. * this has the potential to trigger the slowpath for the
  98. * owner's unlock path needlessly, but that's not a problem
  99. * in practice. ]
  100. */
  101. prev = atomic_xchg(count, prev);
  102. if (prev < 0)
  103. prev = 0;
  104. }
  105. smp_mb();
  106. return prev;
  107. }
  108. #endif