mutex-dec.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * asm-generic/mutex-dec.h
  3. *
  4. * Generic implementation of the mutex fastpath, based on atomic
  5. * decrement/increment.
  6. */
  7. #ifndef _ASM_GENERIC_MUTEX_DEC_H
  8. #define _ASM_GENERIC_MUTEX_DEC_H
  9. /**
  10. * __mutex_fastpath_lock - try to take the lock by moving the count
  11. * from 1 to a 0 value
  12. * @count: pointer of type atomic_t
  13. * @fail_fn: function to call if the original value was not 1
  14. *
  15. * Change the count from 1 to a value lower than 1, and call <fail_fn> if
  16. * it wasn't 1 originally. This function MUST leave the value lower than
  17. * 1 even when the "1" assertion wasn't true.
  18. */
  19. #define __mutex_fastpath_lock(count, fail_fn) \
  20. do { \
  21. if (unlikely(atomic_dec_return(count) < 0)) \
  22. fail_fn(count); \
  23. else \
  24. smp_mb(); \
  25. } while (0)
  26. /**
  27. * __mutex_fastpath_lock_retval - try to take the lock by moving the count
  28. * from 1 to a 0 value
  29. * @count: pointer of type atomic_t
  30. * @fail_fn: function to call if the original value was not 1
  31. *
  32. * Change the count from 1 to a value lower than 1, and call <fail_fn> if
  33. * it wasn't 1 originally. This function returns 0 if the fastpath succeeds,
  34. * or anything the slow path function returns.
  35. */
  36. static inline int
  37. __mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
  38. {
  39. if (unlikely(atomic_dec_return(count) < 0))
  40. return fail_fn(count);
  41. else {
  42. smp_mb();
  43. return 0;
  44. }
  45. }
  46. /**
  47. * __mutex_fastpath_unlock - try to promote the count from 0 to 1
  48. * @count: pointer of type atomic_t
  49. * @fail_fn: function to call if the original value was not 0
  50. *
  51. * Try to promote the count from 0 to 1. If it wasn't 0, call <fail_fn>.
  52. * In the failure case, this function is allowed to either set the value to
  53. * 1, or to set it to a value lower than 1.
  54. *
  55. * If the implementation sets it to a value of lower than 1, then the
  56. * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
  57. * to return 0 otherwise.
  58. */
  59. #define __mutex_fastpath_unlock(count, fail_fn) \
  60. do { \
  61. smp_mb(); \
  62. if (unlikely(atomic_inc_return(count) <= 0)) \
  63. fail_fn(count); \
  64. } while (0)
  65. #define __mutex_slowpath_needs_to_unlock() 1
  66. /**
  67. * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
  68. *
  69. * @count: pointer of type atomic_t
  70. * @fail_fn: fallback function
  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. /*
  85. * We have two variants here. The cmpxchg based one is the best one
  86. * because it never induce a false contention state. It is included
  87. * here because architectures using the inc/dec algorithms over the
  88. * xchg ones are much more likely to support cmpxchg natively.
  89. *
  90. * If not we fall back to the spinlock based variant - that is
  91. * just as efficient (and simpler) as a 'destructive' probing of
  92. * the mutex state would be.
  93. */
  94. #ifdef __HAVE_ARCH_CMPXCHG
  95. if (likely(atomic_cmpxchg(count, 1, 0) == 1)) {
  96. smp_mb();
  97. return 1;
  98. }
  99. return 0;
  100. #else
  101. return fail_fn(count);
  102. #endif
  103. }
  104. #endif