mutex-xchg.h 3.7 KB

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