count, toolsoftrade: Employ new code-snippet scheme (cont.)
cover count, toolsoftrade: Employ new code-snippet scheme (cont.)
Hi Paul,
This is a set of simple conversions of code snippets to the new scheme in count and toolsoftrade. There is no change in the code under CodeSamples, but there are a few visual changes in the pdf output which reflect the actual code samples.
Patch #7 fixes an unintended change in indent width made in the previous patch set.
Thanks, Akira
--
Akira Yokosawa (7):
count: Employ new scheme for inline code snippets
toolsoftrade: Use 'VerbatimU' for inline snippets
toolsoftrade: Employ new scheme for snippets of lock.c
toolsoftrade: Employ new scheme for snippet of rwlockscale.c
toolsoftrade: Employ new scheme for snippet of compiler barriers
toolsoftrade: Employ new scheme for snippets of threadcreate.c
count: Fix indent in count_lim_atomic.c
CodeSamples/api-pthreads/api-pthreads.h | 7 +- CodeSamples/count/count_lim_atomic.c | 2 +- CodeSamples/intro/threadcreate.c | 20 +- CodeSamples/toolsoftrade/lock.c | 76 +++--- CodeSamples/toolsoftrade/rwlockscale.c | 54 ++-- count/count.tex | 89 +++--- toolsoftrade/toolsoftrade.tex | 460 ++++++++------------------------ 7 files changed, 253 insertions(+), 455 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/7 count: Employ new scheme for inline code snippets
From c8ed9b923cfdc68403998cd154f07f7834ad4edb Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 14 Oct 2018 10:50:24 +0900
Subject: [PATCH 1/7] count: Employ new scheme for inline code snippets
--- count/count.tex | 89 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 43 deletions(-)
count/count.tex +46 -43
@@ -2679,30 +2679,33 @@ when updating the counter, and to write-acquire that same reader-writer lock when checking the counter. Code for doing I/O might be as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} - 1 read_lock(&mylock); - 2 if (removing) { - 3 read_unlock(&mylock); - 4 cancel_io(); - 5 } else { - 6 add_count(1); - 7 read_unlock(&mylock); - 8 do_io(); - 9 sub_count(1); - 10 } -\end{verbatim} -\end{minipage} -\vspace{5pt} - -Line~1 read-acquires the lock, and either line~3 or~7 releases it. -Line~2 checks to see if the device is being removed, and, if so, -line~3 releases the lock and line~4 cancels the I/O, or takes whatever +\begin{linelabel}[ln:count:inline:I/O] +\begin{VerbatimN}[commandchars=\\\[\]] +read_lock(&mylock); \lnlbl[acq] +if (removing) { \lnlbl[check] + read_unlock(&mylock); \lnlbl[rel1] + cancel_io(); \lnlbl[cancel] +} else { + add_count(1); \lnlbl[inc] + read_unlock(&mylock); \lnlbl[rel2] + do_io(); \lnlbl[do] + sub_count(1); \lnlbl[dec] +} +\end{VerbatimN} +\end{linelabel} + +\begin{lineref}[ln:count:inline:I/O] +Line~\lnref{acq} read-acquires the lock, and either +line~\lnref{rel1} or~\lnref{rel2} releases it. +Line~\lnref{check} checks to see if the device is being removed, and, if so, +line~\lnref{rel1} releases the lock and +line~\lnref{cancel} cancels the I/O, or takes whatever action is appropriate given that the device is to be removed. -Otherwise, line~6 increments the access count, line~7 releases the -lock, line~8 performs the I/O, and line~9 decrements the access count. +Otherwise, line~\lnref{inc} increments the access count, +line~\lnref{rel2} releases the +lock, line~\lnref{do} performs the I/O, and +line~\lnref{dec} decrements the access count. +\end{lineref} \QuickQuiz{} This is ridiculous!
@@ -2717,27 +2720,27 @@ lock, line~8 performs the I/O, and line~9 decrements the access count. The code to remove the device might be as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} - 1 write_lock(&mylock); - 2 removing = 1; - 3 sub_count(mybias); - 4 write_unlock(&mylock); - 5 while (read_count() != 0) { - 6 poll(NULL, 0, 1); - 7 } - 8 remove_device(); -\end{verbatim} -\end{minipage} -\vspace{5pt} - -Line~1 write-acquires the lock and line~4 releases it. -Line~2 notes that the device is being removed, and the loop spanning -lines~5-7 wait for any I/O operations to complete. -Finally, line~8 does any additional processing needed to prepare for +\begin{linelabel}[ln:count:inline:remove] +\begin{VerbatimN}[commandchars=\\\[\]] +write_lock(&mylock); \lnlbl[acq] +removing = 1; \lnlbl[note] +sub_count(mybias); +write_unlock(&mylock); \lnlbl[rel] +while (read_count() != 0) { \lnlbl[loop:b] + poll(NULL, 0, 1); +} \lnlbl[loop:e] +remove_device(); \lnlbl[remove] +\end{VerbatimN} +\end{linelabel} + +\begin{lineref}[ln:count:inline:remove] +Line~\lnref{acq} write-acquires the lock and +line~\lnref{rel} releases it. +Line~\lnref{note} notes that the device is being removed, and the loop spanning +lines~\lnref{loop:b}-\lnref{loop:e} wait for any I/O operations to complete. +Finally, line~\lnref{remove} does any additional processing needed to prepare for device removal. +\end{lineref} \QuickQuiz{} What other issues would need to be accounted for in a real system?
Signature · 1 line
2.7.4
PATCH 2/7 toolsoftrade: Use 'VerbatimU' for inline snippets
In reply to Akira YokosawaFrom f65277139c063f4b44987edb0eee40ade1957df2 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 14 Oct 2018 14:54:14 +0900
Subject: [PATCH 2/7] toolsoftrade: Use 'VerbatimU' for inline snippets
--- toolsoftrade/toolsoftrade.tex | 90 ++++++++++--------------------------------- 1 file changed, 20 insertions(+), 70 deletions(-)
toolsoftrade/toolsoftrade.tex +20 -70
@@ -88,14 +88,9 @@ of \co{cat} execute sequentially. \QuickQuizAnswer{ One straightforward approach is the shell pipeline: -\begin{minipage}[t]{\columnwidth} -\vspace{0.1ex} -\small -\begin{verbatim} +\begin{VerbatimU} grep $pattern1 | sed -e 's/a/b/' | sort -\end{verbatim} -\vspace{0.1ex} -\end{minipage} +\end{VerbatimU} For a sufficiently large input file, \co{grep} will pattern-match in parallel with \co{sed}
@@ -648,15 +643,10 @@ Lines~7-11 create a thread running \co{lock_writer()}. Lines~12-19 wait for both threads to complete. The output of this code fragment is as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\scriptsize -\begin{verbatim} +\begin{VerbatimU} Creating two threads using same lock: lock_reader(): x = 0 -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} Because both threads are using the same lock, the \co{lock_reader()} thread cannot see any of the intermediate values of \co{x} produced
@@ -718,18 +708,13 @@ shows a similar code fragment, but this time using different locks: \co{lock_writer()}. The output of this code fragment is as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\scriptsize -\begin{verbatim} +\begin{VerbatimU} Creating two threads w/different locks: lock_reader(): x = 0 lock_reader(): x = 1 lock_reader(): x = 2 lock_reader(): x = 3 -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} Because the two threads are using different locks, they do not exclude each other, and can run concurrently.
@@ -1144,16 +1129,11 @@ If you instead need the new value, you can instead use the For example, one could implement \co{__sync_nand_and_fetch()} in terms of \co{__sync_fetch_and_nand()} as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\scriptsize -\begin{verbatim} +\begin{VerbatimU} tmp = v; ret = __sync_fetch_and_nand(p, tmp); ret = ~ret & tmp; -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} It is similarly possible to implement \co{__sync_fetch_and_add()}, \co{__sync_fetch_and_sub()}, and \co{__sync_fetch_and_xor()}
@@ -1615,16 +1595,11 @@ allowing other threads to acquire it. A spinlock named \co{mutex} may be used to protect a variable \co{counter} as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} +\begin{VerbatimU} spin_lock(&mutex); counter++; spin_unlock(&mutex); -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} \QuickQuiz{} What problems could occur if the variable {\tt counter} were
@@ -1633,16 +1608,11 @@ spin_unlock(&mutex); On CPUs with load-store architectures, incrementing {\tt counter} might compile into something like the following: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} +\begin{VerbatimU} LOAD counter,r0 INC r0 STORE r0,counter -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} On such machines, two threads might simultaneously load the value of {\tt counter}, each increment it, and each store the
@@ -2505,51 +2475,31 @@ Section~\ref{sec:count:Statistical Counters}, it is helpful to implement such a counter using a per-thread variable. Such a variable can be defined as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} +\begin{VerbatimU} DEFINE_PER_THREAD(int, counter); -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} The counter must be initialized as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} +\begin{VerbatimU} init_per_thread(counter, 0); -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} A thread can increment its instance of this counter as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} +\begin{VerbatimU} p_counter = &__get_thread_var(counter); WRITE_ONCE(*p_counter, *p_counter + 1); -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} The value of the counter is then the sum of its instances. A snapshot of the value of the counter can thus be collected as follows: -\vspace{5pt} -\begin{minipage}[t]{\columnwidth} -\small -\begin{verbatim} +\begin{VerbatimU} for_each_thread(t) sum += READ_ONCE(per_thread(counter, t)); -\end{verbatim} -\end{minipage} -\vspace{5pt} +\end{VerbatimU} Again, it is possible to gain a similar effect using other mechanisms, but per-thread variables combine convenience and high performance,
Signature · 1 line
2.7.4
PATCH 3/7 toolsoftrade: Employ new scheme for snippets of lock.c
In reply to Akira YokosawaFrom 4bf3961a5c8501a9983a196119038b74cff7a18d Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 14 Oct 2018 16:23:34 +0900
Subject: [PATCH 3/7] toolsoftrade: Employ new scheme for snippets of lock.c
NOTE: Several "if" statements, which are too wide for 2c layout, are divided into a statement to get error code and a simple "if" statement.
--- CodeSamples/toolsoftrade/lock.c | 76 +++++++++-------- toolsoftrade/toolsoftrade.tex | 175 +++++++++++----------------------------- 2 files changed, 89 insertions(+), 162 deletions(-)
CodeSamples/toolsoftrade/lock.c +43 -33
@@ -26,62 +26,64 @@#include <errno.h> #include "../api.h" -pthread_mutex_t lock_a =PTHREAD_MUTEX_INITIALIZER ; -pthread_mutex_t lock_b =PTHREAD_MUTEX_INITIALIZER ; +//\begin{snippet}[labelbase=ln:toolsoftrade:lock:reader_writer,commandchars=\$\[\]] +pthread_mutex_t lock_a =PTHREAD_MUTEX_INITIALIZER ;//\lnlbl{lock_a} +pthread_mutex_t lock_b =PTHREAD_MUTEX_INITIALIZER ;//\lnlbl{lock_b} -int x =0 ; +int x =0 ;//\lnlbl{x} -void *lock_reader (void *arg) +void *lock_reader (void *arg)//\lnlbl{reader:b} {int en;int i;int newx =-1 ;int oldx =-1 ; -pthread_mutex_t *pmlp = (pthread_mutex_t *)arg; +pthread_mutex_t *pmlp = (pthread_mutex_t *)arg;//\lnlbl{reader:cast} -if ((en =pthread_mutex_lock (pmlp)) !=0 ) { +if ((en =pthread_mutex_lock (pmlp)) !=0 ) {//\lnlbl{reader:acq:b} fprintf (stderr,"lock_reader:pthread_mutex_lock: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } -for (i =0 ; i <100 ; i++) { - newx =READ_ONCE (x); + }//\lnlbl{reader:acq:e} +for (i =0 ; i <100 ; i++) {//\lnlbl{reader:loop:b} + newx =READ_ONCE (x);//\lnlbl{reader:read_x} if (newx != oldx) {printf ("lock_reader(): x = %d\n" , newx); } oldx = newx; -poll (NULL ,0 ,1 ); - } -if ((en =pthread_mutex_unlock (pmlp)) !=0 ) { +poll (NULL ,0 ,1 );//\lnlbl{reader:sleep} + }//\lnlbl{reader:loop:e} +if ((en =pthread_mutex_unlock (pmlp)) !=0 ) {//\lnlbl{reader:rel:b} fprintf (stderr,"lock_reader:pthread_mutex_lock: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } -return NULL ; -} + }//\lnlbl{reader:rel:e} +return NULL ;//\lnlbl{reader:return} +}//\lnlbl{reader:e} -void *lock_writer (void *arg) +void *lock_writer (void *arg)//\lnlbl{writer:b} {int en;int i; -pthread_mutex_t *pmlp = (pthread_mutex_t *)arg; +pthread_mutex_t *pmlp = (pthread_mutex_t *)arg;//\lnlbl{writer:cast} -if ((en =pthread_mutex_lock (pmlp)) !=0 ) { +if ((en =pthread_mutex_lock (pmlp)) !=0 ) {//\lnlbl{writer:acq:b} fprintf (stderr,"lock_writer:pthread_mutex_lock: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } -for (i =0 ; i <3 ; i++) { -WRITE_ONCE (x,READ_ONCE (x) +1 ); + }//\lnlbl{writer:acq:e} +for (i =0 ; i <3 ; i++) {//\lnlbl{writer:loop:b} +WRITE_ONCE (x,READ_ONCE (x) +1 );//\lnlbl{writer:inc} poll (NULL ,0 ,5 ); - } -if ((en =pthread_mutex_unlock (pmlp)) !=0 ) { + }//\lnlbl{writer:loop:e} +if ((en =pthread_mutex_unlock (pmlp)) !=0 ) {//\lnlbl{writer:rel:b} fprintf (stderr,"lock_writer:pthread_mutex_lock: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } + }//\lnlbl{writer:rel:e} return NULL ; -} +}//\lnlbl{writer:e} +//\end{snippet} int main (int argc,char *argv[]) {
@@ -90,31 +92,38 @@ int main(int argc, char *argv[])pthread_t tid2;void *vp; +//\begin{snippet}[labelbase=ln:toolsoftrade:lock:same_lock,commandchars=\$\[\]] printf ("Creating two threads using same lock:\n" ); -if ((en =pthread_create (&tid1,NULL , lock_reader, &lock_a)) !=0 ) { + en =pthread_create (&tid1,NULL , lock_reader, &lock_a);//\lnlbl{cr:reader:b} +if (en !=0 ) {fprintf (stderr,"pthread_create: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } -if ((en =pthread_create (&tid2,NULL , lock_writer, &lock_a)) !=0 ) { + }//\lnlbl{cr:reader:e} + en =pthread_create (&tid2,NULL , lock_writer, &lock_a);//\lnlbl{cr:writer:b} +if (en !=0 ) {fprintf (stderr,"pthread_create: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } -if ((en =pthread_join (tid1, &vp)) !=0 ) { + }//\lnlbl{cr:writer:e} +if ((en =pthread_join (tid1, &vp)) !=0 ) {//\lnlbl{wait:b} fprintf (stderr,"pthread_join: %s\n" ,strerror (en));exit (EXIT_FAILURE ); }if ((en =pthread_join (tid2, &vp)) !=0 ) {fprintf (stderr,"pthread_join: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } + }//\lnlbl{wait:e} +//\end{snippet} +//\begin{snippet}[labelbase=ln:toolsoftrade:lock:diff_lock,commandchars=\$\[\]] printf ("Creating two threads w/different locks:\n" ); x =0 ; -if ((en =pthread_create (&tid1,NULL , lock_reader, &lock_a)) !=0 ) { + en =pthread_create (&tid1,NULL , lock_reader, &lock_a); +if (en !=0 ) {fprintf (stderr,"pthread_create: %s\n" ,strerror (en));exit (EXIT_FAILURE ); } -if ((en =pthread_create (&tid2,NULL , lock_writer, &lock_b)) !=0 ) { + en =pthread_create (&tid2,NULL , lock_writer, &lock_b); +if (en !=0 ) {fprintf (stderr,"pthread_create: %s\n" ,strerror (en));exit (EXIT_FAILURE ); }
@@ -126,6 +135,7 @@ int main(int argc, char *argv[])fprintf (stderr,"pthread_join: %s\n" ,strerror (en));exit (EXIT_FAILURE ); } +//\end{snippet} return EXIT_SUCCESS ; }
toolsoftrade/toolsoftrade.tex +46 -129
247 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 4/7 toolsoftrade: Employ new scheme for snippet of rwlockscale.c
In reply to Akira YokosawaFrom 5f85ee80de39323a192da10c4a25ae8ec0f4cd2c Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 14 Oct 2018 18:00:26 +0900
Subject: [PATCH 4/7] toolsoftrade: Employ new scheme for snippet of rwlockscale.c
--- CodeSamples/toolsoftrade/rwlockscale.c | 54 ++++++++++---------- toolsoftrade/toolsoftrade.tex | 93 +++++++++++----------------------- 2 files changed, 57 insertions(+), 90 deletions(-)
CodeSamples/toolsoftrade/rwlockscale.c +28 -26
@@ -26,50 +26,52 @@#include <errno.h> #include "../api.h" -pthread_rwlock_t rwl =PTHREAD_RWLOCK_INITIALIZER ; -int holdtime =0 ;/* # loops holding lock. */ -int thinktime =0 ;/* # loops not holding lock. */ -long long *readcounts; -int nreadersrunning =0 ; +//\begin{snippet}[labelbase=ln:toolsoftrade:rwlockscale:reader,commandchars=\@\^\$] +pthread_rwlock_t rwl =PTHREAD_RWLOCK_INITIALIZER ;//\lnlbl{rwlock} +int holdtime =0 ;/* # loops holding lock. */ //\lnlbl{holdtm} +int thinktime =0 ;/* # loops not holding lock. */ //\lnlbl{thinktm} +long long *readcounts;//\lnlbl{rdcnts} +int nreadersrunning =0 ;//\lnlbl{nrdrun} -#define GOFLAG_INIT 0 +#define GOFLAG_INIT 0 //\lnlbl{goflag:b}#define GOFLAG_RUN 1#define GOFLAG_STOP 2 -char goflag =GOFLAG_INIT ; +char goflag =GOFLAG_INIT ;//\lnlbl{goflag:e} -void *reader (void *arg) +void *reader (void *arg)//\lnlbl{reader:b} {int en;int i;long long loopcnt =0 ;long me = (long )arg; -__sync_fetch_and_add (&nreadersrunning,1 ); -while (READ_ONCE (goflag) ==GOFLAG_INIT ) { +__sync_fetch_and_add (&nreadersrunning,1 );//\lnlbl{reader:atmc_inc} +while (READ_ONCE (goflag) ==GOFLAG_INIT ) {//\lnlbl{reader:wait:b} continue ; - } -while (READ_ONCE (goflag) ==GOFLAG_RUN ) { -if ((en =pthread_rwlock_rdlock (&rwl)) !=0 ) { + }//\lnlbl{reader:wait:e} +while (READ_ONCE (goflag) ==GOFLAG_RUN ) {//\lnlbl{reader:loop:b} +if ((en =pthread_rwlock_rdlock (&rwl)) !=0 ) {//\lnlbl{reader:acq:b} fprintf (stderr, -"pthread_rwlock_rdlock: %s\n" ,strerror (en)); +"pthread_rwlock_rdlock: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } -for (i =1 ; i < holdtime; i++) { + }//\lnlbl{reader:acq:e} +for (i =1 ; i < holdtime; i++) {//\lnlbl{reader:hold:b} barrier (); - } -if ((en =pthread_rwlock_unlock (&rwl)) !=0 ) { + }//\lnlbl{reader:hold:e} +if ((en =pthread_rwlock_unlock (&rwl)) !=0 ) {//\lnlbl{reader:rel:b} fprintf (stderr,"pthread_rwlock_unlock: %s\n" ,strerror (en));exit (EXIT_FAILURE ); - } -for (i =1 ; i < thinktime; i++) { + }//\lnlbl{reader:rel:e} +for (i =1 ; i < thinktime; i++) {//\lnlbl{reader:think:b} barrier (); - } - loopcnt++; - } - readcounts[me] = loopcnt; -return NULL ; -} + }//\lnlbl{reader:think:e} + loopcnt++;//\lnlbl{reader:count} + }//\lnlbl{reader:loop:e} + readcounts[me] = loopcnt;//\lnlbl{reader:mov_cnt} +return NULL ;//\lnlbl{reader:return} +}//\lnlbl{reader:e} +//\end{snippet} int main (int argc,char *argv[]) {
toolsoftrade/toolsoftrade.tex +29 -64
132 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 5/7 toolsoftrade: Employ new scheme for snippet of compiler barriers
In reply to Akira YokosawaFrom 65dac5efc38d7dbb1710d727bc6b67732ce8968c Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 14 Oct 2018 18:11:20 +0900
Subject: [PATCH 5/7] toolsoftrade: Employ new scheme for snippet of compiler barriers
--- CodeSamples/api-pthreads/api-pthreads.h | 7 +++++-- toolsoftrade/toolsoftrade.tex | 12 +----------- 2 files changed, 6 insertions(+), 13 deletions(-)
CodeSamples/api-pthreads/api-pthreads.h +5 -2
@@ -53,7 +53,6 @@ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})#endif /* #ifndef offsetof */ -#define barrier () __asm__ __volatile__("": : :"memory")/* * Default machine parameters.
@@ -133,9 +132,13 @@ static __inline__ int spin_is_locked(spinlock_t *sp)#define spin_lock_irqsave (l, f) do { f = 1; spin_lock(l); } while (0)#define spin_unlock_irqrestore (l, f) do { f = 0; spin_unlock(l); } while (0) +//\begin{snippet}[labelbase=ln:api-pthreads:api-pthreads:compiler_barrier,commandchars=\@\[\],numbers=none] #define ACCESS_ONCE (x) (*(volatile typeof(x) *)&(x)) -#define READ_ONCE (x) ({ typeof(x) ___x = ACCESS_ONCE(x); ___x; }) +#define READ_ONCE (x) \ + ({ typeof(x) ___x = ACCESS_ONCE(x); ___x; })#define WRITE_ONCE (x, val) ({ ACCESS_ONCE(x) = (val); }) +#define barrier () __asm__ __volatile__("": : :"memory") +//\end{snippet} #ifndef unlikely#define unlikely (x) x#endif /* #ifndef unlikely */
toolsoftrade/toolsoftrade.tex +1 -11
@@ -1076,17 +1076,7 @@ and all three are discussed at length in Section~\ref{sec:toolsoftrade:Accessing Shared Variables}. \begin{listing}[tb] -{ \scriptsize -\begin{verbbox} -#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) -#define READ_ONCE(x) \ - ({ typeof(x) ___x = ACCESS_ONCE(x); ___x; }) -#define WRITE_ONCE(x, val) ({ ACCESS_ONCE(x) = (val); }) -#define barrier() __asm__ __volatile__("": : :"memory") -\end{verbbox} -} -\centering -\theverbbox +\input{CodeSamples/api-pthreads/api-pthreads@compiler_barrier.fcv} \caption{Compiler Barrier Primitive (for \GCC)} \label{lst:toolsoftrade:Compiler Barrier Primitive (for GCC)} \end{listing}
Signature · 1 line
2.7.4
PATCH 6/7 toolsoftrade: Employ new scheme for snippets of threadcreate.c
In reply to Akira YokosawaFrom 334b421f68729c28fac868f5d89af6d4aa6d1090 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 14 Oct 2018 20:47:07 +0900
Subject: [PATCH 6/7] toolsoftrade: Employ new scheme for snippets of threadcreate.c
Also convert snippets of thread API, locking API, and per-thread API. As the snippet of thread API fits the column width of 2c layout, use "listing" environment instead of "listing*".
--- CodeSamples/intro/threadcreate.c | 20 +++++---- toolsoftrade/toolsoftrade.tex | 90 ++++++++++------------------------------ 2 files changed, 34 insertions(+), 76 deletions(-)
CodeSamples/intro/threadcreate.c +12 -8
@@ -20,14 +20,16 @@#include "../api.h" +//\begin{snippet}[labelbase=ln:intro:threadcreate:thread_test,commandchars=\@\[\]] void *thread_test (void *arg) {int myarg = (intptr_t )arg;printf ("child thread %d: smp_thread_id() = %d\n" , myarg,smp_thread_id ()); -return NULL ; +return NULL ;//\lnlbl{return} } +//\end{snippet} void usage (char *progname) {
@@ -36,29 +38,31 @@ void usage(char *progname)exit (-1 ); } +//\begin{snippet}[labelbase=ln:intro:threadcreate:main,commandchars=\@\^\$] int main (int argc,char *argv[]) {int i;int nkids =1 ; -smp_init (); +smp_init ();//\lnlbl{smp_init} -if (argc >1 ) { +if (argc >1 ) {//\lnlbl{parse:b} nkids =strtoul (argv[1 ],NULL ,0 );if (nkids >NR_THREADS ) {fprintf (stderr,"nkids = %d too large, max = %d\n" , nkids,NR_THREADS );usage (argv[0 ]); } - } -printf ("Parent thread spawning %d threads.\n" , nkids); + }//\lnlbl{parse:e} +printf ("Parent thread spawning %d threads.\n" , nkids);//\lnlbl{announce} -for (i =0 ; i < nkids; i++) -create_thread (thread_test, (void *)(intptr_t )i); +for (i =0 ; i < nkids; i++)//\lnlbl{create:b} +create_thread (thread_test, (void *)(intptr_t )i);//\lnlbl{create:e} -wait_all_threads (); +wait_all_threads ();//\lnlbl{wait} printf ("All spawned threads completed.\n" );exit (0 ); } +//\end{snippet}
toolsoftrade/toolsoftrade.tex +22 -68
151 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 7/7 count: Fix indent in count_lim_atomic.c
In reply to Akira YokosawaFrom 453bc63fd38da42f52eb88b4fed0a38b0f431cf9 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sat, 20 Oct 2018 07:50:26 +0900
Subject: [PATCH 7/7] count: Fix indent in count_lim_atomic.c
Commit 51e9218ac25e ("count: Employ new scheme for snippet of count_lim_atomic") changed indent width by accident. Fix it.
--- CodeSamples/count/count_lim_atomic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
CodeSamples/count/count_lim_atomic.c +1 -1
@@ -137,7 +137,7 @@ int sub_count(unsigned long delta) //\lnlbl{sub:b}do {//\lnlbl{sub:fast:b} split_counterandmax (&counterandmax, &old, &c, &cm);if (delta > c) - goto slowpath; + goto slowpath; new =merge_counterandmax (c - delta, cm); }while (atomic_cmpxchg (&counterandmax, old, new) != old);
Signature · 1 line
2.7.4
Quoted 6 lines · Akira Yokosawa
Hi Paul,
This is a set of simple conversions of code snippets to the new scheme in count and toolsoftrade. There is no change in the code under CodeSamples, but there are a few visual changes in the pdf output which reflect the actual code samples.
I forgot to mention that Patch #3 contains minor changes in the code to reduce width of code snippets.
Thanks, Akira
Quoted 24 lines
Patch #7 fixes an unintended change in indent width made in the previous patch set.
Thanks, Akira
--
Akira Yokosawa (7):
count: Employ new scheme for inline code snippets
toolsoftrade: Use 'VerbatimU' for inline snippets
toolsoftrade: Employ new scheme for snippets oflock.c
toolsoftrade: Employ new scheme for snippet ofrwlockscale.c
toolsoftrade: Employ new scheme for snippet of compiler barriers
toolsoftrade: Employ new scheme for snippets ofthreadcreate.c
count: Fix indent incount_lim_atomic.cCodeSamples/api-pthreads/api-pthreads.h | 7 +- CodeSamples/count/count_lim_atomic.c | 2 +- CodeSamples/intro/threadcreate.c | 20 +- CodeSamples/toolsoftrade/lock.c | 76 +++--- CodeSamples/toolsoftrade/rwlockscale.c | 54 ++-- count/count.tex | 89 +++--- toolsoftrade/toolsoftrade.tex | 460 ++++++++------------------------ 7 files changed, 253 insertions(+), 455 deletions(-)
Paul E. McKenney
AppliedQuoted 8 lines · Akira Yokosawa
Quoted 6 lines · Akira Yokosawa
Hi Paul,
This is a set of simple conversions of code snippets to the new scheme in count and toolsoftrade. There is no change in the code under CodeSamples, but there are a few visual changes in the pdf output which reflect the actual code samples.
Quoted 10 lines
I forgot to mention that Patch #3 contains minor changes in the code to reduce width of code snippets.
Thanks, Akira
Quoted 5 lines
Patch #7 fixes an unintended change in indent width made in the previous patch set.
Thanks, Akira
I applied and pushed this series, thank you!
Thanx, Paul
Quoted 20 lines
Quoted 19 lines
--
Akira Yokosawa (7):
count: Employ new scheme for inline code snippets
toolsoftrade: Use 'VerbatimU' for inline snippets
toolsoftrade: Employ new scheme for snippets oflock.c
toolsoftrade: Employ new scheme for snippet ofrwlockscale.c
toolsoftrade: Employ new scheme for snippet of compiler barriers
toolsoftrade: Employ new scheme for snippets ofthreadcreate.c
count: Fix indent incount_lim_atomic.cCodeSamples/api-pthreads/api-pthreads.h | 7 +- CodeSamples/count/count_lim_atomic.c | 2 +- CodeSamples/intro/threadcreate.c | 20 +- CodeSamples/toolsoftrade/lock.c | 76 +++--- CodeSamples/toolsoftrade/rwlockscale.c | 54 ++-- count/count.tex | 89 +++--- toolsoftrade/toolsoftrade.tex | 460 ++++++++------------------------ 7 files changed, 253 insertions(+), 455 deletions(-)