diff options
author | Felföldi Zsolt <zsfelfoldi@gmail.com> | 2018-08-06 20:46:30 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-08-06 20:46:30 +0800 |
commit | eef65b20fcb3408dad11ecc79cd5c914993e8ed2 (patch) | |
tree | a12f54601243de7e4bc6d7b18139a811a2e80f5d | |
parent | c4df67461f6e0d35389e0dabd5a932a991e3a42d (diff) | |
download | dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar.gz dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar.zst dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.zip |
p2p: use safe atomic operations when changing connFlags (#17325)
-rw-r--r-- | p2p/server.go | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/p2p/server.go b/p2p/server.go index 669ef740d..8f3a511f3 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -258,13 +258,18 @@ func (c *conn) is(f connFlag) bool { } func (c *conn) set(f connFlag, val bool) { - flags := connFlag(atomic.LoadInt32((*int32)(&c.flags))) - if val { - flags |= f - } else { - flags &= ^f + for { + oldFlags := connFlag(atomic.LoadInt32((*int32)(&c.flags))) + flags := oldFlags + if val { + flags |= f + } else { + flags &= ^f + } + if atomic.CompareAndSwapInt32((*int32)(&c.flags), int32(oldFlags), int32(flags)) { + return + } } - atomic.StoreInt32((*int32)(&c.flags), int32(flags)) } // Peers returns all connected peers. |