crypto/tls: convert Conn.activeCall to atomic type

Change-Id: I5b063070a17bdeed57e73bfb76125b94268b3bc9
Reviewed-on: https://go-review.googlesource.com/c/go/+/426088
Run-TryBot: Michael Pratt <mpratt@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
This commit is contained in:
cuiweixie 2022-08-27 11:49:49 +08:00 committed by Gopher Robot
parent d7df872267
commit ebaa5ff39e
1 changed files with 8 additions and 9 deletions

View File

@ -109,10 +109,9 @@ type Conn struct {
// handshake, nor deliver application data. Protected by in.Mutex. // handshake, nor deliver application data. Protected by in.Mutex.
retryCount int retryCount int
// activeCall is an atomic int32; the low bit is whether Close has // activeCall indicates whether Close has been call in the low bit.
// been called. the rest of the bits are the number of goroutines // the rest of the bits are the number of goroutines in Conn.Write.
// in Conn.Write. activeCall atomic.Int32
activeCall int32
tmp [16]byte tmp [16]byte
} }
@ -1108,15 +1107,15 @@ var (
func (c *Conn) Write(b []byte) (int, error) { func (c *Conn) Write(b []byte) (int, error) {
// interlock with Close below // interlock with Close below
for { for {
x := atomic.LoadInt32(&c.activeCall) x := c.activeCall.Load()
if x&1 != 0 { if x&1 != 0 {
return 0, net.ErrClosed return 0, net.ErrClosed
} }
if atomic.CompareAndSwapInt32(&c.activeCall, x, x+2) { if c.activeCall.CompareAndSwap(x, x+2) {
break break
} }
} }
defer atomic.AddInt32(&c.activeCall, -2) defer c.activeCall.Add(-2)
if err := c.Handshake(); err != nil { if err := c.Handshake(); err != nil {
return 0, err return 0, err
@ -1317,11 +1316,11 @@ func (c *Conn) Close() error {
// Interlock with Conn.Write above. // Interlock with Conn.Write above.
var x int32 var x int32
for { for {
x = atomic.LoadInt32(&c.activeCall) x = c.activeCall.Load()
if x&1 != 0 { if x&1 != 0 {
return net.ErrClosed return net.ErrClosed
} }
if atomic.CompareAndSwapInt32(&c.activeCall, x, x|1) { if c.activeCall.CompareAndSwap(x, x|1) {
break break
} }
} }