From: Paolo Ornati <ornati@fastwebnet.it>
To: Vadim Lobanov <vlobanov@speakeasy.net>
Cc: linux-kernel@vger.kernel.org, tom.anderl@gmail.com
Subject: Re: [OT] volatile keyword
Date: Fri, 26 Aug 2005 09:20:08 +0200 [thread overview]
Message-ID: <20050826092008.55553521@localhost> (raw)
In-Reply-To: <Pine.LNX.4.58.0508251335280.4315@shell2.speakeasy.net>
On Thu, 25 Aug 2005 13:44:55 -0700 (PDT)
Vadim Lobanov <vlobanov@speakeasy.net> wrote:
> int main (void) {
> pthread_t other;
>
> data.lock = 1;
> data.value = 1;
> pthread_create(&other, NULL, thread, NULL);
> while ((volatile unsigned long)(data.lock));
> printf("Value is %lu.\n", data.value);
> pthread_join(other, NULL);
>
> return 0;
> }
The "correct" way should be:
while (*(volatile unsigned long*)(&data.lock));
With only: "while ((volatile unsigned long)(data.lock))" GCC isn't
forced to read to memory simply because "data.lock" isn't volatile.
What than you do with "data.lock" value doesn't change anything. IOW
you should get the same assembly code with and without the cast.
SUMMARY
"(volatile unsigned long)(data.lock)" means:
- take the value of "data.lock" (that isn't volatile so can be
cached)
- cast it to "volatile" (a no-op, since we already HAVE the
value)
"*(volatile unsigned long*)(&data.lock)":
- take the address of "data.lock"
- cast it to "volatile"
- read from _memory_ the value of data.lock (through the
volatile pointer)
Other ways can be:
- use read memory barrier:
while (data.lock)
rmb();
- use everything that implies a memory barrier (eg: locking...)
PS: everything I've said is rigorously NOT tested. :-)
--
Paolo Ornati
Linux 2.6.13-rc7 on x86_64
prev parent reply other threads:[~2005-08-26 7:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-08-25 20:44 [OT] volatile keyword Vadim Lobanov
2005-08-25 20:57 ` Christopher Friesen
2005-08-25 21:16 ` Vadim Lobanov
2005-08-25 21:26 ` Christopher Friesen
2005-08-26 15:39 ` Alan Cox
2005-08-26 7:20 ` Paolo Ornati [this message]
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=20050826092008.55553521@localhost \
--to=ornati@fastwebnet.it \
--cc=linux-kernel@vger.kernel.org \
--cc=tom.anderl@gmail.com \
--cc=vlobanov@speakeasy.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