All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: + atomic-improve-atomic_inc_unless_negative-atomic_dec_unless_positive .patch added to -mm tree
@ 2013-03-14 16:24 Oleg Nesterov
  2013-03-15  3:46 ` Ming Lei
  0 siblings, 1 reply; 19+ messages in thread
From: Oleg Nesterov @ 2013-03-14 16:24 UTC (permalink / raw)
  To: Ming Lei, Shaohua Li, Al Viro, Andrew Morton; +Cc: linux-kernel

> From: Ming Lei <tom.leiming@gmail.com>
> Subject: atomic: improve atomic_inc_unless_negative/atomic_dec_unless_positive
>
> Generally, both atomic_inc_unless_negative() and
> atomic_dec_unless_positive() need at least two atomic_cmpxchg() to
> complete the atomic operation.  In fact, the 1st atomic_cmpxchg() is just
> used to read current value of the atomic variable at most times.

Agreed, this looks ugly...

But can't we make a simpler patch and keep the code simple/readable ?

Oleg.

--- x/include/linux/atomic.h
+++ x/include/linux/atomic.h
@@ -64,7 +64,7 @@ static inline int atomic_inc_not_zero_hi
 static inline int atomic_inc_unless_negative(atomic_t *p)
 {
 	int v, v1;
-	for (v = 0; v >= 0; v = v1) {
+	for (v = atomic_read(p); v >= 0; v = v1) {
 		v1 = atomic_cmpxchg(p, v, v + 1);
 		if (likely(v1 == v))
 			return 1;
@@ -77,7 +77,7 @@ static inline int atomic_inc_unless_nega
 static inline int atomic_dec_unless_positive(atomic_t *p)
 {
 	int v, v1;
-	for (v = 0; v <= 0; v = v1) {
+	for (v = atomic_read(p); v <= 0; v = v1) {
 		v1 = atomic_cmpxchg(p, v, v - 1);
 		if (likely(v1 == v))
 			return 1;


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2013-03-22 16:35 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-14 16:24 + atomic-improve-atomic_inc_unless_negative-atomic_dec_unless_positive .patch added to -mm tree Oleg Nesterov
2013-03-15  3:46 ` Ming Lei
2013-03-15 13:46   ` Oleg Nesterov
2013-03-15 15:13     ` Ming Lei
2013-03-15 16:51       ` Oleg Nesterov
2013-03-15 17:23         ` Frederic Weisbecker
2013-03-15 17:51           ` Oleg Nesterov
2013-03-15 18:34             ` Frederic Weisbecker
2013-03-15 20:17               ` Paul E. McKenney
2013-03-16 18:30                 ` Oleg Nesterov
2013-03-17 17:26                   ` Paul E. McKenney
2013-03-21 17:08                     ` Oleg Nesterov
2013-03-21 17:34                       ` Paul E. McKenney
2013-03-21 18:03                       ` Eric Dumazet
2013-03-21 18:30                         ` Oleg Nesterov
2013-03-21 22:56                           ` Eric Dumazet
2013-03-22 12:59                             ` Oleg Nesterov
2013-03-22 16:34                             ` Paul E. McKenney
2013-03-16 18:19               ` Oleg Nesterov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.