mutex-dec.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * include/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. static inline void
  20. __mutex_fastpath_lock(atomic_t *count, fastcall void (*fail_fn)(atomic_t *))
  21. {
  22. if (unlikely(atomic_dec_return(count) < 0))
  23. fail_fn(count);
  24. else
  25. smp_mb();
  26. }
  27. /**
  28. * __mutex_fastpath_lock_retval - try to take the lock by moving the count
  29. * from 1 to a 0 value
  30. * @count: pointer of type atomic_t
  31. * @fail_fn: function to call if the original value was not 1
  32. *
  33. * Change the count from 1 to a value lower than 1, and call <fail_fn> if
  34. * it wasn't 1 originally. This function returns 0 if the fastpath succeeds,
  35. * or anything the slow path function returns.
  36. */
  37. static inline int
  38. __mutex_fastpath_lock_retval(atomic_t *count, fastcall int (*fail_fn)(atomic_t *))
  39. {
  40. if (unlikely(atomic_dec_return(count) < 0))
  41. return fail_fn(count);
  42. else {
  43. smp_mb();
  44. return 0;
  45. }
  46. }
  47. /**
  48. * __mutex_fastpath_unlock - try to promote the count 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 count from 0 to 1. If it wasn't 0, call <fail_fn>.
  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 1.
  55. *
  56. * If the implementation sets it to a value of lower than 1, then the
  57. * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
  58. * to return 0 otherwise.
  59. */
  60. static inline void
  61. __mutex_fastpath_unlock(atomic_t *count, fastcall void (*fail_fn)(atomic_t *))
  62. {
  63. smp_mb();
  64. if (unlikely(atomic_inc_return(count) <= 0))
  65. fail_fn(count);
  66. }
  67. #define __mutex_slowpath_needs_to_unlock() 1
  68. /**
  69. * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
  70. *
  71. * @count: pointer of type atomic_t
  72. * @fail_fn: fallback function
  73. *
  74. * Change the count from 1 to a value lower than 1, and return 0 (failure)
  75. * if it wasn't 1 originally, or return 1 (success) otherwise. This function
  76. * MUST leave the value lower than 1 even when the "1" assertion wasn't true.
  77. * Additionally, if the value was < 0 originally, this function must not leave
  78. * it to 0 on failure.
  79. *
  80. * If the architecture has no effective trylock variant, it should call the
  81. * <fail_fn> spinlock-based trylock variant unconditionally.
  82. */
  83. static inline int
  84. __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *))
  85. {
  86. /*
  87. * We have two variants here. The cmpxchg based one is the best one
  88. * because it never induce a false contention state. It is included
  89. * here because architectures using the inc/dec algorithms over the
  90. * xchg ones are much more likely to support cmpxchg natively.
  91. *
  92. * If not we fall back to the spinlock based variant - that is
  93. * just as efficient (and simpler) as a 'destructive' probing of
  94. * the mutex state would be.
  95. */
  96. #ifdef __HAVE_ARCH_CMPXCHG
  97. if (likely(atomic_cmpxchg(count, 1, 0) == 1)) {
  98. smp_mb();
  99. return 1;
  100. }
  101. return 0;
  102. #else
  103. return fail_fn(count);
  104. #endif
  105. }
  106. #endif