News for hackers. Stuff that matters.
Re:Hotpatching a C Function on x86 by e12e on Friday April 01, 2016 @07:54AM (#11404169)
Hm, didn't know putc was thread safe, but apparently it generally is (implementation dependent)[1]. Does this mean that using unlocked_stdio[1] is a good idea if you're writing single-threaded programs?

Another question, as I understand it, one must go to c11 before C gains any "standard" thread awareness -- does that mean that the use of a naked int for x in this article, probably should've been a _Atomic(int) x;? [ed: if the article conformed to c11 as opposed to c99, that is]

I suppose it depends where x ends up being stored, if multiple threads across multiple cores will always see/modify the same version of x?

http://linux.die.net/man/3/unlocked_stdio

Re:Hotpatching a C Function on x86 by gjulianm on Friday April 01, 2016 @08:15AM (#11404241)
There are differences between atomicity and thread synchronization. An atomic write means that the variable is always in a correct state. For example, a non-atomic write could write half of the integer in one instruction and the other half in other instruction, so a thread could see that intermediate, incoherent state (actually, a write of a integer is always atomic in 32/64 bits, but this is just an example).

But that the write is atomic does not mean that it is synchronized with other cores. For example, thread A in core 1 could write the value of x, but later thread B in core 2 could read an outdated from its L1 cache. You should use memory barriers that force cache refreshing, so all the threads always see the same version of the variable and do not rely on possibly outdated cachés.

About the unlocked stdio, I don't think there would be a clear advantage. For best performance, I/O is done in blocks as big as possible, so locking mechanisms should not matter that much (you spend much more time in the actual I/O than in the locking). It should only affect significantly when doing lots of I/O calls, but even in that case, removing the locks would not improve performance as much as grouping and batching those calls.

Re:Hotpatching a C Function on x86 by ArkyBeagle on Friday April 01, 2016 @10:19AM (#11404919)
FTA: "In this post I’m going to do a silly, but interesting, exercise that should never be done in any program that actually matters."