mutex_64.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_X86_MUTEX_64_H
  10. #define _ASM_X86_MUTEX_64_H
  11. /**
  12. * __mutex_fastpath_lock - decrement and call function if negative
  13. * @v: pointer of type atomic_t
  14. * @fail_fn: function to call if the result is negative
  15. *
  16. * Atomically decrements @v and calls <fail_fn> if the result is negative.
  17. */
  18. #define __mutex_fastpath_lock(v, fail_fn) \
  19. do { \
  20. unsigned long dummy; \
  21. \
  22. typecheck(atomic_t *, v); \
  23. typecheck_fn(void (*)(atomic_t *), fail_fn); \
  24. \
  25. asm volatile(LOCK_PREFIX " decl (%%rdi)\n" \
  26. " jns 1f \n" \
  27. " call " #fail_fn "\n" \
  28. "1:" \
  29. : "=D" (dummy) \
  30. : "D" (v) \
  31. : "rax", "rsi", "rdx", "rcx", \
  32. "r8", "r9", "r10", "r11", "memory"); \
  33. } while (0)
  34. /**
  35. * __mutex_fastpath_lock_retval - try to take the lock by moving the count
  36. * from 1 to a 0 value
  37. * @count: pointer of type atomic_t
  38. *
  39. * Change the count from 1 to a value lower than 1. This function returns 0
  40. * if the fastpath succeeds, or -1 otherwise.
  41. */
  42. static inline int __mutex_fastpath_lock_retval(atomic_t *count)
  43. {
  44. if (unlikely(atomic_dec_return(count) < 0))
  45. return -1;
  46. else
  47. return 0;
  48. }
  49. /**
  50. * __mutex_fastpath_unlock - increment and call function if nonpositive
  51. * @v: pointer of type atomic_t
  52. * @fail_fn: function to call if the result is nonpositive
  53. *
  54. * Atomically increments @v and calls <fail_fn> if the result is nonpositive.
  55. */
  56. #define __mutex_fastpath_unlock(v, fail_fn) \
  57. do { \
  58. unsigned long dummy; \
  59. \
  60. typecheck(atomic_t *, v); \
  61. typecheck_fn(void (*)(atomic_t *), fail_fn); \
  62. \
  63. asm volatile(LOCK_PREFIX " incl (%%rdi)\n" \
  64. " jg 1f\n" \
  65. " call " #fail_fn "\n" \
  66. "1:" \
  67. : "=D" (dummy) \
  68. : "D" (v) \
  69. : "rax", "rsi", "rdx", "rcx", \
  70. "r8", "r9", "r10", "r11", "memory"); \
  71. } while (0)
  72. #define __mutex_slowpath_needs_to_unlock() 1
  73. /**
  74. * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
  75. *
  76. * @count: pointer of type atomic_t
  77. * @fail_fn: fallback function
  78. *
  79. * Change the count from 1 to 0 and return 1 (success), or return 0 (failure)
  80. * if it wasn't 1 originally. [the fallback function is never used on
  81. * x86_64, because all x86_64 CPUs have a CMPXCHG instruction.]
  82. */
  83. static inline int __mutex_fastpath_trylock(atomic_t *count,
  84. int (*fail_fn)(atomic_t *))
  85. {
  86. if (likely(atomic_cmpxchg(count, 1, 0) == 1))
  87. return 1;
  88. else
  89. return 0;
  90. }
  91. #endif /* _ASM_X86_MUTEX_64_H */