News for hackers. Stuff that matters.
Re:Why volatile is hard to specify and implement by loeg on Sunday April 10, 2016 @10:30PM (#11469290)
Yep, any useful compiler support for fence/lock intrinsics will prevent optimizing out those intrinsics.
Re:Why volatile is hard to specify and implement by rdtsc on Monday April 11, 2016 @12:51AM (#11469708)
Are memory barriers transactional i.e. something like begin barrier(), do some shm writes, end_barrier?

I was declaring data structures in shared mmap-ed memory as volatile so compiler does not optimise it away. How would the compiler know not to optimise away operation between memory fences? A fence to me say something about the guarantee of the order in which operations are seen. The order wasn't the problem, write never making it to the other side was the issue...

Re:Why volatile is hard to specify and implement by Avernar on Monday April 11, 2016 @01:57AM (#11469874)
There are many types of barriers depending on the processor architecture. They're usually used in pairs so for example a read barrier would be used when mutex is acquired and the a write barrier when it's released.

If the compiler is smart about its memory barrier intrinsics it will make sure to reread anything after the read barrier and write everything before the write barrier. This will speed things up by not having to use volatile. Between the barriers it can cache reads and inside registers and optimize out multiple writes like normal.

If the compiler can't do that then you need to use volatile. Volatile was meant for I/O where every read has to hit the bus. So if you're forced to use it then you just copy the volatile variable into a non volatile one for use inside your mutex to get some speed back.

Re:Why volatile is hard to specify and implement by loeg on Monday April 11, 2016 @01:48PM (#11473587)
You do need volatile (or equivalent) to instruct the compiler not to optimize away an access/write. Locked read/write intrinsics will already be annotated such that they don't get optimized.