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>
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 #define __mutex_fastpath_lock(v, fail_fn) \
21 unsigned long dummy; \
23 typecheck(atomic_t *, v); \
24 typecheck_fn(fastcall void (*)(atomic_t *), fail_fn); \
26 __asm__ __volatile__( \
27 LOCK_PREFIX " decl (%%rdi) \n" \
29 " call "#fail_fn" \n" \
34 : "rax", "rsi", "rdx", "rcx", \
35 "r8", "r9", "r10", "r11", "memory"); \
39 * __mutex_fastpath_lock_retval - try to take the lock by moving the count
41 * @count: pointer of type atomic_t
42 * @fail_fn: function to call if the original value was not 1
44 * Change the count from 1 to a value lower than 1, and call <fail_fn> if
45 * it wasn't 1 originally. This function returns 0 if the fastpath succeeds,
46 * or anything the slow path function returns
49 __mutex_fastpath_lock_retval(atomic_t
*count
,
50 int fastcall (*fail_fn
)(atomic_t
*))
52 if (unlikely(atomic_dec_return(count
) < 0))
53 return fail_fn(count
);
59 * __mutex_fastpath_unlock - increment and call function if nonpositive
60 * @v: pointer of type atomic_t
61 * @fail_fn: function to call if the result is nonpositive
63 * Atomically increments @v and calls <fail_fn> if the result is nonpositive.
65 #define __mutex_fastpath_unlock(v, fail_fn) \
67 unsigned long dummy; \
69 typecheck(atomic_t *, v); \
70 typecheck_fn(fastcall void (*)(atomic_t *), fail_fn); \
72 __asm__ __volatile__( \
73 LOCK_PREFIX " incl (%%rdi) \n" \
75 " call "#fail_fn" \n" \
80 : "rax", "rsi", "rdx", "rcx", \
81 "r8", "r9", "r10", "r11", "memory"); \
84 #define __mutex_slowpath_needs_to_unlock() 1
87 * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
89 * @count: pointer of type atomic_t
90 * @fail_fn: fallback function
92 * Change the count from 1 to 0 and return 1 (success), or return 0 (failure)
93 * if it wasn't 1 originally. [the fallback function is never used on
94 * x86_64, because all x86_64 CPUs have a CMPXCHG instruction.]
97 __mutex_fastpath_trylock(atomic_t
*count
, int (*fail_fn
)(atomic_t
*))
99 if (likely(atomic_cmpxchg(count
, 1, 0) == 1))