aboutsummaryrefslogtreecommitdiff
path: root/ipc
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@tailscale.com>2022-08-30 07:43:11 -0700
committerJason A. Donenfeld <Jason@zx2c4.com>2022-09-04 12:57:30 +0200
commitb51010ba13f0a3e59808fbdb1566cd2c6b834b95 (patch)
tree37a6753e5604c13f7141bd5613b0ab80e7b794f7 /ipc
parentd1d08426b27b57990b1ee6782794f56d2c002aa3 (diff)
downloadwireguard-go-b51010ba13f0a3e59808fbdb1566cd2c6b834b95.tar.gz
wireguard-go-b51010ba13f0a3e59808fbdb1566cd2c6b834b95.zip
all: use Go 1.19 and its atomic types
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ipc')
-rw-r--r--ipc/namedpipe/file.go20
-rw-r--r--ipc/namedpipe/namedpipe.go10
2 files changed, 15 insertions, 15 deletions
diff --git a/ipc/namedpipe/file.go b/ipc/namedpipe/file.go
index c5dd48a..ec9b8d4 100644
--- a/ipc/namedpipe/file.go
+++ b/ipc/namedpipe/file.go
@@ -54,7 +54,7 @@ type file struct {
handle windows.Handle
wg sync.WaitGroup
wgLock sync.RWMutex
- closing uint32 // used as atomic boolean
+ closing atomic.Bool
socket bool
readDeadline deadlineHandler
writeDeadline deadlineHandler
@@ -65,7 +65,7 @@ type deadlineHandler struct {
channel timeoutChan
channelLock sync.RWMutex
timer *time.Timer
- timedout uint32 // used as atomic boolean
+ timedout atomic.Bool
}
// makeFile makes a new file from an existing file handle
@@ -89,7 +89,7 @@ func makeFile(h windows.Handle) (*file, error) {
func (f *file) closeHandle() {
f.wgLock.Lock()
// Atomically set that we are closing, releasing the resources only once.
- if atomic.SwapUint32(&f.closing, 1) == 0 {
+ if f.closing.Swap(true) == false {
f.wgLock.Unlock()
// cancel all IO and wait for it to complete
windows.CancelIoEx(f.handle, nil)
@@ -112,7 +112,7 @@ func (f *file) Close() error {
// The caller must call f.wg.Done() when the IO is finished, prior to Close() returning.
func (f *file) prepareIo() (*ioOperation, error) {
f.wgLock.RLock()
- if atomic.LoadUint32(&f.closing) == 1 {
+ if f.closing.Load() {
f.wgLock.RUnlock()
return nil, os.ErrClosed
}
@@ -144,7 +144,7 @@ func (f *file) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, err err
return int(bytes), err
}
- if atomic.LoadUint32(&f.closing) == 1 {
+ if f.closing.Load() {
windows.CancelIoEx(f.handle, &c.o)
}
@@ -160,7 +160,7 @@ func (f *file) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, err err
case r = <-c.ch:
err = r.err
if err == windows.ERROR_OPERATION_ABORTED {
- if atomic.LoadUint32(&f.closing) == 1 {
+ if f.closing.Load() {
err = os.ErrClosed
}
} else if err != nil && f.socket {
@@ -192,7 +192,7 @@ func (f *file) Read(b []byte) (int, error) {
}
defer f.wg.Done()
- if atomic.LoadUint32(&f.readDeadline.timedout) == 1 {
+ if f.readDeadline.timedout.Load() {
return 0, os.ErrDeadlineExceeded
}
@@ -219,7 +219,7 @@ func (f *file) Write(b []byte) (int, error) {
}
defer f.wg.Done()
- if atomic.LoadUint32(&f.writeDeadline.timedout) == 1 {
+ if f.writeDeadline.timedout.Load() {
return 0, os.ErrDeadlineExceeded
}
@@ -256,7 +256,7 @@ func (d *deadlineHandler) set(deadline time.Time) error {
}
d.timer = nil
}
- atomic.StoreUint32(&d.timedout, 0)
+ d.timedout.Store(false)
select {
case <-d.channel:
@@ -271,7 +271,7 @@ func (d *deadlineHandler) set(deadline time.Time) error {
}
timeoutIO := func() {
- atomic.StoreUint32(&d.timedout, 1)
+ d.timedout.Store(true)
close(d.channel)
}
diff --git a/ipc/namedpipe/namedpipe.go b/ipc/namedpipe/namedpipe.go
index 6db5ea3..92cc1ee 100644
--- a/ipc/namedpipe/namedpipe.go
+++ b/ipc/namedpipe/namedpipe.go
@@ -29,7 +29,7 @@ type pipe struct {
type messageBytePipe struct {
pipe
- writeClosed int32
+ writeClosed atomic.Bool
readEOF bool
}
@@ -51,17 +51,17 @@ func (f *pipe) SetDeadline(t time.Time) error {
// CloseWrite closes the write side of a message pipe in byte mode.
func (f *messageBytePipe) CloseWrite() error {
- if !atomic.CompareAndSwapInt32(&f.writeClosed, 0, 1) {
+ if !f.writeClosed.CompareAndSwap(false, true) {
return io.ErrClosedPipe
}
err := f.file.Flush()
if err != nil {
- atomic.StoreInt32(&f.writeClosed, 0)
+ f.writeClosed.Store(false)
return err
}
_, err = f.file.Write(nil)
if err != nil {
- atomic.StoreInt32(&f.writeClosed, 0)
+ f.writeClosed.Store(false)
return err
}
return nil
@@ -70,7 +70,7 @@ func (f *messageBytePipe) CloseWrite() error {
// Write writes bytes to a message pipe in byte mode. Zero-byte writes are ignored, since
// they are used to implement CloseWrite.
func (f *messageBytePipe) Write(b []byte) (int, error) {
- if atomic.LoadInt32(&f.writeClosed) != 0 {
+ if f.writeClosed.Load() {
return 0, io.ErrClosedPipe
}
if len(b) == 0 {