From 552b6c47ff46df7f5be514a7c6354f22a7ad5ca7 Mon Sep 17 00:00:00 2001 From: Roberto Ricci Date: Fri, 18 Aug 2023 23:05:17 +0200 Subject: [PATCH] lib: use atomic types --- lib/atexit/atexit.go | 12 ++++++------ lib/file/preallocate_unix.go | 6 +++--- lib/oauthutil/renew.go | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/atexit/atexit.go b/lib/atexit/atexit.go index fa1dcc8b7..07af75ebc 100644 --- a/lib/atexit/atexit.go +++ b/lib/atexit/atexit.go @@ -19,8 +19,8 @@ var ( exitChan chan os.Signal exitOnce sync.Once registerOnce sync.Once - signalled int32 - runCalled int32 + signalled atomic.Int32 + runCalled atomic.Int32 ) // FnHandle is the type of the handle returned by function `Register` @@ -47,7 +47,7 @@ func Register(fn func()) FnHandle { return } signal.Stop(exitChan) - atomic.StoreInt32(&signalled, 1) + signalled.Store(1) fs.Infof(nil, "Signal received: %s", sig) Run() fs.Infof(nil, "Exiting...") @@ -60,12 +60,12 @@ func Register(fn func()) FnHandle { // Signalled returns true if an exit signal has been received func Signalled() bool { - return atomic.LoadInt32(&signalled) != 0 + return signalled.Load() != 0 } // running returns true if run has been called func running() bool { - return atomic.LoadInt32(&runCalled) != 0 + return runCalled.Load() != 0 } // Unregister a function using the handle returned by `Register` @@ -93,7 +93,7 @@ func IgnoreSignals() { // Run all the at exit functions if they haven't been run already func Run() { - atomic.StoreInt32(&runCalled, 1) + runCalled.Store(1) // Take the lock here (not inside the exitOnce) so we wait // until the exit handlers have run before any calls to Run() // return. diff --git a/lib/file/preallocate_unix.go b/lib/file/preallocate_unix.go index e6f85272c..3ffefb20d 100644 --- a/lib/file/preallocate_unix.go +++ b/lib/file/preallocate_unix.go @@ -18,7 +18,7 @@ var ( unix.FALLOC_FL_KEEP_SIZE, // Default unix.FALLOC_FL_KEEP_SIZE | unix.FALLOC_FL_PUNCH_HOLE, // for ZFS #3066 } - fallocFlagsIndex int32 + fallocFlagsIndex atomic.Int32 preAllocateMu sync.Mutex ) @@ -37,7 +37,7 @@ func PreAllocate(size int64, out *os.File) (err error) { for { - index := atomic.LoadInt32(&fallocFlagsIndex) + index := fallocFlagsIndex.Load() again: if index >= int32(len(fallocFlags)) { return nil // Fallocate is disabled @@ -47,7 +47,7 @@ func PreAllocate(size int64, out *os.File) (err error) { if err == unix.ENOTSUP { // Try the next flags combination index++ - atomic.StoreInt32(&fallocFlagsIndex, index) + fallocFlagsIndex.Store(index) fs.Debugf(nil, "preAllocate: got error on fallocate, trying combination %d/%d: %v", index, len(fallocFlags), err) goto again diff --git a/lib/oauthutil/renew.go b/lib/oauthutil/renew.go index 29786cf69..7f98491e8 100644 --- a/lib/oauthutil/renew.go +++ b/lib/oauthutil/renew.go @@ -10,7 +10,7 @@ import ( type Renew struct { name string // name to use in logs ts *TokenSource // token source that needs renewing - uploads int32 // number of uploads in progress - atomic access required + uploads atomic.Int32 // number of uploads in progress run func() error // a transaction to run to renew the token on } @@ -37,7 +37,7 @@ func (r *Renew) renewOnExpiry() { expiry := r.ts.OnExpiry() for { <-expiry - uploads := atomic.LoadInt32(&r.uploads) + uploads := r.uploads.Load() if uploads != 0 { fs.Debugf(r.name, "Token expired - %d uploads in progress - refreshing", uploads) // Do a transaction @@ -55,12 +55,12 @@ func (r *Renew) renewOnExpiry() { // Start should be called before starting an upload func (r *Renew) Start() { - atomic.AddInt32(&r.uploads, 1) + r.uploads.Add(1) } // Stop should be called after finishing an upload func (r *Renew) Stop() { - atomic.AddInt32(&r.uploads, -1) + r.uploads.Add(-1) } // Invalidate invalidates the token source