Skip to contentSkip to conversation

Let (atomic_)cmpxchg directly return true or false

PATCH New Junchang Wang 1 participant 1 message Oct 28, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Let (atomic_)cmpxchg directly return true or false

To change the definition of (atomic_)cmpxchg. This patch is for code review only.

Before the patch:
old/(old+1) (atomic_)cmpxchg(*ptr, old, new)

After the patch:
true/false (atomic_)cmpxchg(*ptr, *old, new)

---
 CodeSamples/advsync/q.c              |  2 +-
 CodeSamples/advsync/singleq.c        |  3 ++-
 CodeSamples/api-pthreads/api-gcc.h   | 12 ++++--------
 CodeSamples/count/count_lim_atomic.c |  8 ++++----
 CodeSamples/defer/rcu64_atomicgp.c   |  2 +-
 CodeSamples/defer/route_refcnt.c     |  2 +-
 6 files changed, 13 insertions(+), 16 deletions(-)
CodeSamples/advsync/q.c +1 -1
index 5fade3e..4e8abd3 100644
@@ -51,7 +51,7 @@ struct el *q_pop(struct queue *q)
 			pnext = p->next;
 			if (pnext == NULL)
 				return NULL;
-		} while (cmpxchg(&q->head, p, pnext) != p);
+		} while (!cmpxchg(&q->head, &p, pnext));
 		if (p != &q->dummy)
 			return p;
 		q_push(&q->dummy, q);
CodeSamples/advsync/singleq.c +2 -1
index 6586561..c56c8d8 100644
@@ -31,7 +31,8 @@ void init_q(struct queue *qp)
 
 int q_push(struct el *ep, struct queue *qp)
 {
-	return cmpxchg(&qp->head, NULL, ep) == NULL;
+	void *nil = NULL;
+	return cmpxchg(&qp->head, &nil, ep);
 }
 
 struct el *q_pop(struct queue *qp)
CodeSamples/api-pthreads/api-gcc.h +4 -8
index 3afe340..9cb5998 100644
@@ -166,13 +166,11 @@ struct __xchg_dummy {
 
 #define cmpxchg(ptr, o, n) \
 ({ \
-	typeof(*ptr) _____actual = (o); \
-	\
-	__atomic_compare_exchange_n(ptr, (void *)&_____actual, (n), 1, \
-			__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? (o) : (o)+1; \
+	__atomic_compare_exchange_n(ptr, (void *)(o), (n), 1, \
+			__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
 })
 
-static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
+static __inline__ int atomic_cmpxchg(atomic_t *v, int *old, int new)
 {
 	return cmpxchg(&v->counter, old, new);
 }
@@ -197,10 +195,8 @@ static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
 	for (;;) {						\
 		if (unlikely(c == (u)))				\
 			break;					\
-		old = atomic_cmpxchg((v), c, c + (a));		\
-		if (likely(old == c))				\
+		if (likely(atomic_cmpxchg((v), &c, c + (a))))	\
 			break;					\
-		c = old;					\
 	}							\
 	c != (u);						\
 })
CodeSamples/count/count_lim_atomic.c +4 -4
index 02f4e6d..72bdb65 100644
@@ -106,8 +106,8 @@ int add_count(unsigned long delta)			//\lnlbl{add:b}
 		if (delta > MAX_COUNTERMAX || c + delta > cm)//\lnlbl{add:check}
 			goto slowpath;			//\lnlbl{add:goto}
 		new = merge_counterandmax(c + delta, cm);//\lnlbl{add:merge}
-	} while (atomic_cmpxchg(&counterandmax,		//\lnlbl{add:atmcmpex}
-	                        old, new) != old);	//\lnlbl{add:loop:e}
+	} while (!atomic_cmpxchg(&counterandmax,	//\lnlbl{add:atmcmpex}
+	                        &old, new));		//\lnlbl{add:loop:e}
 	return 1;					//\lnlbl{add:return:fs}
 slowpath:						//\lnlbl{add:slow:b}
 	spin_lock(&gblcnt_mutex);			//\lnlbl{add:acquire}
@@ -139,8 +139,8 @@ int sub_count(unsigned long delta)			//\lnlbl{sub:b}
 		if (delta > c)
 			goto slowpath;
 		new = merge_counterandmax(c - delta, cm);
-	} while (atomic_cmpxchg(&counterandmax,
-	                        old, new) != old);
+	} while (!atomic_cmpxchg(&counterandmax,
+	                        &old, new));
 	return 1;					//\lnlbl{sub:fast:e}
  slowpath:						//\lnlbl{sub:slow:b}
 	spin_lock(&gblcnt_mutex);
CodeSamples/defer/rcu64_atomicgp.c +1 -1
index f7cb6ff..923df93 100644
@@ -39,7 +39,7 @@ void synchronize_rcu(void)
 	 */
 
 	oldval = rcu_gp_ctr;
-	(void)cmpxchg(&rcu_gp_ctr, oldval, oldval - 1);
+	(void)cmpxchg(&rcu_gp_ctr, &oldval, oldval - 1);
 	smp_mb();
 
 	/*
CodeSamples/defer/route_refcnt.c +1 -1
index 8a48faf..c202548 100644
@@ -68,7 +68,7 @@ retry:
 			if (old <= 0)
 				goto retry;
 			new = old + 1;
-		} while (atomic_cmpxchg(&rep->re_refcnt, old, new) != old);
+		} while (!atomic_cmpxchg(&rep->re_refcnt, &old, new));
 
 		/* Advance to next. */
 		repp = &rep->re_next;
Signature · 1 line
2.7.4