Skip to content

Commit 79d365a

Browse files
Mathieu DesnoyersLinus Torvalds
authored andcommitted
atomic.h: add atomic64 cmpxchg, xchg and add_unless to x86_64
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> Cc: Andi Kleen <ak@muc.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2549c85 commit 79d365a

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

‎include/asm-x86_64/atomic.h‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ static __inline__ long atomic64_add_return(long i, atomic64_t *v)
375375
long __i = i;
376376
__asm__ __volatile__(
377377
LOCK_PREFIX "xaddq %0, %1;"
378-
:"=r"(i)
379-
:"m"(v->counter), "0"(i));
378+
:"+r" (i), "+m" (v->counter)
379+
: : "memory");
380380
return i + __i;
381381
}
382382

@@ -388,7 +388,10 @@ static __inline__ long atomic64_sub_return(long i, atomic64_t *v)
388388
#define atomic64_inc_return(v) (atomic64_add_return(1,v))
389389
#define atomic64_dec_return(v) (atomic64_sub_return(1,v))
390390

391-
#define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
391+
#define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
392+
#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
393+
394+
#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
392395
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
393396

394397
/**
@@ -402,7 +405,7 @@ static __inline__ long atomic64_sub_return(long i, atomic64_t *v)
402405
*/
403406
#define atomic_add_unless(v, a, u) \
404407
({ \
405-
int c, old; \
408+
__typeof__((v)->counter) c, old; \
406409
c = atomic_read(v); \
407410
for (;;) { \
408411
if (unlikely(c == (u))) \
@@ -416,6 +419,31 @@ static __inline__ long atomic64_sub_return(long i, atomic64_t *v)
416419
})
417420
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
418421

422+
/**
423+
* atomic64_add_unless - add unless the number is a given value
424+
* @v: pointer of type atomic64_t
425+
* @a: the amount to add to v...
426+
* @u: ...unless v is equal to u.
427+
*
428+
* Atomically adds @a to @v, so long as it was not @u.
429+
* Returns non-zero if @v was not @u, and zero otherwise.
430+
*/
431+
#define atomic64_add_unless(v, a, u) \
432+
({ \
433+
__typeof__((v)->counter) c, old; \
434+
c = atomic64_read(v); \
435+
for (;;) { \
436+
if (unlikely(c == (u))) \
437+
break; \
438+
old = atomic64_cmpxchg((v), c, c + (a)); \
439+
if (likely(old == c)) \
440+
break; \
441+
c = old; \
442+
} \
443+
c != (u); \
444+
})
445+
#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
446+
419447
/* These are x86-specific, used by some header files */
420448
#define atomic_clear_mask(mask, addr) \
421449
__asm__ __volatile__(LOCK_PREFIX "andl %0,%1" \

0 commit comments

Comments
 (0)