Skip to content

Commit

Permalink
p2p: use safe atomic operations when changing connFlags (ethereum#17325)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsfelfoldi authored and karalabe committed Aug 6, 2018
1 parent c4df674 commit eef65b2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit eef65b2

Please sign in to comment.