mutex.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Assembly implementation of the mutex fastpath, based on atomic
  3. * decrement/increment.
  4. *
  5. * started by Ingo Molnar:
  6. *
  7. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  8. */
  9. #ifndef _ASM_MUTEX_H
  10. #define _ASM_MUTEX_H
  11. #include "asm/alternative.h"
  12. /**
  13. * __mutex_fastpath_lock - try to take the lock by moving the count
  14. * from 1 to a 0 value
  15. * @count: pointer of type atomic_t
  16. * @fn: function to call if the original value was not 1
  17. *
  18. * Change the count from 1 to a value lower than 1, and call <fn> if it
  19. * wasn't 1 originally. This function MUST leave the value lower than 1
  20. * even when the "1" assertion wasn't true.
  21. */
  22. #define __mutex_fastpath_lock(count, fail_fn) \
  23. do { \
  24. unsigned int dummy; \
  25. \
  26. typecheck(atomic_t *, count); \
  27. typecheck_fn(fastcall void (*)(atomic_t *), fail_fn); \
  28. \
  29. __asm__ __volatile__( \
  30. LOCK_PREFIX " decl (%%eax) \n" \
  31. " js 2f \n" \
  32. "1: \n" \
  33. \
  34. LOCK_SECTION_START("") \
  35. "2: call "#fail_fn" \n" \
  36. " jmp 1b \n" \
  37. LOCK_SECTION_END \
  38. \
  39. :"=a" (dummy) \
  40. : "a" (count) \
  41. : "memory", "ecx", "edx"); \
  42. } while (0)
  43. /**
  44. * __mutex_fastpath_lock_retval - try to take the lock by moving the count
  45. * from 1 to a 0 value
  46. * @count: pointer of type atomic_t
  47. * @fail_fn: function to call if the original value was not 1
  48. *
  49. * Change the count from 1 to a value lower than 1, and call <fail_fn> if it
  50. * wasn't 1 originally. This function returns 0 if the fastpath succeeds,
  51. * or anything the slow path function returns
  52. */
  53. static inline int
  54. __mutex_fastpath_lock_retval(atomic_t *count,
  55. int fastcall (*fail_fn)(atomic_t *))
  56. {
  57. if (unlikely(atomic_dec_return(count) < 0))
  58. return fail_fn(count);
  59. else
  60. return 0;
  61. }
  62. /**
  63. * __mutex_fastpath_unlock - try to promote the mutex from 0 to 1
  64. * @count: pointer of type atomic_t
  65. * @fail_fn: function to call if the original value was not 0
  66. *
  67. * try to promote the mutex from 0 to 1. if it wasn't 0, call <fail_fn>.
  68. * In the failure case, this function is allowed to either set the value
  69. * to 1, or to set it to a value lower than 1.
  70. *
  71. * If the implementation sets it to a value of lower than 1, the
  72. * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
  73. * to return 0 otherwise.
  74. */
  75. #define __mutex_fastpath_unlock(count, fail_fn) \
  76. do { \
  77. unsigned int dummy; \
  78. \
  79. typecheck(atomic_t *, count); \
  80. typecheck_fn(fastcall void (*)(atomic_t *), fail_fn); \
  81. \
  82. __asm__ __volatile__( \
  83. LOCK_PREFIX " incl (%%eax) \n" \
  84. " jle 2f \n" \
  85. "1: \n" \
  86. \
  87. LOCK_SECTION_START("") \
  88. "2: call "#fail_fn" \n" \
  89. " jmp 1b \n" \
  90. LOCK_SECTION_END \
  91. \
  92. :"=a" (dummy) \
  93. : "a" (count) \
  94. : "memory", "ecx", "edx"); \
  95. } while (0)
  96. #define __mutex_slowpath_needs_to_unlock() 1
  97. /**
  98. * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
  99. *
  100. * @count: pointer of type atomic_t
  101. * @fail_fn: fallback function
  102. *
  103. * Change the count from 1 to a value lower than 1, and return 0 (failure)
  104. * if it wasn't 1 originally, or return 1 (success) otherwise. This function
  105. * MUST leave the value lower than 1 even when the "1" assertion wasn't true.
  106. * Additionally, if the value was < 0 originally, this function must not leave
  107. * it to 0 on failure.
  108. */
  109. static inline int
  110. __mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *))
  111. {
  112. /*
  113. * We have two variants here. The cmpxchg based one is the best one
  114. * because it never induce a false contention state. It is included
  115. * here because architectures using the inc/dec algorithms over the
  116. * xchg ones are much more likely to support cmpxchg natively.
  117. *
  118. * If not we fall back to the spinlock based variant - that is
  119. * just as efficient (and simpler) as a 'destructive' probing of
  120. * the mutex state would be.
  121. */
  122. #ifdef __HAVE_ARCH_CMPXCHG
  123. if (likely(atomic_cmpxchg(count, 1, 0) == 1))
  124. return 1;
  125. return 0;
  126. #else
  127. return fail_fn(count);
  128. #endif
  129. }
  130. #endif