why no memory barriers on example code ?

Programming, for all ages and all languages.
Post Reply
cds84
Posts: 14
Joined: Thu Jan 13, 2011 7:20 am

why no memory barriers on example code ?

Post by cds84 »

Hi guys... Just a quick question.
Ive noticed the lack of any memory barriers in some of the wiki code snippets.

Have I completely misunderstood out-of-order execution ?

for example, take the following code snipped from the IO-APIC wiki page.

Code: Select all

 void write_ioapic_register(const ptr_t apic_base, const uint8_t offset, const uint32_t val)
 {
     /* tell IOREGSEL where we want to write to */
     *(uint32_t*)(apic_base) = offset;
     /* write the value to IOWIN */
     *(uint32_t*)(apic_base + 0x10) = val;
 }
Don't we need an sfence between the two writes, to make sure the value doesn't get written before the register is selected ?

Surely the out of order execution engine isn't clever enough to see the virtual address writes are mapped to the IOAPIC ?

Thanks.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: why no memory barriers on example code ?

Post by Owen »

No. Intel and AMD both have very good descriptions of the x86 memory ordering model. It can be summed up as, on a single CPU, being that all operations will (appear to) complete before any following operations. In a multicore setup, it is guaranteed that, if you observe the result of an operation on another core, then (from your point of view) all preceding operations on that core will have completed.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: why no memory barriers on example code ?

Post by gerryg400 »

The compiler, on the other hand might re-order things so you need to cast the the register addresses to pointers-to-volatile.
If a trainstation is where trains stop, what is a workstation ?
Post Reply