Re:Go channels are bad
by chrisseaton on Wednesday March 02, 2016 @11:47AM
(#11211088)
I wonder if there are any compilers which can replace
With a 'lock xaddl x 1' instruction.
[ Parent
| on HN ]
Re:Go channels are bad
by mike_hearn on Wednesday March 02, 2016 @01:05PM
(#11211703)
It's not quite the same thing but recent JVMs can translate synchronised blocks into Intel TSX transactions, which means multiple threads can run inside the lock at once, with rollback and retry if interference is detected at the hardware (cache line) level. So yeah .... almost. But it's fancy and cutting edge stuff.
It's conceivable, if you made mutexes compiler/language intrinsic, but as long as you're calling pthread_mutex_lock, the compiler has to assume that that pthread library, which is linked dynamically, is interchangeable, and can do anything it likes to memory. That includes mutating x
That hasn't inhibited optimizations for a long time. Disassemble a call to printf("Hello world") in optimized clang output and look at what it turns into.
Re:Go channels are bad
by chrisseaton on Wednesday March 02, 2016 @05:02PM
(#11213389)
Yes if the library is covered by a standard just like the language is, then the compiler can make assumptions. Also threads are a language feature in C and C++ now.
Java and .NET will do it if you make use of InterlockedIncrement APIs.
Re:Go channels are bad
by chrisseaton on Wednesday March 02, 2016 @01:01PM
(#11211672)
But I think InterlockedIncrement is just 'lock xaddl x 1', so using InterlockedIncrement would be to do it manually.
I'm asking if any compiler can take a statement which uses a high level, general purpose lock and increments a variable inside it using conventional language expressions, and convert it to use 'lock xaddl x 1' (perhaps via InterlockedIncrement or whatever other intrinsics you have) instead.
I only know Java well, not .NET, but I'm pretty sure no Java compiler does it.
Ah, I missed the point.
There ist std::atomic in recent C++ flavors
Re:Go channels are bad
by chrisseaton on Wednesday March 02, 2016 @04:55PM
(#11213355)
Right. My question is whether we can translate locks which only reference a single variable, to use something like std::atomic, automatically.