how atomic instruction work?

232 views
Skip to first unread message

xie cui

unread,
Sep 17, 2021, 9:25:23 AM9/17/21
to golang-nuts
how atomic insturction work in golang at x86/amd64,
I think the atomic insturtion will flush the store buffer and invalid queue of current cpu core, 
but I am  not sure, does someone know about it?

Ian Lance Taylor

unread,
Sep 17, 2021, 3:17:26 PM9/17/21
to xie cui, golang-nuts
I assume that you are asking about the sync/atomic package. The
functions in that package will ensure sequential consistency of atomic
operationns, but they will not flush the store buffer. See
https://research.swtch.com/gomm .

Ian

xie cui

unread,
Sep 21, 2021, 3:54:29 PM9/21/21
to golang-nuts

https://stackoverflow.com/questions/2599238/are-memory-barriers-necessary-for-atomic-reference-counting-shared-immutable-dat
this answer say that:
On x86, it will turn into a lock prefixed assembly instruction, like LOCK XADD.
Being a single instruction, it is non-interruptible. As an added "feature", the lock prefix results in a full memory barrier.

In my opinion, full memory barrier means flush store buffer and invalid queue(not very sure, is this right?)

Ian Lance Taylor

unread,
Sep 21, 2021, 5:18:46 PM9/21/21
to xie cui, golang-nuts
On Tue, Sep 21, 2021 at 12:54 PM xie cui <cuiw...@gmail.com> wrote:
>
>
> https://stackoverflow.com/questions/2599238/are-memory-barriers-necessary-for-atomic-reference-counting-shared-immutable-dat
> this answer say that:
> On x86, it will turn into a lock prefixed assembly instruction, like LOCK XADD.
> Being a single instruction, it is non-interruptible. As an added "feature", the lock prefix results in a full memory barrier.
>
> In my opinion, full memory barrier means flush store buffer and invalid queue(not very sure, is this right?)

Can you be specific about exactly what you are asking? You originally
said "atomic instruction," and my answer was based on the functions
sync/atomic.LoadInt32 and sync/atomic.StoreInt32. Here you seem to be
talking about sync/atomic.AddInt32, which is fine, but let's agree on
what the question is. This is a Go language mailing list, so can you
ask your question about a Go function, rather than about an "atomic
instruction"? Thanks.

Ian


> On Saturday, September 18, 2021 at 3:17:26 AM UTC+8 Ian Lance Taylor wrote:
>>
>> On Fri, Sep 17, 2021 at 6:25 AM xie cui <cuiw...@gmail.com> wrote:
>> >
>> > how atomic insturction work in golang at x86/amd64,
>> > I think the atomic insturtion will flush the store buffer and invalid queue of current cpu core,
>> > but I am not sure, does someone know about it?
>>
>> I assume that you are asking about the sync/atomic package. The
>> functions in that package will ensure sequential consistency of atomic
>> operationns, but they will not flush the store buffer. See
>> https://research.swtch.com/gomm .
>>
>> Ian
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/aa7487d1-13f1-451b-86a3-f7f70a7c040cn%40googlegroups.com.

robert engels

unread,
Sep 21, 2021, 5:23:14 PM9/21/21
to Ian Lance Taylor, xie cui, golang-nuts

Dan Kortschak

unread,
Sep 21, 2021, 6:09:23 PM9/21/21
to golan...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages