From: Vadim Lobanov <vlobanov@speakeasy.net>
To: linux-kernel@vger.kernel.org
Cc: tom.anderl@gmail.com
Subject: [OT] volatile keyword
Date: Thu, 25 Aug 2005 13:44:55 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.58.0508251335280.4315@shell2.speakeasy.net> (raw)
Hi,
The recent discussion on the list concerning memory barriers and write
ordering took a side-trip to the volatile keyword, especially its
correct / incorrect usage. Someone posted a link to the LKML archives,
in which the argument is made that it is best to keep 'volatile' _out_
of variable and structure definitions, and _into_ the code that uses
them. I was curious, so I decided to try this out (looking at
kernel/posix-timers.c for inspiration)...
Here's sample userland program number one, written one way:
===========================
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
struct sync {
volatile unsigned long lock;
volatile unsigned long value;
};
struct sync data;
void * thread (void * arg) {
sleep(5);
data.value = 0;
data.lock = 0;
return NULL;
}
int main (void) {
pthread_t other;
data.lock = 1;
data.value = 1;
pthread_create(&other, NULL, thread, NULL);
while (data.lock);
printf("Value is %lu.\n", data.value);
pthread_join(other, NULL);
return 0;
}
===========================
And here's what should be the same program, written the "suggested" way:
===========================
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
struct sync {
unsigned long lock;
unsigned long value;
};
struct sync data;
void * thread (void * arg) {
sleep(5);
data.value = 0;
data.lock = 0;
return NULL;
}
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 first program works exactly as expected. The second program,
however, never syncs with the sleeping thread. In fact, for the second
program, gcc compiles the while loop down to an infinite loop.
I'm positive I'm doing something wrong here. In fact, I bet it's the
volatile cast within the loop that's wrong; but I'm not sure how to do
it correctly. Any help / pointers / discussion would be appreciated.
Thanks. :-)
-VadimL
next reply other threads:[~2005-08-25 20:44 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-08-25 20:44 Vadim Lobanov [this message]
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
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=Pine.LNX.4.58.0508251335280.4315@shell2.speakeasy.net \
--to=vlobanov@speakeasy.net \
--cc=linux-kernel@vger.kernel.org \
--cc=tom.anderl@gmail.com \
/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
all inboxes | Powered by JetHome®