2 * Assembly implementation of the mutex fastpath, based on atomic
5 * started by Ingo Molnar:
7 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 #ifndef _ASM_X86_MUTEX_64_H
10 #define _ASM_X86_MUTEX_64_H
13 * __mutex_fastpath_lock - decrement and call function if negative
14 * @v: pointer of type atomic_t
15 * @fail_fn: function to call if the result is negative
17 * Atomically decrements @v and calls <fail_fn> if the result is negative.
19 #ifdef CC_HAVE_ASM_GOTO
20 static inline void __mutex_fastpath_lock(atomic_t
*v
,
21 void (*fail_fn
)(atomic_t
*))
23 asm_volatile_goto(LOCK_PREFIX
" decl %0\n"
33 #define __mutex_fastpath_lock(v, fail_fn) \
35 unsigned long dummy; \
37 typecheck(atomic_t *, v); \
38 typecheck_fn(void (*)(atomic_t *), fail_fn); \
40 asm volatile(LOCK_PREFIX " decl (%%rdi)\n" \
42 " call " #fail_fn "\n" \
46 : "rax", "rsi", "rdx", "rcx", \
47 "r8", "r9", "r10", "r11", "memory"); \
52 * __mutex_fastpath_lock_retval - try to take the lock by moving the count
54 * @count: pointer of type atomic_t
56 * Change the count from 1 to a value lower than 1. This function returns 0
57 * if the fastpath succeeds, or -1 otherwise.
59 static inline int __mutex_fastpath_lock_retval(atomic_t
*count
)
61 if (unlikely(atomic_dec_return(count
) < 0))
68 * __mutex_fastpath_unlock - increment and call function if nonpositive
69 * @v: pointer of type atomic_t
70 * @fail_fn: function to call if the result is nonpositive
72 * Atomically increments @v and calls <fail_fn> if the result is nonpositive.
74 #ifdef CC_HAVE_ASM_GOTO
75 static inline void __mutex_fastpath_unlock(atomic_t
*v
,
76 void (*fail_fn
)(atomic_t
*))
78 asm_volatile_goto(LOCK_PREFIX
" incl %0\n"
88 #define __mutex_fastpath_unlock(v, fail_fn) \
90 unsigned long dummy; \
92 typecheck(atomic_t *, v); \
93 typecheck_fn(void (*)(atomic_t *), fail_fn); \
95 asm volatile(LOCK_PREFIX " incl (%%rdi)\n" \
97 " call " #fail_fn "\n" \
101 : "rax", "rsi", "rdx", "rcx", \
102 "r8", "r9", "r10", "r11", "memory"); \
106 #define __mutex_slowpath_needs_to_unlock() 1
109 * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
111 * @count: pointer of type atomic_t
112 * @fail_fn: fallback function
114 * Change the count from 1 to 0 and return 1 (success), or return 0 (failure)
115 * if it wasn't 1 originally. [the fallback function is never used on
116 * x86_64, because all x86_64 CPUs have a CMPXCHG instruction.]
118 static inline int __mutex_fastpath_trylock(atomic_t
*count
,
119 int (*fail_fn
)(atomic_t
*))
121 if (likely(atomic_cmpxchg(count
, 1, 0) == 1))
127 #endif /* _ASM_X86_MUTEX_64_H */