lkml.org 
[lkml]   [2005]   [Aug]   [26]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [OT] volatile keyword
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
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-08-26 09:25    [from the cache]
©2003-2014 Jasper Spaans. Advertise on this site