aboutsummaryrefslogtreecommitdiffstats
path: root/p2p
diff options
context:
space:
mode:
authorFelföldi Zsolt <zsfelfoldi@gmail.com>2018-08-06 20:46:30 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-08-06 20:46:30 +0800
commiteef65b20fcb3408dad11ecc79cd5c914993e8ed2 (patch)
treea12f54601243de7e4bc6d7b18139a811a2e80f5d /p2p
parentc4df67461f6e0d35389e0dabd5a932a991e3a42d (diff)
downloaddexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar
dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar.gz
dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar.bz2
dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar.lz
dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar.xz
dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.tar.zst
dexon-eef65b20fcb3408dad11ecc79cd5c914993e8ed2.zip
p2p: use safe atomic operations when changing connFlags (#17325)
Diffstat (limited to 'p2p')
-rw-r--r--p2p/server.go17
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.