From: Matthew Wilcox <willy@infradead.org>
To: linux-ia64@vger.kernel.org
Subject: Re: [PATCH] ia64: Rewrite atomic_add and atomic_sub
Date: Thu, 18 Jan 2018 19:19:04 +0000 [thread overview]
Message-ID: <20180118191904.GC23800@bombadil.infradead.org> (raw)
In-Reply-To: <20180118183953.29257-1-willy@infradead.org>
On Thu, Jan 18, 2018 at 11:02:43AM -0800, Luck, Tony wrote:
> On Thu, Jan 18, 2018 at 10:39:53AM -0800, Matthew Wilcox wrote:
> > + int __i = (i); \
> > + static const int __ia64_atomic_p = __ia64_atomic_const(i); \
> > + __ia64_atomic_p ? ia64_fetch_and_add(__i, &(v)->counter) : \
> > + ia64_atomic_add(__i, v); \
>
> "static"? Is that safe? What if we are executing
> atomic_add on multiple cpus at the same time?
>
> Do all those "static" declarations resolve to separate
> memory locations per call site?
My understanding is that they get optimised away again by the front end
and so they never resolve into an actual memory location. Here's my test
program that demonstrates:
1. Calling it with a complex "i" does indeed result in i only being
evaluated once.
2. It manages to successfully turn each call into the right version.
I compiled it on my laptop with gcc -W -Wall -O2 -c test.c -o test.o
and examined the results of objdump -Sr.
typedef struct { unsigned long counter; } atomic_t;
long ia64_fetch_and_add(long i, unsigned long *v);
long ia64_atomic_add(long i, atomic_t *a);
#define __ia64_atomic_const(i) __builtin_constant_p(i) ? \
((i) = 1 || (i) = 4 || (i) = 8 || (i) = 16 || \
(i) = -1 || (i) = -4 || (i) = -8 || (i) = -16) : 0
#define atomic_add_return(i, v) \
({ \
int __i = (i); \
static const int __ia64_atomic_p = __ia64_atomic_const(i); \
__ia64_atomic_p ? ia64_fetch_and_add(__i, &(v)->counter) : \
ia64_atomic_add(__i, v); \
})
int main(int argc, char **argv)
{
atomic_t a;
atomic_add_return(1, &a);
atomic_add_return(3, &a);
atomic_add_return(argc++, &a);
atomic_add_return(argc++, &a);
atomic_add_return(4, &a);
}
next prev parent reply other threads:[~2018-01-18 19:19 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-18 18:39 [PATCH] ia64: Rewrite atomic_add and atomic_sub Matthew Wilcox
2018-01-18 19:02 ` Luck, Tony
2018-01-18 19:19 ` Matthew Wilcox [this message]
2018-01-18 19:19 ` Jakub Jelinek
2018-01-18 21:52 ` Tony Luck
2018-01-21 3:21 ` Matthew Wilcox
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=20180118191904.GC23800@bombadil.infradead.org \
--to=willy@infradead.org \
--cc=linux-ia64@vger.kernel.org \
/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).