diff --git a/core/atomic/flag.go b/core/atomic/flag.go index daca6d4fe..f31ec4c19 100644 --- a/core/atomic/flag.go +++ b/core/atomic/flag.go @@ -7,14 +7,14 @@ type Flag struct { value uint32 } -// SetReturningPrevious sets flag and returns its previous value -func (flag *Flag) SetReturningPrevious() bool { +// Set sets flag and returns its previous value +func (flag *Flag) Set() bool { previousValue := atomic.SwapUint32(&flag.value, 1) return previousValue == 1 } -// Reset resets the flag, putting it in off position -func (flag *Flag) Reset() { +// Unset sets flag +func (flag *Flag) Unset() { atomic.StoreUint32(&flag.value, 0) } @@ -24,11 +24,11 @@ func (flag *Flag) IsSet() bool { return value == 1 } -// SetValue sets the new value in the flag -func (flag *Flag) SetValue(newValue bool) { - if newValue { - _ = flag.SetReturningPrevious() +// Toggle toggles the flag +func (flag *Flag) Toggle(set bool) { + if set { + flag.Set() } else { - flag.Reset() + flag.Unset() } } diff --git a/core/atomic/flag_test.go b/core/atomic/flag_test.go index 1b216e5df..ec4a4212c 100644 --- a/core/atomic/flag_test.go +++ b/core/atomic/flag_test.go @@ -8,8 +8,6 @@ import ( ) func TestFlag_Set(t *testing.T) { - t.Parallel() - var flag Flag var wg sync.WaitGroup @@ -18,12 +16,12 @@ func TestFlag_Set(t *testing.T) { wg.Add(2) go func() { - _ = flag.SetReturningPrevious() + flag.Set() wg.Done() }() go func() { - _ = flag.SetReturningPrevious() + flag.Set() wg.Done() }() @@ -31,24 +29,22 @@ func TestFlag_Set(t *testing.T) { require.True(t, flag.IsSet()) } -func TestFlag_Reset(t *testing.T) { - t.Parallel() - +func TestFlag_Unset(t *testing.T) { var flag Flag var wg sync.WaitGroup - _ = flag.SetReturningPrevious() + flag.Set() require.True(t, flag.IsSet()) wg.Add(2) go func() { - flag.Reset() + flag.Unset() wg.Done() }() go func() { - flag.Reset() + flag.Unset() wg.Done() }() @@ -56,9 +52,7 @@ func TestFlag_Reset(t *testing.T) { require.False(t, flag.IsSet()) } -func TestFlag_SetValue(t *testing.T) { - t.Parallel() - +func TestFlag_Toggle(t *testing.T) { var flag Flag var wg sync.WaitGroup @@ -66,12 +60,12 @@ func TestFlag_SetValue(t *testing.T) { wg.Add(2) go func() { - flag.SetValue(true) + flag.Toggle(true) wg.Done() }() go func() { - flag.SetValue(true) + flag.Toggle(true) wg.Done() }() @@ -82,12 +76,12 @@ func TestFlag_SetValue(t *testing.T) { wg.Add(2) go func() { - flag.SetValue(false) + flag.Toggle(false) wg.Done() }() go func() { - flag.SetValue(false) + flag.Toggle(false) wg.Done() }()