This does work on ARM?
Reading your README says that you "need to generate a dmb instruction on ARM, which is impossible in C++11". That sounds strange to me as the C++ language memory model should always just correctly follow the "as if" rule, regardless of the hardware.
Testing this on Godbolt seems to indeed generate quite some dmb ish instructions:
https://godbolt.org/z/93oha5Mn3
So, what's going on with the claim in the README? Are you saying that this seqlock implementation is incorrect with respect to the language memory model, but just happens to work on the x86-TSO memory model?
Hmm, it seems that changing the -mcpu=cortex-m3 to -mcpu=cortex-a76 removes the dmb instructions...
Aha, but! I uses other instructions, such as stlr (Store-Release register) and ldar (Load-Acquire register). So it might still be a correct seqlock in the end?
There's actually a paper from Hans Boehm showing that you cannot express Seqlock in C++11 memory model :(.
I would recommend making your own version that uses assembly to insert to correct memory barriers.
On Fri, Feb 10, 2023 at 5:28 AM Martijn Courteaux @.***> wrote:
Hmm, it seems that changing the -mcpu=cortex-m3 to -mcpu=cortex-a76 removes the dmb instructions...
— Reply to this email directly, view it on GitHub https://github.com/rigtorp/Seqlock/issues/6#issuecomment-1425666654, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABLO22ECHCIHAB3T5HF7ETWWYQ7TANCNFSM6AAAAAAUXVA65Q . You are receiving this because you are subscribed to this thread.Message ID: @.***>
The count.fetch_add(0) recommended by that paper is actually recognized by clang - on x64 it compiles to an mfence followed by a regular load. gcc doesn't do this, however.
Best bet might be count.fetch_add(0) as a default with a manual mfence+load for platforms known to need it (e.g. gcc x64). Then at least you know the ordering will be correct even if it doesn't perform optimally everywhere.
Fascinating. I’d do a couple of lines of inline asm, or use intrinsics and plain C and be sure. I know intrinsics aren’t very portable but that’s one of those cases where correctness and performance prevail over portability. Just my 0.02ct
Yes and unfortunately it's not really possible to implement seqlock in C++11 :(
On Thu, Mar 2, 2023 at 12:52 AM stravager @.***> wrote:
axelriet the whole point of this repo is to portable though. It's supposed to be a generic C++11 seqlock.
— Reply to this email directly, view it on GitHub https://github.com/rigtorp/Seqlock/issues/6#issuecomment-1451382520, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABLO23PDJHGZPSB5KAT663W2A7S7ANCNFSM6AAAAAAUXVA65Q . You are receiving this because you commented.Message ID: @.***>