Skip to contentSkip to conversation

count: Employ new code-snippet scheme (cont.)

PATCH In review Akira Yokosawa 2 participants 8 messages Oct 8, 2018 – Oct 9, 2018

lore
Share & tools
lore
#0 lore raw

cover count: Employ new code-snippet scheme (cont.)

Hi Paul,

This is the continuation of changes to snippets in the "count" chapter.

To help your review of the changes regarding READ_ONCE()/WRITE_ONCE(), I split the patches to the ones just applying the scheme and the ones modifying READ_ONCE()/WRITE_ONCE().

Note on patch #3: It fixes the segfault observed in count_tstat. However, I'm not sure if you want to take it or not. The fail can be thought of as a Quick Quiz within the Answer to the Quick Quiz. So keeping it as is would also be reasonable.

Thanks, Akira
--
Akira Yokosawa (5):
count: Employ new scheme for snippet of count_end and count_tstat
count: Fix uses of READ/WRITE_ONCE()s in count_end and count_tstat
count: Tweak counttorture.h to avoid segfault
count: Employ new scheme for snippet of count_lim
count: Fix uses of READ/WRITE_ONCE() in count_lim

 CodeSamples/count/count_end.c    |  53 ++---
 CodeSamples/count/count_lim.c    | 147 +++++++------
 CodeSamples/count/count_tstat.c  |  27 +--
 CodeSamples/count/counttorture.h |  21 +-
 count/count.tex                  | 437 +++++++++++++--------------------------
 5 files changed, 287 insertions(+), 398 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/5 count: Employ new scheme for snippet of count_end and count_tstat

From eca7914e5161de426bb04a623e9586bae89d8a73 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sat, 6 Oct 2018 18:49:11 +0900
Subject: [PATCH 1/5] count: Employ new scheme for snippet of count_end and count_tstat

---
 CodeSamples/count/count_end.c   |  50 ++++++++-------
 CodeSamples/count/count_tstat.c |  15 +++--
 count/count.tex                 | 139 ++++++++++------------------------------
 3 files changed, 68 insertions(+), 136 deletions(-)
CodeSamples/count/count_end.c +26 -24
index 00335f2..94b9273 100644
@@ -21,53 +21,55 @@
 
 #include "../api.h"
 
-unsigned long __thread counter = 0;
+//\begin{snippet}[labelbase=ln:count:count_end:whole,commandchars=\\\@\$]
+unsigned long __thread counter = 0;		//\lnlbl{var:b}
 unsigned long *counterp[NR_THREADS] = { NULL };
 unsigned long finalcount = 0;
-DEFINE_SPINLOCK(final_mutex);
+DEFINE_SPINLOCK(final_mutex);			//\lnlbl{var:e}
 
-__inline__ void inc_count(void)
+static __inline__ void inc_count(void)		//\lnlbl{inc:b}
 {
 	WRITE_ONCE(counter,
 		   READ_ONCE(counter) + 1);
-}
+}						//\lnlbl{inc:e}
 
-unsigned long read_count(void)
+static __inline__ unsigned long read_count(void)
 {
 	int t;
 	unsigned long sum;
 
-	spin_lock(&final_mutex);
-	sum = finalcount;
-	for_each_thread(t)
-		if (counterp[t] != NULL)
-			sum += *counterp[t];
-	spin_unlock(&final_mutex);
-	return sum;
-}
-
-__inline__ void count_init(void)
-{
+	spin_lock(&final_mutex);			//\lnlbl{read:acquire}
+	sum = finalcount;				//\lnlbl{read:sum:init}
+	for_each_thread(t)				//\lnlbl{read:loop:b}
+		if (counterp[t] != NULL)		//\lnlbl{read:check}
+			sum += *counterp[t];		//\lnlbl{read:loop:e}
+	spin_unlock(&final_mutex);			//\lnlbl{read:release}
+	return sum;					//\lnlbl{read:return}
 }
 
-void count_register_thread(unsigned long *p)
+__inline__ void count_init(void)		//\fcvexclude
+{						//\fcvexclude
+}						//\fcvexclude
+						//\fcvexclude
+void count_register_thread(unsigned long *p)	//\lnlbl{reg:b}
 {
 	int idx = smp_thread_id();
 
 	spin_lock(&final_mutex);
 	counterp[idx] = &counter;
 	spin_unlock(&final_mutex);
-}
+}						//\lnlbl{reg:e}
 
-void count_unregister_thread(int nthreadsexpected)
+void count_unregister_thread(int nthreadsexpected)	//\lnlbl{unreg:b}
 {
 	int idx = smp_thread_id();
 
-	spin_lock(&final_mutex);
-	finalcount += counter;
-	counterp[idx] = NULL;
-	spin_unlock(&final_mutex);
-}
+	spin_lock(&final_mutex);			//\lnlbl{unreg:acquire}
+	finalcount += counter;				//\lnlbl{unreg:add}
+	counterp[idx] = NULL;				//\lnlbl{unreg:NULL}
+	spin_unlock(&final_mutex);			//\lnlbl{unreg:release}
+}							//\lnlbl{unreg:e}
+//\end{snippet}
 
 __inline__ void count_cleanup(void)
 {
CodeSamples/count/count_tstat.c +9 -6
index 59e4025..4318bc3 100644
@@ -22,18 +22,20 @@
 
 #include "../api.h"
 
+//\begin{snippet}[labelbase=ln:count:count_tstat:whole,commandchars=\\\@\$]
 unsigned long __thread counter = 0;
 unsigned long *counterp[NR_THREADS] = { NULL };
 int finalthreadcount = 0;
 DEFINE_SPINLOCK(final_mutex);
 
-void inc_count(void)
+static __inline__ void inc_count(void)
 {
 	WRITE_ONCE(counter,
 		   READ_ONCE(counter) + 1);
 }
 
-unsigned long read_count(void)  /* known failure with counttorture! */
+static __inline__ unsigned long read_count(void)
+                  /* known failure with counttorture! */
 {
 	int t;
 	unsigned long sum = 0;
@@ -44,10 +46,10 @@ unsigned long read_count(void)  /* known failure with counttorture! */
 	return sum;
 }
 
-void count_init(void)
-{
-}
-
+void count_init(void)		//\fcvexclude
+{				//\fcvexclude
+}				//\fcvexclude
+				//\fcvexclude
 void count_register_thread(unsigned long *p)
 {
 	counterp[smp_thread_id()] = &counter;
@@ -61,6 +63,7 @@ void count_unregister_thread(int nthreadsexpected)
 	while (finalthreadcount < nthreadsexpected)
 		poll(NULL, 0, 1);
 }
+//\end{snippet}
 
 void count_cleanup(void)
 {
count/count.tex +33 -106

201 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/5 count: Fix uses of READ/WRITE_ONCE()s in count_end and count_tstat

In reply to Akira Yokosawa

From 4dc689c1439cc6f68989cdc2b5c177ec06002910 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Mon, 8 Oct 2018 09:13:15 +0900
Subject: [PATCH 2/5] count: Fix uses of READ/WRITE_ONCE()s in count_end and count_tstat

Add/remove READ_ONCE()/WRITE_ONCE()s to comply with the guideline to be expanded in toolsoftrade.

Note on addition of READ_ONCE() while holding a lock:
In read_count() of count_end.c, READ_ONCE() is added to access of *counterp[t] while holding final_mutex. This is because final_mutex protects only counterp[], but per-thread variables pointed to by the array are updated by the owning threads' inc_count().

---
 CodeSamples/count/count_end.c   |  5 ++---
 CodeSamples/count/count_tstat.c | 11 +++++------
 2 files changed, 7 insertions(+), 9 deletions(-)
CodeSamples/count/count_end.c +2 -3
index 94b9273..61f9d81 100644
@@ -29,8 +29,7 @@ DEFINE_SPINLOCK(final_mutex);			//\lnlbl{var:e}
 
 static __inline__ void inc_count(void)		//\lnlbl{inc:b}
 {
-	WRITE_ONCE(counter,
-		   READ_ONCE(counter) + 1);
+	WRITE_ONCE(counter, counter + 1);
 }						//\lnlbl{inc:e}
 
 static __inline__ unsigned long read_count(void)
@@ -42,7 +41,7 @@ static __inline__ unsigned long read_count(void)
 	sum = finalcount;				//\lnlbl{read:sum:init}
 	for_each_thread(t)				//\lnlbl{read:loop:b}
 		if (counterp[t] != NULL)		//\lnlbl{read:check}
-			sum += *counterp[t];		//\lnlbl{read:loop:e}
+			sum += READ_ONCE(*counterp[t]);	//\lnlbl{read:loop:e}
 	spin_unlock(&final_mutex);			//\lnlbl{read:release}
 	return sum;					//\lnlbl{read:return}
 }
CodeSamples/count/count_tstat.c +5 -6
index 4318bc3..4b99415 100644
@@ -30,8 +30,7 @@ DEFINE_SPINLOCK(final_mutex);
 
 static __inline__ void inc_count(void)
 {
-	WRITE_ONCE(counter,
-		   READ_ONCE(counter) + 1);
+	WRITE_ONCE(counter, counter + 1);
 }
 
 static __inline__ unsigned long read_count(void)
@@ -41,8 +40,8 @@ static __inline__ unsigned long read_count(void)
 	unsigned long sum = 0;
 
 	for_each_thread(t)
-		if (counterp[t] != NULL)
-			sum += *counterp[t];
+		if (READ_ONCE(counterp[t]) != NULL)
+			sum += READ_ONCE(*counterp[t]);
 	return sum;
 }
 
@@ -52,7 +51,7 @@ void count_init(void)		//\fcvexclude
 				//\fcvexclude
 void count_register_thread(unsigned long *p)
 {
-	counterp[smp_thread_id()] = &counter;
+	WRITE_ONCE(counterp[smp_thread_id()], &counter);
 }
 
 void count_unregister_thread(int nthreadsexpected)
@@ -60,7 +59,7 @@ void count_unregister_thread(int nthreadsexpected)
 	spin_lock(&final_mutex);
 	finalthreadcount++;
 	spin_unlock(&final_mutex);
-	while (finalthreadcount < nthreadsexpected)
+	while (READ_ONCE(finalthreadcount) < nthreadsexpected)
 		poll(NULL, 0, 1);
 }
 //\end{snippet}
Signature · 1 line
2.7.4
#3 lore raw

PATCH 3/5 count: Tweak counttorture.h to avoid segfault

In reply to Akira Yokosawa

From 19fb8b2915e5d918a6c1e98f265ab89bd373fea7 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sat, 6 Oct 2018 23:47:44 +0900
Subject: [PATCH 3/5] count: Tweak counttorture.h to avoid segfault

As has been mentioned in comment to count_read(), count_tstat sometimes fails. The cause is the use of count_read() in counttorture.h after wait_all_threads() returns. Because __thread variables are freed when owning threads exit, count_read() would access nonexistent variables.

As a workaround, add #ifndef KEEP_GCC_THREAD_LOCAL in counttorture.h and define _wait_all_thread(), _count_unregister_thread(), and final_wait_all_thread() as macros which change behavior when KEEP_GCC_THREAD_LOCAL is defined. They permit counttorture's controlling thread to participate in the thread group to be kept alive.

---
 CodeSamples/count/count_tstat.c  |  3 ++-
 CodeSamples/count/counttorture.h | 21 ++++++++++++++++++---
 count/count.tex                  |  4 ++--
 3 files changed, 22 insertions(+), 6 deletions(-)
CodeSamples/count/count_tstat.c +2 -1
index 4b99415..d30ee48 100644
@@ -34,7 +34,7 @@ static __inline__ void inc_count(void)
 }
 
 static __inline__ unsigned long read_count(void)
-                  /* known failure with counttorture! */
+                  /* need to tweak counttorture! */
 {
 	int t;
 	unsigned long sum = 0;
@@ -69,4 +69,5 @@ void count_cleanup(void)
 }
 
 #define NEED_REGISTER_THREAD
+#define KEEP_GCC_THREAD_LOCAL
 #include "counttorture.h"
CodeSamples/count/counttorture.h +18 -3
index bdfc7d4..60d1d11 100644
@@ -64,6 +64,20 @@ int goflag __attribute__((__aligned__(CACHE_LINE_SIZE))) = GOFLAG_INIT;
 #define count_unregister_thread(n)	do ; while (0)
 #endif /* #ifndef NEED_REGISTER_THREAD */
 
+#ifndef KEEP_GCC_THREAD_LOCAL
+#define _wait_all_threads() wait_all_threads()
+#define _count_unregister_thread(n) count_unregister_thread(n)
+#define final_wait_all_threads()
+#else  /* #ifndef KEEP_GCC_THREAD_LOCAL */
+#define _wait_all_threads() { \
+	while (READ_ONCE(finalthreadcount) < nthreadsexpected) \
+		poll(NULL, 0, 1);}
+#define _count_unregister_thread(n) count_unregister_thread(n + 1)
+#define final_wait_all_threads() { \
+	WRITE_ONCE(finalthreadcount, nthreadsexpected + 1); \
+	wait_all_threads();}
+#endif /* #ifndef KEEP_GCC_THREAD_LOCAL */
+
 unsigned long garbage = 0; /* disable compiler optimizations. */
 
 /*
@@ -90,7 +104,7 @@ void *count_read_perf_test(void *arg)
 		n_reads_local += COUNT_READ_RUN;
 	}
 	__get_thread_var(n_reads_pt) += n_reads_local;
-	count_unregister_thread(nthreadsexpected);
+	_count_unregister_thread(nthreadsexpected);
 	garbage += j;
 
 	return (NULL);
@@ -113,7 +127,7 @@ void *count_update_perf_test(void *arg)
 		n_updates_local += COUNT_UPDATE_RUN;
 	}
 	__get_thread_var(n_updates_pt) += n_updates_local;
-	count_unregister_thread(nthreadsexpected);
+	_count_unregister_thread(nthreadsexpected);
 	return NULL;
 }
 
@@ -139,7 +153,7 @@ void perftestrun(int nthreads, int nreaders, int nupdaters)
 	smp_mb();
 	goflag = GOFLAG_STOP;
 	smp_mb();
-	wait_all_threads();
+	_wait_all_threads();
 	count_cleanup();
 	for_each_thread(t) {
 		n_reads += per_thread(n_reads_pt, t);
@@ -159,6 +173,7 @@ void perftestrun(int nthreads, int nreaders, int nupdaters)
 	        (double)n_reads),
 	       ((duration * 1000*1000.*(double)nupdaters) /
 	        (double)n_updates));
+	final_wait_all_threads();
 	exit(EXIT_SUCCESS);
 }
 
count/count.tex +2 -2
index a4e9c7f..b008a19 100644
@@ -1127,9 +1127,9 @@ variables vanish when that thread exits.
 	Listing~\ref{lst:count:Per-Thread Statistical Counters With Lockless Summation}
 	(\path{count_tstat.c}).
 	Analysis of this code is left as an exercise to the reader,
-	however, please note that it does not fit well into the
+	however, please note that it requires tweaks in the
 	\path{counttorture.h} counter-evaluation scheme.
-	(Why not?)
+	(Hint: See \co{#ifndef KEEP_GCC_THREAD_LOCAL}.)
 	Chapter~\ref{chp:Deferred Processing} will introduce 
 	synchronization mechanisms that handle this situation in a much
 	more graceful manner.
Signature · 1 line
2.7.4
#4 lore raw

PATCH 4/5 count: Employ new scheme for snippet of count_lim

In reply to Akira Yokosawa

From 39a3830b185c9f4f6d65ac9162d9981522ed5b94 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Mon, 8 Oct 2018 10:23:32 +0900
Subject: [PATCH 4/5] count: Employ new scheme for snippet of count_lim

NOTE: To bundle code for the "Utility Functions" snippet, definition of globalize_count() and balance_count() is deferred to behind that of read_count().

---
 CodeSamples/count/count_lim.c | 147 +++++++++++----------
 count/count.tex               | 294 ++++++++++++++++--------------------------
 2 files changed, 192 insertions(+), 249 deletions(-)
CodeSamples/count/count_lim.c +79 -68
index 4cc8595..69ced9a 100644
@@ -20,105 +20,116 @@
 
 #include "../api.h"
 
+static __inline__ void globalize_count(void);
+static __inline__ void balance_count(void);
+
+//\begin{snippet}[labelbase=ln:count:count_lim:variable,commandchars=\\\@\$]
 unsigned long __thread counter = 0;
 unsigned long __thread countermax = 0;
-unsigned long globalcountmax = 10000;
-unsigned long globalcount = 0;
-unsigned long globalreserve = 0;
+unsigned long globalcountmax = 10000;		//\lnlbl{globalcountmax}
+unsigned long globalcount = 0;			//\lnlbl{globalcount}
+unsigned long globalreserve = 0;		//\lnlbl{globalreserve}
 unsigned long *counterp[NR_THREADS] = { NULL };
 DEFINE_SPINLOCK(gblcnt_mutex);
+//\end{snippet}
 
-static void globalize_count(void)
-{
-	globalcount += counter;
-	counter = 0;
-	globalreserve -= countermax;
-	countermax = 0;
-}
-
-static void balance_count(void)
-{
-	countermax = globalcountmax - globalcount - globalreserve;
-	countermax /= num_online_threads();
-	globalreserve += countermax;
-	counter = countermax / 2;
-	if (counter > globalcount)
-		counter = globalcount;
-	globalcount -= counter;
-}
-
-int add_count(unsigned long delta)
+//\begin{snippet}[labelbase=ln:count:count_lim:add_sub_read,commandchars=\\\@\$]
+static __inline__ int add_count(unsigned long delta)	//\lnlbl{add:b}
 {
-	if (countermax - counter >= delta) {
-		counter += delta;
-		return 1;
+	if (countermax - counter >= delta) {		//\lnlbl{add:checklocal}
+		counter += delta);			//\lnlbl{add:add}
+		return 1;				//\lnlbl{add:return:ls}
 	}
-	spin_lock(&gblcnt_mutex);
-	globalize_count();
-	if (globalcountmax - globalcount - globalreserve < delta) {
-		spin_unlock(&gblcnt_mutex);
-		return 0;
+	spin_lock(&gblcnt_mutex);			//\lnlbl{add:acquire}
+	globalize_count();				//\lnlbl{add:globalize}
+	if (globalcountmax -				//\lnlbl{add:checkglb:b}
+	    globalcount - globalreserve < delta) {	//\lnlbl{add:checkglb:e}
+		spin_unlock(&gblcnt_mutex);		//\lnlbl{add:release:f}
+		return 0;				//\lnlbl{add:return:gf}
 	}
-	globalcount += delta;
-	balance_count();
-	spin_unlock(&gblcnt_mutex);
-	return 1;
-}
+	globalcount += delta;				//\lnlbl{add:addglb}
+	balance_count();				//\lnlbl{add:balance}
+	spin_unlock(&gblcnt_mutex);			//\lnlbl{add:release:s}
+	return 1;					//\lnlbl{add:return:gs}
+}							//\lnlbl{add:e}
 
-int sub_count(unsigned long delta)
+static __inline__ int sub_count(unsigned long delta)	//\lnlbl{sub:b}
 {
-	if (counter >= delta) {
-		counter -= delta;
-		return 1;
+	if (counter >= delta) {				//\lnlbl{sub:checklocal}
+		counter -= delta;			//\lnlbl{sub:sub}
+		return 1;				//\lnlbl{sub:return:ls}
 	}
-	spin_lock(&gblcnt_mutex);
-	globalize_count();
-	if (globalcount < delta) {
-		spin_unlock(&gblcnt_mutex);
-		return 0;
+	spin_lock(&gblcnt_mutex);			//\lnlbl{sub:acquire}
+	globalize_count();				//\lnlbl{sub:globalize}
+	if (globalcount < delta) {			//\lnlbl{sub:checkglb}
+		spin_unlock(&gblcnt_mutex);		//\lnlbl{sub:release:f}
+		return 0;				//\lnlbl{sub:return:gf}
 	}
-	globalcount -= delta;
-	balance_count();
-	spin_unlock(&gblcnt_mutex);
-	return 1;
-}
+	globalcount -= delta;				//\lnlbl{sub:subglb}
+	balance_count();				//\lnlbl{sub:balance}
+	spin_unlock(&gblcnt_mutex);			//\lnlbl{sub:release:s}
+	return 1;					//\lnlbl{sub:return:gs}
+}							//\lnlbl{sub:e}
 
-unsigned long read_count(void)
+static __inline__ unsigned long read_count(void)	//\lnlbl{read:b}
 {
 	int t;
 	unsigned long sum;
 
-	spin_lock(&gblcnt_mutex);
-	sum = globalcount;
-	for_each_thread(t)
+	spin_lock(&gblcnt_mutex);			//\lnlbl{read:acquire}
+	sum = globalcount;				//\lnlbl{read:initsum}
+	for_each_thread(t)				//\lnlbl{read:loop:b}
 		if (counterp[t] != NULL)
-			sum += *counterp[t];
-	spin_unlock(&gblcnt_mutex);
-	return sum;
-}
+			sum += *counterp[t];		//\lnlbl{read:loop:e}
+	spin_unlock(&gblcnt_mutex);			//\lnlbl{read:release}
+	return sum;					//\lnlbl{read:return}
+}							//\lnlbl{read:e}
+//\end{snippet}
 
-void count_init(void)
+//\begin{snippet}[labelbase=ln:count:count_lim:utility,commandchars=\\\@\$]
+static __inline__ void globalize_count(void)	//\lnlbl{globalize:b}
 {
-}
+	globalcount += counter;			//\lnlbl{globalize:add}
+	counter = 0;				//\lnlbl{globalize:zero}
+	globalreserve -= countermax;		//\lnlbl{globalize:sub}
+	countermax = 0;			//\lnlbl{globalize:zeromax}
+}						//\lnlbl{globalize:e}
 
-void count_register_thread(void)
+static __inline__ void balance_count(void)	//\lnlbl{balance:b}
+{
+	countermax = globalcountmax -		//\lnlbl{balance:share:b}
+	             globalcount - globalreserve;
+	countermax /= num_online_threads();	//\lnlbl{balance:share:e}
+	globalreserve += countermax;		//\lnlbl{balance:adjreserve}
+	counter = countermax / 2;		//\lnlbl{balance:middle}
+	if (counter > globalcount)		//\lnlbl{balance:check}
+		counter = globalcount;		//\lnlbl{balance:adjcounter}
+	globalcount -= counter;			//\lnlbl{balance:adjglobal}
+}						//\lnlbl{balance:e}
+
+void count_init(void)			//\fcvexclude
+{					//\fcvexclude
+}					//\fcvexclude
+					//\fcvexclude
+void count_register_thread(void)	//\lnlbl{register:b}
 {
 	int idx = smp_thread_id();
 
 	spin_lock(&gblcnt_mutex);
 	counterp[idx] = &counter;
 	spin_unlock(&gblcnt_mutex);
-}
+}					//\lnlbl{register:e}
 
-void count_unregister_thread(int nthreadsexpected)
+void count_unregister_thread(int nthreadsexpected)	//\lnlbl{unregister:b}
 {
 	int idx = smp_thread_id();
 
-	spin_lock(&gblcnt_mutex);
-	globalize_count();
-	counterp[idx] = NULL;
-	spin_unlock(&gblcnt_mutex);
-}
+	spin_lock(&gblcnt_mutex);		//\lnlbl{unregister:acquire}
+	globalize_count();			//\lnlbl{unregister:globalize}
+	counterp[idx] = NULL;			//\lnlbl{unregister:clear}
+	spin_unlock(&gblcnt_mutex);		//\lnlbl{unregister:release}
+}						//\lnlbl{unregister:e}
+//\end{snippet}
 
 void count_cleanup(void)
 {
count/count.tex +113 -181

440 lines not on this page · raw message

Signature · 1 line
2.7.4
#5 lore raw

PATCH 5/5 count: Fix uses of READ/WRITE_ONCE() in count_lim

In reply to Akira Yokosawa

From 83f496142e594e69f89b154bebcfd8d278ac7831 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Mon, 8 Oct 2018 10:26:24 +0900
Subject: [PATCH 5/5] count: Fix uses of READ/WRITE_ONCE() in count_lim

Add a few READ_ONCE()/WRITE_ONCE()s to prevent load/store tearing of per-thread variables "counter" and "countermax".

NOTE: Updates of those per-thread variables in utility functions don't need WRITE_ONCE()s because they are protected by glbcnt_mutex and can't be seen by read_count().

---
 CodeSamples/count/count_lim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
CodeSamples/count/count_lim.c +3 -3
index 69ced9a..c207beb 100644
@@ -37,7 +37,7 @@ DEFINE_SPINLOCK(gblcnt_mutex);
 static __inline__ int add_count(unsigned long delta)	//\lnlbl{add:b}
 {
 	if (countermax - counter >= delta) {		//\lnlbl{add:checklocal}
-		counter += delta);			//\lnlbl{add:add}
+		WRITE_ONCE(counter, counter + delta);	//\lnlbl{add:add}
 		return 1;				//\lnlbl{add:return:ls}
 	}
 	spin_lock(&gblcnt_mutex);			//\lnlbl{add:acquire}
@@ -56,7 +56,7 @@ static __inline__ int add_count(unsigned long delta)	//\lnlbl{add:b}
 static __inline__ int sub_count(unsigned long delta)	//\lnlbl{sub:b}
 {
 	if (counter >= delta) {				//\lnlbl{sub:checklocal}
-		counter -= delta;			//\lnlbl{sub:sub}
+		WRITE_ONCE(counter, counter - delta);	//\lnlbl{sub:sub}
 		return 1;				//\lnlbl{sub:return:ls}
 	}
 	spin_lock(&gblcnt_mutex);			//\lnlbl{sub:acquire}
@@ -80,7 +80,7 @@ static __inline__ unsigned long read_count(void)	//\lnlbl{read:b}
 	sum = globalcount;				//\lnlbl{read:initsum}
 	for_each_thread(t)				//\lnlbl{read:loop:b}
 		if (counterp[t] != NULL)
-			sum += *counterp[t];		//\lnlbl{read:loop:e}
+			sum += READ_ONCE(*counterp[t]);	//\lnlbl{read:loop:e}
 	spin_unlock(&gblcnt_mutex);			//\lnlbl{read:release}
 	return sum;					//\lnlbl{read:return}
 }							//\lnlbl{read:e}
Signature · 1 line
2.7.4
#6 lore raw

[PATCH 6/5] count: Fix typo (\lnlbl{} -> \lnref{})

In reply to Akira Yokosawa

From 6cd4d087e034532c228589aa82b287233e45ccf8 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Mon, 8 Oct 2018 21:44:10 +0900
Subject: [PATCH 6/5] count: Fix typo (\lnlbl{} -> \lnref{})

---
Paul,

This fixes a couple of typo in "[PATCH 4/5] count: Employ new scheme for snippet of count_lim".

Thanks, Akira
--

 count/count.tex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
count/count.tex +1 -1
index db2107e..f8c68a4 100644
@@ -1482,7 +1482,7 @@ These lines form \co{sub_count()}'s fastpath, and, as with
 
 If the fastpath cannot accommodate subtraction of \co{delta},
 execution proceeds to the slowpath on
-lines~\lnlbl{acquire}-\lnlbl{return:gs}.
+lines~\lnref{acquire}-\lnref{return:gs}.
 Because the slowpath must access global state, line~\lnref{acquire}
 acquires \co{gblcnt_mutex}, which is released either by line~\lnref{release:f}
 (in case of failure) or by line~\lnref{release:s} (in case of success).
Signature · 1 line
2.7.4
#7 lore raw
In reply to Akira Yokosawa
Quoted 9 lines · Akira Yokosawa

Hi Paul,

This is the continuation of changes to snippets in the "count" chapter.

To help your review of the changes regarding READ_ONCE()/WRITE_ONCE(), I split the patches to the ones just applying the scheme and the ones modifying READ_ONCE()/WRITE_ONCE().

Quoted 6 lines

Note on patch #3: It fixes the segfault observed in count_tstat. However, I'm not sure if you want to take it or not. The fail can be thought of as a Quick Quiz within the Answer to the Quick Quiz. So keeping it as is would also be reasonable.

Thanks, Akira

They look good at first glance, so I have queued and pushed them. Thank you very much!

Thanx, Paul

Quoted 18 lines

--
Akira Yokosawa (5):
count: Employ new scheme for snippet of count_end and count_tstat
count: Fix uses of READ/WRITE_ONCE()s in count_end and count_tstat
count: Tweak counttorture.h to avoid segfault
count: Employ new scheme for snippet of count_lim
count: Fix uses of READ/WRITE_ONCE() in count_lim

 CodeSamples/count/count_end.c    |  53 ++---
 CodeSamples/count/count_lim.c    | 147 +++++++------
 CodeSamples/count/count_tstat.c  |  27 +--
 CodeSamples/count/counttorture.h |  21 +-
 count/count.tex                  | 437 +++++++++++++--------------------------
 5 files changed, 287 insertions(+), 398 deletions(-)
Signature · 1 line
2.7.4