blob: bbeefb96ddfd6062657334b15915598ed4b26595 [file] [log] [blame]
Arjan van de Ven2af7f592006-01-09 15:59:18 -08001/*
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
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080012#include "asm/alternative.h"
13
Arjan van de Ven2af7f592006-01-09 15:59:18 -080014/**
15 * __mutex_fastpath_lock - try to take the lock by moving the count
16 * from 1 to a 0 value
17 * @count: pointer of type atomic_t
18 * @fn: function to call if the original value was not 1
19 *
20 * Change the count from 1 to a value lower than 1, and call <fn> if it
21 * wasn't 1 originally. This function MUST leave the value lower than 1
22 * even when the "1" assertion wasn't true.
23 */
24#define __mutex_fastpath_lock(count, fail_fn) \
25do { \
26 unsigned int dummy; \
27 \
28 typecheck(atomic_t *, count); \
Harvey Harrison341d8852008-01-30 13:31:17 +010029 typecheck_fn(void (*)(atomic_t *), fail_fn); \
Arjan van de Ven2af7f592006-01-09 15:59:18 -080030 \
31 __asm__ __volatile__( \
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080032 LOCK_PREFIX " decl (%%eax) \n" \
Andi Kleen01215ad2006-09-26 10:52:31 +020033 " jns 1f \n" \
34 " call "#fail_fn" \n" \
Ingo Molnar73165b82006-01-10 22:07:44 +010035 "1: \n" \
36 \
Arjan van de Ven2af7f592006-01-09 15:59:18 -080037 :"=a" (dummy) \
38 : "a" (count) \
39 : "memory", "ecx", "edx"); \
40} while (0)
41
42
43/**
44 * __mutex_fastpath_lock_retval - try to take the lock by moving the count
45 * from 1 to a 0 value
46 * @count: pointer of type atomic_t
47 * @fail_fn: function to call if the original value was not 1
48 *
49 * Change the count from 1 to a value lower than 1, and call <fail_fn> if it
50 * wasn't 1 originally. This function returns 0 if the fastpath succeeds,
51 * or anything the slow path function returns
52 */
53static inline int
Harvey Harrison341d8852008-01-30 13:31:17 +010054__mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
Arjan van de Ven2af7f592006-01-09 15:59:18 -080055{
56 if (unlikely(atomic_dec_return(count) < 0))
57 return fail_fn(count);
58 else
59 return 0;
60}
61
62/**
63 * __mutex_fastpath_unlock - try to promote the mutex from 0 to 1
64 * @count: pointer of type atomic_t
65 * @fail_fn: function to call if the original value was not 0
66 *
67 * try to promote the mutex from 0 to 1. if it wasn't 0, call <fail_fn>.
68 * In the failure case, this function is allowed to either set the value
69 * to 1, or to set it to a value lower than 1.
70 *
71 * If the implementation sets it to a value of lower than 1, the
72 * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
73 * to return 0 otherwise.
74 */
75#define __mutex_fastpath_unlock(count, fail_fn) \
76do { \
77 unsigned int dummy; \
78 \
79 typecheck(atomic_t *, count); \
Harvey Harrison341d8852008-01-30 13:31:17 +010080 typecheck_fn(void (*)(atomic_t *), fail_fn); \
Arjan van de Ven2af7f592006-01-09 15:59:18 -080081 \
82 __asm__ __volatile__( \
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080083 LOCK_PREFIX " incl (%%eax) \n" \
Andi Kleen01215ad2006-09-26 10:52:31 +020084 " jg 1f \n" \
85 " call "#fail_fn" \n" \
Ingo Molnar73165b82006-01-10 22:07:44 +010086 "1: \n" \
87 \
Arjan van de Ven2af7f592006-01-09 15:59:18 -080088 :"=a" (dummy) \
89 : "a" (count) \
90 : "memory", "ecx", "edx"); \
91} while (0)
92
93#define __mutex_slowpath_needs_to_unlock() 1
94
95/**
96 * __mutex_fastpath_trylock - try to acquire the mutex, without waiting
97 *
98 * @count: pointer of type atomic_t
99 * @fail_fn: fallback function
100 *
101 * Change the count from 1 to a value lower than 1, and return 0 (failure)
102 * if it wasn't 1 originally, or return 1 (success) otherwise. This function
103 * MUST leave the value lower than 1 even when the "1" assertion wasn't true.
104 * Additionally, if the value was < 0 originally, this function must not leave
105 * it to 0 on failure.
106 */
107static inline int
108__mutex_fastpath_trylock(atomic_t *count, int (*fail_fn)(atomic_t *))
109{
110 /*
111 * We have two variants here. The cmpxchg based one is the best one
112 * because it never induce a false contention state. It is included
113 * here because architectures using the inc/dec algorithms over the
114 * xchg ones are much more likely to support cmpxchg natively.
115 *
116 * If not we fall back to the spinlock based variant - that is
117 * just as efficient (and simpler) as a 'destructive' probing of
118 * the mutex state would be.
119 */
120#ifdef __HAVE_ARCH_CMPXCHG
Linus Torvalds4cec8732006-01-11 15:50:47 -0800121 if (likely(atomic_cmpxchg(count, 1, 0) == 1))
Arjan van de Ven2af7f592006-01-09 15:59:18 -0800122 return 1;
123 return 0;
124#else
125 return fail_fn(count);
126#endif
127}
128
129#endif