On Wed, Aug 1, 2018 at 10:29 AM, Scott Cotton <
w...@iri-labs.com> wrote:
> Apologies, a delay in my mail client response to my input something made me
> press send prematurely.
>
> Let me repeat the partial message and finish it correctly here.
>
>> More or less, but in C11 stdatomic terms atomic.CompareAndSwap is like
>> atomic_compare_exchange_weak_explicit(p, &old, new,
>> memory_order_acq_rel, memory_order_relaxed). It doesn't have the
>> guarantees of atomic_compare_exchange_strong and it doesn't guarantee
>> sequential consistency even when it succeeds. Care is required for
>> portable code as on x86 there is no difference between
>> compare_exchange_weak and compare_exchange_strong, and
>> compare/exchange always gives you sequential consistency, but these
>> statements are not true on some non-x86 platforms.
>
>
> Thanks for your clarification.
>
> From the gcc docs I understand, on success, I do get sequential consitency
> guarantee using
> """
> atomic_compare_exchange_weak_explicit(p, &old, new,
> memory_order_acq_rel, memory_order_relaxed)
> """
> but only if at most "another" thread is involved, no sequential guarantees
> if more than one other thread is doing the atomic CAS on the same address.
>
> (the docs read
> """
> __ATOMIC_ACQ_RELFull barrier in both directions and synchronizes with
> acquire loads and release stores in another thread.
> __ATOMIC_SEQ_CSTFull barrier in both directions and synchronizes with
> acquire loads and release stores in all threads."""
>
> So now here's a question: Suppose that there is exactly one goroutine
> calling atomic.CompareAndSwap
> and exactly one C thread calling atomic_cas(...) on the same address.
>
> Can I assume sequential consistency (acquire and release) between the
> goroutine and the thread? Or is the fact that the go scheduler may put the
> goroutine on different threads over time going to pose a risk, thus (cringe)
> inviting a fix with runtime.LockOSThread()?
I don't think the fact that goroutines can change threads is going to
matter here. When a goroutine changes threads the new thread by
definition sees all the memory writes done by the old thread.
acquire-release is not the same as sequential consistency but I think
in this case it doesn't matter.
Ian
>> __ATOMIC_ACQ_RELFull barrier in both directions and synchronizes with
>> acquire loads and release stores in another thread.
>> __ATOMIC_SEQ_CSTFull barrier in both directions and synchronizes with