How to operate atomic.SwapPointer for interface?

1,217 views
Skip to first unread message

Shuhei Tanuma

unread,
Sep 28, 2014, 6:28:02 AM9/28/14
to golan...@googlegroups.com
Hi, I'm writing MQTT server with go. 

MQTT has Session feature that is able to detach, attach existing connection. Currently, I use sync.Mutex to swap existing connection and dummy connection. 
Although, I'd like to swap struct member which type is interface. 
As I guess atomic.SwapPointer has several advantages over mutex.Lock (no need to lock every time...)

I simplified my problem on Go Playground: I could do with specific type (A) http://play.golang.org/p/oerL4ldtnl 
Although, I'm not sure how do I operate with interface like this (B) http://play.golang.org/p/Fg9Qu0K4d_

  type Quacker interface {
    Quack()
  }

  type Toy struct {
  c Quacker // how to do atomic.SwapPointer?
  }

I guess interface is not simple pointer like *TinyDuck so I can't swap with (A) idioms. How do I write this?

Thanks,
Shuhei

Tahir

unread,
Sep 28, 2014, 9:03:09 AM9/28/14
to golan...@googlegroups.com
Interesting.
I think it is because an interface is a Dword.
Also, I've been wondering shouldn't you use `Compare and Swap` rather to implement lock-freedom in this case?
(I don't have a response unfortunately :(

Matt Harden

unread,
Sep 28, 2014, 7:55:33 PM9/28/14
to Tahir, golang-nuts
c *Quacker

--
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.
For more options, visit https://groups.google.com/d/optout.

Shuhei Tanuma

unread,
Sep 28, 2014, 10:49:19 PM9/28/14
to Matt Harden, Tahir, golang-nuts
> Tahir
I've knew atomic.CompareSwap API. that's it.

> Matt
Thank you! I can swap the interface.

http://play.golang.org/p/7Q5B7jE-uz

But I realized I should learn Go more deeply. I can't explain what is
interface pointer.

Thanks,
Shuhei
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/2Abz-DnSy8w/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

dja...@gmail.com

unread,
Sep 29, 2014, 2:25:30 AM9/29/14
to golan...@googlegroups.com, matt....@gmail.com, welcometot...@gmail.com
Hi,
one more solution, but In next go release:

dja...@gmail.com

unread,
Sep 29, 2014, 4:10:40 AM9/29/14
to golan...@googlegroups.com, matt....@gmail.com, welcometot...@gmail.com, dja...@gmail.com
sorry, this is not swap, only atomic load and atomic store for interface.

Jan Mercl

unread,
Sep 29, 2014, 4:23:34 AM9/29/14
to Shuhei Tanuma, golang-nuts
On Sun, Sep 28, 2014 at 12:28 PM, Shuhei Tanuma <chob...@gmail.com> wrote:
> Although, I'd like to swap struct member which type is interface.
> As I guess atomic.SwapPointer has several advantages over mutex.Lock (no
> need to lock every time...)

The largest object that can be atomically swapped using a single [bus
lock prefixed] CPU instruction is a machine word (in the first
approximation). Interface is larger than a machine word, so no, you
cannot do that. OTOH a pointer is a machine word so you can
semantically swap anything regardless of its size when you atomically
swap pointers to that things and always access them through
dereferencing that (volatile) pointer. The sync/atomic package
provides all of that.

In case importing package unsafe is a show stopper one can invent
something like a struct with fields [2]MyInterface and an atomically
changed index, for example.

-j

Dave Cheney

unread,
Sep 29, 2014, 7:13:40 AM9/29/14
to golan...@googlegroups.com
Do you have to do this with interfaces ? Your problem would be much simpler to solve if you avoided the duck typing.

Shuhei Tanuma

unread,
Sep 29, 2014, 6:45:04 PM9/29/14
to golang-nuts
> Djadala
Thanks pointing me, It looks useful in another cases.

> Jan
Your explanations is really understandable. Thank you.
I haven't thought that things before my experience as I was a LL person.

> Dave
Probably, I don't have to do that with interface. just current
implementation takes duck typing.

I'll look for another approach like passing message to channel when that case.
Reply all
Reply to author
Forward
0 new messages