Skip to content

Commit e358c1a

Browse files
Nicolas PitreLinus Torvalds
authored andcommitted
[PATCH] mutex: some cleanups
Turn some macros into inline functions and add proper type checking as well as being more readable. Also a minor comment adjustment. Signed-off-by: Nicolas Pitre <nico@cam.org> Acked-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent a58e00e commit e358c1a

2 files changed

Lines changed: 33 additions & 30 deletions

File tree

‎include/asm-generic/mutex-dec.h‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
* it wasn't 1 originally. This function MUST leave the value lower than
1818
* 1 even when the "1" assertion wasn't true.
1919
*/
20-
#define __mutex_fastpath_lock(count, fail_fn) \
21-
do { \
22-
if (unlikely(atomic_dec_return(count) < 0)) \
23-
fail_fn(count); \
24-
else \
25-
smp_mb(); \
26-
} while (0)
20+
static inline void
21+
__mutex_fastpath_lock(atomic_t *count, fastcall void (*fail_fn)(atomic_t *))
22+
{
23+
if (unlikely(atomic_dec_return(count) < 0))
24+
fail_fn(count);
25+
else
26+
smp_mb();
27+
}
2728

2829
/**
2930
* __mutex_fastpath_lock_retval - try to take the lock by moving the count
@@ -36,7 +37,7 @@ do { \
3637
* or anything the slow path function returns.
3738
*/
3839
static inline int
39-
__mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
40+
__mutex_fastpath_lock_retval(atomic_t *count, fastcall int (*fail_fn)(atomic_t *))
4041
{
4142
if (unlikely(atomic_dec_return(count) < 0))
4243
return fail_fn(count);
@@ -59,12 +60,13 @@ __mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
5960
* __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
6061
* to return 0 otherwise.
6162
*/
62-
#define __mutex_fastpath_unlock(count, fail_fn) \
63-
do { \
64-
smp_mb(); \
65-
if (unlikely(atomic_inc_return(count) <= 0)) \
66-
fail_fn(count); \
67-
} while (0)
63+
static inline void
64+
__mutex_fastpath_unlock(atomic_t *count, fastcall void (*fail_fn)(atomic_t *))
65+
{
66+
smp_mb();
67+
if (unlikely(atomic_inc_return(count) <= 0))
68+
fail_fn(count);
69+
}
6870

6971
#define __mutex_slowpath_needs_to_unlock() 1
7072

‎include/asm-generic/mutex-xchg.h‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Generic implementation of the mutex fastpath, based on xchg().
55
*
6-
* NOTE: An xchg based implementation is less optimal than an atomic
6+
* NOTE: An xchg based implementation might be less optimal than an atomic
77
* decrement/increment based implementation. If your architecture
88
* has a reasonable atomic dec/inc then you should probably use
99
* asm-generic/mutex-dec.h instead, or you could open-code an
@@ -22,14 +22,14 @@
2222
* wasn't 1 originally. This function MUST leave the value lower than 1
2323
* even when the "1" assertion wasn't true.
2424
*/
25-
#define __mutex_fastpath_lock(count, fail_fn) \
26-
do { \
27-
if (unlikely(atomic_xchg(count, 0) != 1)) \
28-
fail_fn(count); \
29-
else \
30-
smp_mb(); \
31-
} while (0)
32-
25+
static inline void
26+
__mutex_fastpath_lock(atomic_t *count, fastcall void (*fail_fn)(atomic_t *))
27+
{
28+
if (unlikely(atomic_xchg(count, 0) != 1))
29+
fail_fn(count);
30+
else
31+
smp_mb();
32+
}
3333

3434
/**
3535
* __mutex_fastpath_lock_retval - try to take the lock by moving the count
@@ -42,7 +42,7 @@ do { \
4242
* or anything the slow path function returns
4343
*/
4444
static inline int
45-
__mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
45+
__mutex_fastpath_lock_retval(atomic_t *count, fastcall int (*fail_fn)(atomic_t *))
4646
{
4747
if (unlikely(atomic_xchg(count, 0) != 1))
4848
return fail_fn(count);
@@ -64,12 +64,13 @@ __mutex_fastpath_lock_retval(atomic_t *count, int (*fail_fn)(atomic_t *))
6464
* __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs
6565
* to return 0 otherwise.
6666
*/
67-
#define __mutex_fastpath_unlock(count, fail_fn) \
68-
do { \
69-
smp_mb(); \
70-
if (unlikely(atomic_xchg(count, 1) != 0)) \
71-
fail_fn(count); \
72-
} while (0)
67+
static inline void
68+
__mutex_fastpath_unlock(atomic_t *count, fastcall void (*fail_fn)(atomic_t *))
69+
{
70+
smp_mb();
71+
if (unlikely(atomic_xchg(count, 1) != 0))
72+
fail_fn(count);
73+
}
7374

7475
#define __mutex_slowpath_needs_to_unlock() 0
7576

0 commit comments

Comments
 (0)