-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add async.Value implementation (#21)
- Loading branch information
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package async | ||
|
||
import ( | ||
"sync/atomic" | ||
) | ||
|
||
// A Value provides an atomic load and store of a value of any type. | ||
// The behavior is analogous to atomic.Value, except that | ||
// the value is not required to be of the same specific type. | ||
// Can be useful for storing different implementations of an interface. | ||
type Value struct { | ||
p atomic.Pointer[atomic.Value] | ||
} | ||
|
||
// CompareAndSwap executes the compare-and-swap operation for the Value. | ||
// The current implementation is not atomic. | ||
func (v *Value) CompareAndSwap(old any, new any) (swapped bool) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
swapped = false | ||
} | ||
}() | ||
delegate := v.p.Load() | ||
if delegate != nil { | ||
if old == delegate.Load() { | ||
v.p.Store(initValue(new)) | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Load returns the value set by the most recent Store. | ||
// It returns nil if there has been no call to Store for this Value. | ||
func (v *Value) Load() (val any) { | ||
delegate := v.p.Load() | ||
if delegate != nil { | ||
return delegate.Load() | ||
} | ||
return nil | ||
} | ||
|
||
// Store sets the value of the Value v to val. | ||
// Store(nil) panics. | ||
func (v *Value) Store(val any) { | ||
v.p.Store(initValue(val)) | ||
} | ||
|
||
// Swap stores new into Value and returns the previous value. | ||
// It returns nil if the Value is empty. | ||
// Swap(nil) panics. | ||
func (v *Value) Swap(new any) (old any) { | ||
oldValue := v.p.Swap(initValue(new)) | ||
if oldValue != nil { | ||
return oldValue.Load() | ||
} | ||
return nil | ||
} | ||
|
||
func initValue(val any) *atomic.Value { | ||
value := atomic.Value{} | ||
value.Store(val) | ||
return &value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package async | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/reugn/async/internal/assert" | ||
) | ||
|
||
//nolint:funlen | ||
func TestValueCompareAndSwap(t *testing.T) { | ||
var value Value | ||
swapped := value.CompareAndSwap(1, 2) | ||
assert.Equal(t, swapped, false) | ||
assert.Equal(t, value.Load(), nil) | ||
|
||
swapped = value.CompareAndSwap(1, nil) | ||
assert.Equal(t, swapped, false) | ||
assert.Equal(t, value.Load(), nil) | ||
|
||
swapped = value.CompareAndSwap(nil, 1) | ||
assert.Equal(t, swapped, false) | ||
assert.Equal(t, value.Load(), nil) | ||
|
||
value.Store(1) | ||
|
||
swapped = value.CompareAndSwap("a", nil) | ||
assert.Equal(t, swapped, false) | ||
assert.Equal(t, value.Load(), 1) | ||
|
||
swapped = value.CompareAndSwap(nil, nil) | ||
assert.Equal(t, swapped, false) | ||
assert.Equal(t, value.Load(), 1) | ||
|
||
swapped = value.CompareAndSwap("a", 2) | ||
assert.Equal(t, swapped, false) | ||
assert.Equal(t, value.Load(), 1) | ||
|
||
swapped = value.CompareAndSwap(-1, 2) | ||
assert.Equal(t, swapped, false) | ||
assert.Equal(t, value.Load(), 1) | ||
|
||
swapped = value.CompareAndSwap(1, 2) | ||
assert.Equal(t, swapped, true) | ||
assert.Equal(t, value.Load(), 2) | ||
|
||
swapped = value.CompareAndSwap(2, "a") | ||
assert.Equal(t, swapped, true) | ||
assert.Equal(t, value.Load(), "a") | ||
|
||
stringPointer := ptr("b") | ||
swapped = value.CompareAndSwap("a", stringPointer) | ||
assert.Equal(t, swapped, true) | ||
if value.Load() != stringPointer { | ||
t.Fail() | ||
} | ||
|
||
swapped = value.CompareAndSwap(ptr("b"), "c") | ||
assert.Equal(t, swapped, false) | ||
if value.Load() != stringPointer { | ||
t.Fail() | ||
} | ||
|
||
swapped = value.CompareAndSwap(stringPointer, "c") | ||
assert.Equal(t, swapped, true) | ||
assert.Equal(t, value.Load(), "c") | ||
} | ||
|
||
func TestValueLoad(t *testing.T) { | ||
var value Value | ||
assert.Equal(t, value.Load(), nil) | ||
|
||
value.Store(1) | ||
assert.Equal(t, value.Load(), 1) | ||
} | ||
|
||
func TestValueStore(t *testing.T) { | ||
var value Value | ||
value.Store(1) | ||
assert.Equal(t, value.Load(), 1) | ||
|
||
assert.Panic(t, func() { value.Store(nil) }) | ||
|
||
value.Store("a") | ||
assert.Equal(t, value.Load(), "a") | ||
|
||
stringPointer := ptr("b") | ||
value.Store(stringPointer) | ||
if value.Load() != stringPointer { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestValueSwap(t *testing.T) { | ||
var value Value | ||
old := value.Swap(1) | ||
assert.Equal(t, old, nil) | ||
|
||
assert.Panic(t, func() { _ = value.Swap(nil) }) | ||
|
||
old = value.Swap("a") | ||
assert.Equal(t, old, 1) | ||
|
||
stringPointer := ptr("b") | ||
old = value.Swap(stringPointer) | ||
assert.Equal(t, old, "a") | ||
if value.Load() != stringPointer { | ||
t.Fail() | ||
} | ||
} |