On 1/18/21 9:15 PM, Liu Hao wrote: > 在 2021/1/19 上午3:32, Zebediah Figura (she/her) 写道: >> I guess I ask because it's not clear to me whether it should be >> necessary either. It doesn't seem to be present for x86, but there might >> be code out there that does something like >> >> "while (!var) YieldProcessor();" >> >> and expects it to work. I'm assuming the "dmb ishst" would prevent CPU >> reordering enough for that to work, but it's not clear to me that the >> compiler wouldn't optimize out the loop somehow... > > This requires that the read operation on `var` should have acquire semantics, > which consequently > plays the role as the acquire barrier. `YieldProcessor()` doesn't have to be > a barrier itself.
Do you mean that the writer should change the above code to e.g. "__atomic_load_n(&var, ___ATOMIC_ACQUIRE)", or that the read already implies acquire semantics? I didn't think that the latter was true, but I don't trust that judgement; locking is way too hard... But even if the above code isn't correct, I don't think it's worth potentially breaking it. > > On the other hand, the effect of memory clobber seems a bit unclear to me. > Because inline assembly > can call external functions, all memory that is reachable (save for local > variables that are known > not to escape) will be clobbered. There don't seem to be any differences > between having it and not > having it [1]. I believe it is not necessary in `YieldProcessor()`. > > > [1] https://gcc.godbolt.org/z/8W1K8q > > So here's a fun fact: if I change the statement __asm__ ("nop"); to __asm__ ("nop":); then gcc will *not* reload ival. This is apparently the difference between "basic" and "extended" asm, which I was not previously aware of. I can't find it explicitly stated in gcc's documentation that basic asm always clobbers memory, but apparently it's the case. Of course, in that case, I'd encourage sticking with the explicitly documented behavior and using extended asm. _______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
