mutex_64.h 2.9 KB

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