mutex_64.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * @fail_fn: function to call if the original value was not 1
  39. *
  40. * Change the count from 1 to a value lower than 1, and call <fail_fn> if
  41. * it wasn't 1 originally. This function returns 0 if the fastpath succeeds,
  42. * or anything the slow path function returns
  43. */
  44. static inline int __mutex_fastpath_lock_retval(atomic_t *count,
  45. int (*fail_fn)(atomic_t *))
  46. {
  47. if (unlikely(atomic_dec_return(count) < 0))
  48. return fail_fn(count);
  49. else
  50. return 0;
  51. }
  52. /**
  53. * __mutex_fastpath_unlock - increment and call function if nonpositive
  54. * @v: pointer of type atomic_t
  55. * @fail_fn: function to call if the result is nonpositive
  56. *
  57. * Atomically increments @v and calls <fail_fn> if the result is nonpositive.
  58. */
  59. #define __mutex_fastpath_unlock(v, fail_fn) \
  60. do { \
  61. unsigned long dummy; \
  62. \
  63. typecheck(atomic_t *, v); \
  64. typecheck_fn(void (*)(atomic_t *), fail_fn); \
  65. \
  66. asm volatile(LOCK_PREFIX " incl (%%rdi)\n" \
  67. " jg 1f\n" \
  68. " call " #fail_fn "\n" \
  69. "1:" \
  70. : "=D" (dummy) \
  71. : "D" (v) \
  72. : "rax", "rsi", "rdx", "rcx", \
  73. "r8", "r9", "r10", "r11", "memory"); \
  74. } while (0)
  75. #define __mutex_slowpath_needs_to_unlock() 1
  76. /**
  77. * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
  78. *
  79. * @count: pointer of type atomic_t
  80. * @fail_fn: fallback function
  81. *
  82. * Change the count from 1 to 0 and return 1 (success), or return 0 (failure)
  83. * if it wasn't 1 originally. [the fallback function is never used on
  84. * x86_64, because all x86_64 CPUs have a CMPXCHG instruction.]
  85. */
  86. static inline int __mutex_fastpath_trylock(atomic_t *count,
  87. int (*fail_fn)(atomic_t *))
  88. {
  89. if (likely(atomic_cmpxchg(count, 1, 0) == 1))
  90. return 1;
  91. else
  92. return 0;
  93. }
  94. #endif /* _ASM_X86_MUTEX_64_H */