DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Tyler Retzlaff" <roretzla@linux.microsoft.com>,
	<thomas@monjalon.net>, <bruce.richardson@intel.com>
Cc: dev@dpdk.org, techboard@dpdk.org,
	"Honnappa Nagarahalli" <honnappa.nagarahalli@arm.com>,
	"Ruifeng Wang" <ruifeng.wang@arm.com>,
	"Jerin Jacob" <jerinj@marvell.com>,
	"Sunil Kumar Kori" <skori@marvell.com>,
	"Mattias Rönnblom" <mattias.ronnblom@ericsson.com>,
	"Joyce Kong" <joyce.kong@arm.com>,
	"David Christensen" <drc@linux.vnet.ibm.com>,
	"Konstantin Ananyev" <konstantin.v.ananyev@yandex.ru>,
	"David Hunt" <david.hunt@intel.com>,
	"David Marchand" <david.marchand@redhat.com>
Subject: RE: [PATCH v2 2/6] eal: adapt EAL to present rte optional atomics API
Date: Wed, 16 Aug 2023 22:13:22 +0200	[thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35D87B0F@smartserver.smartshare.dk> (raw)
In-Reply-To: <20230814174706.GB12422@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net>

> From: Tyler Retzlaff [mailto:roretzla@linux.microsoft.com]
> Sent: Monday, 14 August 2023 19.47
> 
> On Mon, Aug 14, 2023 at 10:00:49AM +0200, Morten Brørup wrote:
> > > From: Tyler Retzlaff [mailto:roretzla@linux.microsoft.com]
> > > Sent: Friday, 11 August 2023 19.32
> > >
> > > Adapt the EAL public headers to use rte optional atomics API instead
> of
> > > directly using and exposing toolchain specific atomic builtin
> intrinsics.
> > >
> > > Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
> > > ---
> >
> > [...]
> >
> 
> will fix the comments identified.
> 
> >
> > [...]
> >
> > > --- a/lib/eal/include/generic/rte_spinlock.h
> > > +++ b/lib/eal/include/generic/rte_spinlock.h
> > > @@ -29,7 +29,7 @@
> > >   * The rte_spinlock_t type.
> > >   */
> > >  typedef struct __rte_lockable {
> > > -	volatile int locked; /**< lock status 0 = unlocked, 1 = locked */
> > > +	volatile int __rte_atomic locked; /**< lock status 0 = unlocked, 1
> =
> > > locked */
> >
> > I think __rte_atomic should be before the type:
> > 	volatile __rte_atomic int locked; /**< lock status [...]
> > Alternatively (just mentioning it, I know we don't use this form):
> > 	volatile __rte_atomic(int) locked; /**< lock status [...]
> >
> > Thinking of where you would put "const" might help.

Regarding "const", I use the mental trick of reading from right-to-left when pointers are involved, e.g.:

const int * * const x;
----5---- 4 3 --2-- 1

x(1) is a const(2) pointer(3) to a pointer(4) to a const int(5).

And yes, treating "const int" as one word is cheating... formally it should be "int" "const", i.e. the reverse order; but that is not the convention, so I have learned to accept it.

> >
> > Maybe your order is also correct, so it is a matter of preference.
> 
> so for me what you suggest is the canonical convention for c and i did
> initially try to make the change with this convention but ran into
> trouble when using the keyword in a context used as a type specifier
> and the type was incomplete.
> 
> the rte_mcslock is a good example for illustration.
> 
>   // original struct
>   typedef struct rte_mcslock {
>     struct rte_mcslock *next;
>     ...
>   };
> 
>   it simply doesn't work / won't compile (at least with clang) which is
>   what drove me to use the less-often used syntax.
> 
>   typedef struct rte_mcslock {
>     _Atomic struct rte_mcslock *next;
>     ...
>   };
> 
>   In file included from ../app/test/test_mcslock.c:19:
>   ..\lib\eal\include\rte_mcslock.h:36:2: error: _Atomic cannot be
> applied
>   to incomplete type 'struct rte_mcslock'
> 	  _Atomic struct rte_mcslock *next;
> 	  ^
>   ..\lib\eal\include\rte_mcslock.h:35:16: note: definition of 'struct
>   rte_mcslock' is not complete until the closing '}'
>   typedef struct rte_mcslock {
> 		 ^
>   1 error generated.
> 
> so i ended up choosing to use a single syntax by convention consistently
> rather than using one for the exceptional case and one everywhere else.
> 
> i think (based on our other thread of discussion) i would recommend we
> use adopt and require the use of the _Atomic(T) macro to disambiguate it
> also has the advantage of not being churned later when we can do c++23.
> 
>   // using macro
>   typedef struct rte_mcslock {
>     _Atomic(struct rte_mcslock *) next;

This makes it an atomic pointer. Your example above tried making the struct rts_mcslock atomic. Probably what you wanted was:
  typedef struct rte_mcslock {
    struct rte_mcslock * _Atomic next;
    ...
  };

Like "const", the convention should be putting it before any type, but after the "*" for pointers.

I suppose clang doesn't accept applying _Atomic to incomplete types, regardless where you put it... I.e. this should also fail, I guess:
  typedef struct rte_mcslock {
    struct rte_mcslock _Atomic * next;
    ...
  };

>     ...
>   };
> 
> this is much easier at a glance to know when the specified type is the T
> or the T * similarly in parameter lists it becomes more clear too.
> 
> e.g.
> void foo(int *v)
> 
> that it is either void foo(_Atomic(int) *v) or void foo(_Atomic(int *)
> v) becomes
> much clearer without having to do mental gymnastics.

The same could be said about making "const" clearer:
void foo(const(int) * v) instead of void foo(const int * v), and
void foo(const(int *) v) instead of void foo(int * const v).

Luckily, we don't need toolchain specific handling of "const", so let's just leave that the way it is. :-)

> 
> so i propose we retain
> 
>   #define __rte_atomic _Atomic
> 
>   allow it to be used in contexts where we need a type-qualifier.
>   note:
>     most of the cases where _Atomic is used as a type-qualifier it
>     is a red flag that we are sensitive to an implementation detail
>     of the compiler. in time i hope most of these will go away as we
>     remove deprecated rte_atomic_xx apis.
> 
> but also introduce the following macro
> 
>   #define RTE_ATOMIC(type) _Atomic(type)
>   require it be used in the contexts that we are using it as a type-
> specifier.
> 
> if folks agree with this please reply back positively and i'll update
> the series. feel free to propose alternate names or whatever, but sooner
> than later so i don't have to churn things too much :)

+1 to Tyler's updated proposal, with macro names as suggested.

If anyone disagrees, please speak up soon!

If in doubt, please read https://en.cppreference.com/w/c/language/atomic carefully. It says:
(1) _Atomic(type-name) (since C11): Use as a type specifier; this designates a new atomic type.
(2) _Atomic type-name (since C11): Use as a type qualifier; this designates the atomic version of type-name. In this role, it may be mixed with const, volatile, and restrict, although unlike other qualifiers, the atomic version of type-name may have a different size, alignment, and object representation.

NB: I hadn't noticed this before, otherwise I had probably suggested using _Atomic(T) earlier on. We learn something new every day. :-)

> 
> thanks!

Sorry about the late response, Tyler. Other work prevented me from setting aside coherent time to review your updated proposal.

> 
> >
> > The DPDK coding style guidelines doesn't mention where to place
> "const", but looking at the code, it seems to use "const unsigned int"
> and "const char *".
> 
> we probably should document it as a convention and most likely we should
> adopt what is already in use more commonly.

+1, but not as part of this series. :-)


  reply	other threads:[~2023-08-16 20:13 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-11  1:31 [PATCH 0/6] RFC optional rte optional stdatomics API Tyler Retzlaff
2023-08-11  1:31 ` [PATCH 1/6] eal: provide rte stdatomics optional atomics API Tyler Retzlaff
2023-08-11  8:56   ` Bruce Richardson
2023-08-11  9:42   ` Morten Brørup
2023-08-11 15:54     ` Tyler Retzlaff
2023-08-14  9:04       ` Morten Brørup
2023-08-11  1:31 ` [PATCH 2/6] eal: adapt EAL to present rte " Tyler Retzlaff
2023-08-11  1:31 ` [PATCH 3/6] eal: add rte atomic qualifier with casts Tyler Retzlaff
2023-08-11  1:31 ` [PATCH 4/6] distributor: adapt for EAL optional atomics API changes Tyler Retzlaff
2023-08-11  1:32 ` [PATCH 5/6] bpf: " Tyler Retzlaff
2023-08-11  1:32 ` [PATCH 6/6] devtools: forbid new direct use of GCC atomic builtins Tyler Retzlaff
2023-08-11  8:57   ` Bruce Richardson
2023-08-11  9:51   ` Morten Brørup
2023-08-11 15:56     ` Tyler Retzlaff
2023-08-14  6:37       ` Morten Brørup
2023-08-11 17:32 ` [PATCH v2 0/6] RFC optional rte optional stdatomics API Tyler Retzlaff
2023-08-11 17:32   ` [PATCH v2 1/6] eal: provide rte stdatomics optional atomics API Tyler Retzlaff
2023-08-14  7:06     ` Morten Brørup
2023-08-11 17:32   ` [PATCH v2 2/6] eal: adapt EAL to present rte " Tyler Retzlaff
2023-08-14  8:00     ` Morten Brørup
2023-08-14 17:47       ` Tyler Retzlaff
2023-08-16 20:13         ` Morten Brørup [this message]
2023-08-16 20:32           ` Tyler Retzlaff
2023-08-11 17:32   ` [PATCH v2 3/6] eal: add rte atomic qualifier with casts Tyler Retzlaff
2023-08-14  8:05     ` Morten Brørup
2023-08-11 17:32   ` [PATCH v2 4/6] distributor: adapt for EAL optional atomics API changes Tyler Retzlaff
2023-08-14  8:07     ` Morten Brørup
2023-08-11 17:32   ` [PATCH v2 5/6] bpf: " Tyler Retzlaff
2023-08-14  8:11     ` Morten Brørup
2023-08-11 17:32   ` [PATCH v2 6/6] devtools: forbid new direct use of GCC atomic builtins Tyler Retzlaff
2023-08-14  8:12     ` Morten Brørup
2023-08-16 19:19 ` [PATCH v3 0/6] RFC optional rte optional stdatomics API Tyler Retzlaff
2023-08-16 19:19   ` [PATCH v3 1/6] eal: provide rte stdatomics optional atomics API Tyler Retzlaff
2023-08-16 20:55     ` Morten Brørup
2023-08-16 21:04       ` Tyler Retzlaff
2023-08-16 21:08         ` Morten Brørup
2023-08-16 21:10         ` Tyler Retzlaff
2023-08-16 19:19   ` [PATCH v3 2/6] eal: adapt EAL to present rte " Tyler Retzlaff
2023-08-16 19:19   ` [PATCH v3 3/6] eal: add rte atomic qualifier with casts Tyler Retzlaff
2023-08-16 19:19   ` [PATCH v3 4/6] distributor: adapt for EAL optional atomics API changes Tyler Retzlaff
2023-08-16 19:19   ` [PATCH v3 5/6] bpf: " Tyler Retzlaff
2023-08-16 19:19   ` [PATCH v3 6/6] devtools: forbid new direct use of GCC atomic builtins Tyler Retzlaff
2023-08-16 21:38 ` [PATCH v4 0/6] RFC optional rte optional stdatomics API Tyler Retzlaff
2023-08-16 21:38   ` [PATCH v4 1/6] eal: provide rte stdatomics optional atomics API Tyler Retzlaff
2023-08-17 11:45     ` Morten Brørup
2023-08-17 19:09       ` Tyler Retzlaff
2023-08-18  6:55         ` Morten Brørup
2023-08-16 21:38   ` [PATCH v4 2/6] eal: adapt EAL to present rte " Tyler Retzlaff
2023-08-16 21:38   ` [PATCH v4 3/6] eal: add rte atomic qualifier with casts Tyler Retzlaff
2023-08-16 21:38   ` [PATCH v4 4/6] distributor: adapt for EAL optional atomics API changes Tyler Retzlaff
2023-08-16 21:38   ` [PATCH v4 5/6] bpf: " Tyler Retzlaff
2023-08-16 21:38   ` [PATCH v4 6/6] devtools: forbid new direct use of GCC atomic builtins Tyler Retzlaff
2023-08-17 11:57     ` Morten Brørup
2023-08-17 19:14       ` Tyler Retzlaff
2023-08-18  7:13         ` Morten Brørup
2023-08-22 18:14           ` Tyler Retzlaff
2023-08-17 21:42 ` [PATCH v5 0/6] optional rte optional stdatomics API Tyler Retzlaff
2023-08-17 21:42   ` [PATCH v5 1/6] eal: provide rte stdatomics optional atomics API Tyler Retzlaff
2023-08-17 21:42   ` [PATCH v5 2/6] eal: adapt EAL to present rte " Tyler Retzlaff
2023-08-17 21:42   ` [PATCH v5 3/6] eal: add rte atomic qualifier with casts Tyler Retzlaff
2023-08-17 21:42   ` [PATCH v5 4/6] distributor: adapt for EAL optional atomics API changes Tyler Retzlaff
2023-08-17 21:42   ` [PATCH v5 5/6] bpf: " Tyler Retzlaff
2023-08-17 21:42   ` [PATCH v5 6/6] devtools: forbid new direct use of GCC atomic builtins Tyler Retzlaff
2023-08-21 22:27   ` [PATCH v5 0/6] optional rte optional stdatomics API Konstantin Ananyev
2023-08-22 21:00 ` [PATCH v6 0/6] rte atomics API for optional stdatomic Tyler Retzlaff
2023-08-22 21:00   ` [PATCH v6 1/6] eal: provide rte stdatomics optional atomics API Tyler Retzlaff
2023-09-28  8:06     ` Thomas Monjalon
2023-09-29  8:04       ` David Marchand
2023-09-29  8:54         ` Morten Brørup
2023-09-29  9:02           ` David Marchand
2023-09-29  9:26             ` Bruce Richardson
2023-09-29  9:34               ` David Marchand
2023-09-29 10:26                 ` Thomas Monjalon
2023-09-29 11:38                   ` David Marchand
2023-09-29 11:51                     ` Thomas Monjalon
2023-08-22 21:00   ` [PATCH v6 2/6] eal: adapt EAL to present rte " Tyler Retzlaff
2023-08-22 21:00   ` [PATCH v6 3/6] eal: add rte atomic qualifier with casts Tyler Retzlaff
2023-08-22 21:00   ` [PATCH v6 4/6] distributor: adapt for EAL optional atomics API changes Tyler Retzlaff
2023-08-22 21:00   ` [PATCH v6 5/6] bpf: " Tyler Retzlaff
2023-08-22 21:00   ` [PATCH v6 6/6] devtools: forbid new direct use of GCC atomic builtins Tyler Retzlaff
2023-08-29 15:57   ` [PATCH v6 0/6] rte atomics API for optional stdatomic Tyler Retzlaff
2023-09-29 14:09   ` David Marchand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98CBD80474FA8B44BF855DF32C47DC35D87B0F@smartserver.smartshare.dk \
    --to=mb@smartsharesystems.com \
    --cc=bruce.richardson@intel.com \
    --cc=david.hunt@intel.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=drc@linux.vnet.ibm.com \
    --cc=honnappa.nagarahalli@arm.com \
    --cc=jerinj@marvell.com \
    --cc=joyce.kong@arm.com \
    --cc=konstantin.v.ananyev@yandex.ru \
    --cc=mattias.ronnblom@ericsson.com \
    --cc=roretzla@linux.microsoft.com \
    --cc=ruifeng.wang@arm.com \
    --cc=skori@marvell.com \
    --cc=techboard@dpdk.org \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).