lib: use atomic types

This commit is contained in:
Roberto Ricci 2023-08-18 23:05:17 +02:00 committed by Nick Craig-Wood
parent 01a155fb00
commit 552b6c47ff
3 changed files with 13 additions and 13 deletions

View File

@ -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.

View File

@ -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

View File

@ -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