All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] count: Switch from GCC to C11 thread-local storage
@ 2022-08-17 11:00 Elad Lahav
  2022-08-17 15:18 ` Akira Yokosawa
  0 siblings, 1 reply; 6+ messages in thread
From: Elad Lahav @ 2022-08-17 11:00 UTC (permalink / raw)
  To: perfbook; +Cc: Elad Lahav

Signed-off-by: Elad Lahav <e2lahav@gmail.com>
---
 CodeSamples/count/count_end.c | 12 ++++++------
 count/count.tex               | 20 ++++++++++++--------
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/CodeSamples/count/count_end.c b/CodeSamples/count/count_end.c
index 722ad2f7..5e7b9ee1 100644
--- a/CodeSamples/count/count_end.c
+++ b/CodeSamples/count/count_end.c
@@ -1,6 +1,6 @@
 /*
  * count_end.c: Per-thread statistical counters that provide sum at end.
- *	Uses __thread for each thread's counter.
+ *	Uses _Thread_local for each thread's counter.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,17 +23,17 @@
 #include "../api.h"
 
 //\begin{snippet}[labelbase=ln:count:count_end:whole,commandchars=\\\@\$]
-unsigned long __thread counter = 0;		//\lnlbl{var:b}
+unsigned long _Thread_local counter = 0;		//\lnlbl{var:b}
 unsigned long *counterp[NR_THREADS] = { NULL };
 unsigned long finalcount = 0;
 DEFINE_SPINLOCK(final_mutex);			//\lnlbl{var:e}
 
-static __inline__ void inc_count(void)		//\lnlbl{inc:b}
+static inline void inc_count(void)		//\lnlbl{inc:b}
 {
 	WRITE_ONCE(counter, counter + 1);
 }						//\lnlbl{inc:e}
 
-static __inline__ unsigned long read_count(void)
+static inline unsigned long read_count(void)
 {
 	int t;
 	unsigned long sum;
@@ -48,7 +48,7 @@ static __inline__ unsigned long read_count(void)
 }
 
 #ifndef FCV_SNIPPET
-__inline__ void count_init(void)
+inline void count_init(void)
 {
 }
 #endif /* FCV_SNIPPET */
@@ -73,7 +73,7 @@ void count_unregister_thread(int nthreadsexpected)	//\lnlbl{unreg:b}
 }							//\lnlbl{unreg:e}
 //\end{snippet}
 
-__inline__ void count_cleanup(void)
+inline void count_cleanup(void)
 {
 }
 
diff --git a/count/count.tex b/count/count.tex
index 523789e2..775cf77e 100644
--- a/count/count.tex
+++ b/count/count.tex
@@ -490,7 +490,7 @@ thread (presumably cache aligned and padded to avoid false sharing).
 	It can, and in this toy implementation, it does.
 	But it is not that hard to come up with an alternative
 	implementation that permits an arbitrary number of threads,
-	for example, using \GCC's \co{__thread} facility,
+	for example, using C11's \co{_Thread_local} facility,
 	as shown in
 	\cref{sec:count:Per-Thread-Variable-Based Implementation}.
 }\QuickQuizEnd
@@ -721,8 +721,12 @@ This is the topic of the next section.
 \subsection{Per-Thread-Variable-Based Implementation}
 \label{sec:count:Per-Thread-Variable-Based Implementation}
 
-\GCC\ provides an \apig{__thread} storage class that provides
-per-thread storage.
+The C language, since C11, features a \apig{_Thread_local} storage class that
+provides per-thread storage.
+\footnote{\GCC\ provides its own \apig{__thread} storage class, which was used
+in previous versions of this book.
+The two methods for specifying a thread-local variable are interchangeable
+when using \GCC\@.}
 This can be used as shown in
 \cref{lst:count:Per-Thread Statistical Counters} (\path{count_end.c})
 to implement
@@ -749,14 +753,14 @@ value of the counter and exiting threads.
 	Doesn't that explicit \co{counterp} array in
 	\cref{lst:count:Per-Thread Statistical Counters}
 	reimpose an arbitrary limit on the number of threads?
-	Why doesn't \GCC\ provide a \co{per_thread()} interface, similar
+	Why doesn't the C language provide a \co{per_thread()} interface, similar
 	to the Linux kernel's \co{per_cpu()} primitive, to allow
 	threads to more easily access each others' per-thread variables?
 }\QuickQuizAnswer{
 	Why indeed?
 
-	To be fair, \GCC\ faces some challenges that the Linux kernel
-	gets to ignore.
+	To be fair, user-mode thread-local storage faces some challenges
+	that the Linux kernel gets to ignore.
 	When a user-level thread exits, its per-thread variables all
 	disappear, which complicates the problem of per-thread-variable
 	access, particularly before the advent of user-level RCU
@@ -940,7 +944,7 @@ variables vanish when that thread exits.
 	more graceful manner.
 }\QuickQuizEnd
 
-Both the array-based and \co{__thread}-based approaches offer excellent
+Both the array-based and \co{_Thread_local}-based approaches offer excellent
 update-side performance and scalability.
 However, these benefits result in large read-side expense for large
 numbers of threads.
@@ -2961,7 +2965,7 @@ courtesy of eventual consistency.
 	work when there are more threads.
 	In addition, the last two algorithms interpose an additional
 	level of indirection because they map from integer thread ID
-	to the corresponding \co{__thread} variable.
+	to the corresponding \co{_Thread_local} variable.
 }\QuickQuizEndB
 %
 \QuickQuizE{
-- 
2.25.1


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

* Re: [PATCH v2] count: Switch from GCC to C11 thread-local storage
  2022-08-17 11:00 [PATCH v2] count: Switch from GCC to C11 thread-local storage Elad Lahav
@ 2022-08-17 15:18 ` Akira Yokosawa
  2022-08-17 17:36   ` Elad Lahav
  0 siblings, 1 reply; 6+ messages in thread
From: Akira Yokosawa @ 2022-08-17 15:18 UTC (permalink / raw)
  To: Elad Lahav; +Cc: Paul E. McKenney, perfbook, Akira Yokosawa

Hi Elad,

On Wed, 17 Aug 2022 07:00:50 -0400, Elad Lahav wrote:
> Signed-off-by: Elad Lahav <e2lahav@gmail.com>
> ---

As there is no changelog here, let me ask a (maybe stupid) question.

What is your goal of diverting from GCC extensions and switching
to the C11 standard?

Do you want the codebase under CodeSamples/ to be strictly
conformant to C11 or later?

Runnig "make" under CodeSamples/count, with "-std=c11" appended
to GCC_ARGS, I get a lot of compile errors/warnings (with GCC 9.4.0
under Ubuntu 20.04), beginning with:

    cc -g -O2 -Wall -std=c11  -o count_atomic count_atomic.c -lpthread

    In file included from /usr/include/sched.h:34,

                     from /usr/include/pthread.h:22,

                     from ../api.h:159,

                     from count_atomic.c:22:

    /usr/include/time.h:113:5: error: unknown type name 'locale_t'

      113 |     locale_t __loc) __THROW;

          |     ^~~~~~~~


A workaround is to append "-D_GNU_SOURCE" to GCC_ARGS.  With that,
I get the next warning of:

    ../api.h:766:2: warning: implicit declaration of function 'typeof' [-Wimplicit-function-declaration]

  766 |  typeof(*ptr) _____actual = (o); \


Apparently, typeof() is another GCC extension.  __typeof__() might
be used instead, but it is not ISO C.

So what is you goal of these switches?

        Thanks, Akira

>  CodeSamples/count/count_end.c | 12 ++++++------
>  count/count.tex               | 20 ++++++++++++--------
>  2 files changed, 18 insertions(+), 14 deletions(-)
> 
[...]

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

* Re: [PATCH v2] count: Switch from GCC to C11 thread-local storage
  2022-08-17 15:18 ` Akira Yokosawa
@ 2022-08-17 17:36   ` Elad Lahav
  2022-08-19  2:02     ` Paul E. McKenney
  0 siblings, 1 reply; 6+ messages in thread
From: Elad Lahav @ 2022-08-17 17:36 UTC (permalink / raw)
  To: Akira Yokosawa; +Cc: Paul E. McKenney, perfbook

[-- Attachment #1: Type: text/plain, Size: 2084 bytes --]

Hi Akira,

The reason for the change is that using C11 is more portable. I have no
problem dropping it if it causes any problems.

That said, there is a simple solution to the issue you are facing. The
-std=c11 option specifies strict adherence to ISO C, without extensions.
You rarely want that. Instead, specify -std=gnu11, which will give you C11
plus POSIX plus GNU extensions.

--Elad

On Wed., Aug. 17, 2022, 11:18 a.m. Akira Yokosawa, <akiyks@gmail.com> wrote:

> Hi Elad,
>
> On Wed, 17 Aug 2022 07:00:50 -0400, Elad Lahav wrote:
> > Signed-off-by: Elad Lahav <e2lahav@gmail.com>
> > ---
>
> As there is no changelog here, let me ask a (maybe stupid) question.
>
> What is your goal of diverting from GCC extensions and switching
> to the C11 standard?
>
> Do you want the codebase under CodeSamples/ to be strictly
> conformant to C11 or later?
>
> Runnig "make" under CodeSamples/count, with "-std=c11" appended
> to GCC_ARGS, I get a lot of compile errors/warnings (with GCC 9.4.0
> under Ubuntu 20.04), beginning with:
>
>     cc -g -O2 -Wall -std=c11  -o count_atomic count_atomic.c -lpthread
>
>     In file included from /usr/include/sched.h:34,
>
>                      from /usr/include/pthread.h:22,
>
>                      from ../api.h:159,
>
>                      from count_atomic.c:22:
>
>     /usr/include/time.h:113:5: error: unknown type name 'locale_t'
>
>       113 |     locale_t __loc) __THROW;
>
>           |     ^~~~~~~~
>
>
> A workaround is to append "-D_GNU_SOURCE" to GCC_ARGS.  With that,
> I get the next warning of:
>
>     ../api.h:766:2: warning: implicit declaration of function 'typeof'
> [-Wimplicit-function-declaration]
>
>   766 |  typeof(*ptr) _____actual = (o); \
>
>
> Apparently, typeof() is another GCC extension.  __typeof__() might
> be used instead, but it is not ISO C.
>
> So what is you goal of these switches?
>
>         Thanks, Akira
>
> >  CodeSamples/count/count_end.c | 12 ++++++------
> >  count/count.tex               | 20 ++++++++++++--------
> >  2 files changed, 18 insertions(+), 14 deletions(-)
> >
> [...]
>

[-- Attachment #2: Type: text/html, Size: 2881 bytes --]

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

* Re: [PATCH v2] count: Switch from GCC to C11 thread-local storage
  2022-08-17 17:36   ` Elad Lahav
@ 2022-08-19  2:02     ` Paul E. McKenney
  2022-08-21 17:57       ` Elad Lahav
  0 siblings, 1 reply; 6+ messages in thread
From: Paul E. McKenney @ 2022-08-19  2:02 UTC (permalink / raw)
  To: Elad Lahav; +Cc: Akira Yokosawa, perfbook

Incrementalist that I am, my thought is to take this one thing at a
time, if and when.  ;-0

							Thanx, Paul

On Wed, Aug 17, 2022 at 01:36:43PM -0400, Elad Lahav wrote:
> Hi Akira,
> 
> The reason for the change is that using C11 is more portable. I have no
> problem dropping it if it causes any problems.
> 
> That said, there is a simple solution to the issue you are facing. The
> -std=c11 option specifies strict adherence to ISO C, without extensions.
> You rarely want that. Instead, specify -std=gnu11, which will give you C11
> plus POSIX plus GNU extensions.
> 
> --Elad
> 
> On Wed., Aug. 17, 2022, 11:18 a.m. Akira Yokosawa, <akiyks@gmail.com> wrote:
> 
> > Hi Elad,
> >
> > On Wed, 17 Aug 2022 07:00:50 -0400, Elad Lahav wrote:
> > > Signed-off-by: Elad Lahav <e2lahav@gmail.com>
> > > ---
> >
> > As there is no changelog here, let me ask a (maybe stupid) question.
> >
> > What is your goal of diverting from GCC extensions and switching
> > to the C11 standard?
> >
> > Do you want the codebase under CodeSamples/ to be strictly
> > conformant to C11 or later?
> >
> > Runnig "make" under CodeSamples/count, with "-std=c11" appended
> > to GCC_ARGS, I get a lot of compile errors/warnings (with GCC 9.4.0
> > under Ubuntu 20.04), beginning with:
> >
> >     cc -g -O2 -Wall -std=c11  -o count_atomic count_atomic.c -lpthread
> >
> >     In file included from /usr/include/sched.h:34,
> >
> >                      from /usr/include/pthread.h:22,
> >
> >                      from ../api.h:159,
> >
> >                      from count_atomic.c:22:
> >
> >     /usr/include/time.h:113:5: error: unknown type name 'locale_t'
> >
> >       113 |     locale_t __loc) __THROW;
> >
> >           |     ^~~~~~~~
> >
> >
> > A workaround is to append "-D_GNU_SOURCE" to GCC_ARGS.  With that,
> > I get the next warning of:
> >
> >     ../api.h:766:2: warning: implicit declaration of function 'typeof'
> > [-Wimplicit-function-declaration]
> >
> >   766 |  typeof(*ptr) _____actual = (o); \
> >
> >
> > Apparently, typeof() is another GCC extension.  __typeof__() might
> > be used instead, but it is not ISO C.
> >
> > So what is you goal of these switches?
> >
> >         Thanks, Akira
> >
> > >  CodeSamples/count/count_end.c | 12 ++++++------
> > >  count/count.tex               | 20 ++++++++++++--------
> > >  2 files changed, 18 insertions(+), 14 deletions(-)
> > >
> > [...]
> >

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

* Re: [PATCH v2] count: Switch from GCC to C11 thread-local storage
  2022-08-19  2:02     ` Paul E. McKenney
@ 2022-08-21 17:57       ` Elad Lahav
  2022-08-21 22:59         ` Paul E. McKenney
  0 siblings, 1 reply; 6+ messages in thread
From: Elad Lahav @ 2022-08-21 17:57 UTC (permalink / raw)
  To: paulmck; +Cc: Akira Yokosawa, perfbook

On 2022-08-18 22:02, Paul E. McKenney wrote:
> Incrementalist that I am, my thought is to take this one thing at a
> time, if and when.  ;-0

What would you like to do? I have no objection to dropping this change, 
but I think (at least in principle) that using standard language 
features is better than using compiler-specific extensions. If nothing 
else it makes the MISRA folks happy ;-)

--Elad

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

* Re: [PATCH v2] count: Switch from GCC to C11 thread-local storage
  2022-08-21 17:57       ` Elad Lahav
@ 2022-08-21 22:59         ` Paul E. McKenney
  0 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2022-08-21 22:59 UTC (permalink / raw)
  To: Elad Lahav; +Cc: Akira Yokosawa, perfbook

On Sun, Aug 21, 2022 at 01:57:42PM -0400, Elad Lahav wrote:
> On 2022-08-18 22:02, Paul E. McKenney wrote:
> > Incrementalist that I am, my thought is to take this one thing at a
> > time, if and when.  ;-0
> 
> What would you like to do? I have no objection to dropping this change, but
> I think (at least in principle) that using standard language features is
> better than using compiler-specific extensions. If nothing else it makes the
> MISRA folks happy ;-)

;-)

Applied, thank you both!

							Thanx, Paul

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

end of thread, other threads:[~2022-08-21 22:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-17 11:00 [PATCH v2] count: Switch from GCC to C11 thread-local storage Elad Lahav
2022-08-17 15:18 ` Akira Yokosawa
2022-08-17 17:36   ` Elad Lahav
2022-08-19  2:02     ` Paul E. McKenney
2022-08-21 17:57       ` Elad Lahav
2022-08-21 22:59         ` Paul E. McKenney

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.