Skip to content

Commit 81bb2e5

Browse files
committed
use go 1.18 atomic operations
1 parent 66a3060 commit 81bb2e5

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

Diff for: ssh/server_test.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ func TestNewServerConnValidationErrors(t *testing.T) {
7171
if err == nil {
7272
t.Fatal("NewServerConn with invalid public key auth algorithms succeeded")
7373
}
74-
if !c.closed.Load() {
74+
if !c.isClosed() {
7575
t.Fatal("NewServerConn with invalid public key auth algorithms left connection open")
7676
}
77-
if c.used.Load() {
77+
if c.isUsed() {
7878
t.Fatal("NewServerConn with invalid public key auth algorithms used connection")
7979
}
8080

@@ -88,36 +88,44 @@ func TestNewServerConnValidationErrors(t *testing.T) {
8888
if err == nil {
8989
t.Fatal("NewServerConn with unsupported key exchange succeeded")
9090
}
91-
if !c.closed.Load() {
91+
if !c.isClosed() {
9292
t.Fatal("NewServerConn with unsupported key exchange left connection open")
9393
}
94-
if c.used.Load() {
94+
if c.isUsed() {
9595
t.Fatal("NewServerConn with unsupported key exchange used connection")
9696
}
9797
}
9898

9999
type markerConn struct {
100-
closed atomic.Bool
101-
used atomic.Bool
100+
closed uint32
101+
used uint32
102+
}
103+
104+
func (c *markerConn) isClosed() bool {
105+
return atomic.LoadUint32(&c.closed) != 0
106+
}
107+
108+
func (c *markerConn) isUsed() bool {
109+
return atomic.LoadUint32(&c.used) != 0
102110
}
103111

104112
func (c *markerConn) Close() error {
105-
c.closed.Store(true)
113+
atomic.StoreUint32(&c.closed, 1)
106114
return nil
107115
}
108116

109117
func (c *markerConn) Read(b []byte) (n int, err error) {
110-
c.used.Store(true)
111-
if c.closed.Load() {
118+
atomic.StoreUint32(&c.used, 1)
119+
if atomic.LoadUint32(&c.closed) != 0 {
112120
return 0, net.ErrClosed
113121
} else {
114122
return 0, io.EOF
115123
}
116124
}
117125

118126
func (c *markerConn) Write(b []byte) (n int, err error) {
119-
c.used.Store(true)
120-
if c.closed.Load() {
127+
atomic.StoreUint32(&c.used, 1)
128+
if atomic.LoadUint32(&c.closed) != 0 {
121129
return 0, net.ErrClosed
122130
} else {
123131
return 0, io.ErrClosedPipe

0 commit comments

Comments
 (0)