On Thu, 25 Aug 2005 13:44:55 -0700 (PDT)
Vadim Lobanov <[email protected]> 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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[Index of Archives]
[Kernel Newbies]
[Netfilter]
[Bugtraq]
[Photo]
[Gimp]
[Yosemite News]
[MIPS Linux]
[ARM Linux]
[Linux Security]
[Linux RAID]
[Video 4 Linux]
[Linux for the blind]
|
|