aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--funcs.go2
-rw-r--r--go.mod1
-rw-r--r--go.sum6
-rw-r--r--tx.go4
-rw-r--r--var.go9
5 files changed, 15 insertions, 7 deletions
diff --git a/funcs.go b/funcs.go
index 8694eb4..53975e0 100644
--- a/funcs.go
+++ b/funcs.go
@@ -105,7 +105,7 @@ retry:
// AtomicGet is a helper function that atomically reads a value.
func AtomicGet(v *Var) interface{} {
- return v.value.Load().(VarValue).Get()
+ return v.value.Load().Get()
}
// AtomicSet is a helper function that atomically writes a value.
diff --git a/go.mod b/go.mod
index 88ff607..ed11f22 100644
--- a/go.mod
+++ b/go.mod
@@ -11,6 +11,7 @@ require (
)
require (
+ github.com/alecthomas/atomic v0.1.0-alpha2
github.com/bradfitz/iter v0.0.0-20190303215204-33e6a9893b0c // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/huandu/xstrings v1.2.0 // indirect
diff --git a/go.sum b/go.sum
index b535fa7..f0522d7 100644
--- a/go.sum
+++ b/go.sum
@@ -5,6 +5,10 @@ github.com/RoaringBitmap/roaring v0.4.7/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQiJ
github.com/RoaringBitmap/roaring v0.4.17/go.mod h1:D3qVegWTmfCaX4Bl5CrBE9hfrSrrXIr8KVNvRsDi1NI=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
+github.com/alecthomas/assert/v2 v2.0.0-alpha3 h1:pcHeMvQ3OMstAWgaeaXIAL8uzB9xMm2zlxt+/4ml8lk=
+github.com/alecthomas/atomic v0.1.0-alpha2 h1:dqwXmax66gXvHhsOS4pGPZKqYOlTkapELkLb3MNdlH8=
+github.com/alecthomas/atomic v0.1.0-alpha2/go.mod h1:zD6QGEyw49HIq19caJDc2NMXAy8rNi9ROrxtMXATfyI=
+github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/anacrolix/envpprof v0.0.0-20180404065416-323002cec2fa/go.mod h1:KgHhUaQMc8cC0+cEflSgCFNFbKwi5h54gqtVn8yhP7c=
@@ -51,12 +55,14 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/btree v0.0.0-20180124185431-e89373fe6b4a/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
+github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20190309154008-847fc94819f9/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
+github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.0.0/go.mod h1:4qWG/gcEcfX4z/mBDHJ++3ReCw9ibxbsNJbcucJdbSo=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
diff --git a/tx.go b/tx.go
index 9be08b5..3cd0301 100644
--- a/tx.go
+++ b/tx.go
@@ -24,7 +24,7 @@ type Tx struct {
// Check that none of the logged values have changed since the transaction began.
func (tx *Tx) inputsChanged() bool {
for v, read := range tx.reads {
- if read.Changed(v.value.Load().(VarValue)) {
+ if read.Changed(v.value.Load()) {
return true
}
}
@@ -84,7 +84,7 @@ func (tx *Tx) Get(v *Var) interface{} {
// If we haven't previously read v, record its version
vv, ok := tx.reads[v]
if !ok {
- vv = v.value.Load().(VarValue)
+ vv = v.value.Load()
tx.reads[v] = vv
}
return vv.Get()
diff --git a/var.go b/var.go
index 3f43096..ec85996 100644
--- a/var.go
+++ b/var.go
@@ -2,18 +2,19 @@ package stm
import (
"sync"
- "sync/atomic"
+
+ "github.com/alecthomas/atomic"
)
// Holds an STM variable.
type Var struct {
- value atomic.Value
+ value atomic.Value[VarValue]
watchers sync.Map
mu sync.Mutex
}
func (v *Var) changeValue(new interface{}) {
- old := v.value.Load().(VarValue)
+ old := v.value.Load()
newVarValue := old.Set(new)
v.value.Store(newVarValue)
if old.Changed(newVarValue) {
@@ -34,7 +35,7 @@ func (v *Var) wakeWatchers(new VarValue) {
}
}
tx.mu.Unlock()
- return !v.value.Load().(VarValue).Changed(new)
+ return !v.value.Load().Changed(new)
})
}