aboutsummaryrefslogtreecommitdiff
path: root/stm_test.go
diff options
context:
space:
mode:
authorMatt Joiner <anacrolix@gmail.com>2019-10-31 16:21:27 +1100
committerMatt Joiner <anacrolix@gmail.com>2019-10-31 16:21:27 +1100
commitd04075d6f23e92c33e30b244d2f4fc99428ee285 (patch)
tree31a2c212f4c549e43ae2e605baa4f8fbc489a4a5 /stm_test.go
parentPanic when trying to set a nil Var (diff)
downloadstm-d04075d6f23e92c33e30b244d2f4fc99428ee285.tar.gz
stm-d04075d6f23e92c33e30b244d2f4fc99428ee285.tar.xz
Add Tx.Return and a return value from Atomically
Diffstat (limited to 'stm_test.go')
-rw-r--r--stm_test.go30
1 files changed, 12 insertions, 18 deletions
diff --git a/stm_test.go b/stm_test.go
index f030a8a..527e480 100644
--- a/stm_test.go
+++ b/stm_test.go
@@ -223,25 +223,19 @@ func testPingPong(t testing.TB, n int, afterHit func(string)) {
hits := NewVar(0)
ready := NewVar(true) // The ball is ready for hitting.
bat := func(from, to interface{}, noise string) {
- done := false
- for {
- Atomically(func(tx *Tx) {
- if tx.Get(doneVar).(bool) {
- done = true
- return
- }
- tx.Assert(tx.Get(ready).(bool))
- if tx.Get(ball) == from {
- tx.Set(ball, to)
- tx.Set(hits, tx.Get(hits).(int)+1)
- tx.Set(ready, false)
- return
- }
- tx.Retry()
- })
- if done {
- break
+ for !Atomically(func(tx *Tx) {
+ if tx.Get(doneVar).(bool) {
+ tx.Return(true)
+ }
+ tx.Assert(tx.Get(ready).(bool))
+ if tx.Get(ball) == from {
+ tx.Set(ball, to)
+ tx.Set(hits, tx.Get(hits).(int)+1)
+ tx.Set(ready, false)
+ tx.Return(false)
}
+ tx.Retry()
+ }).(bool) {
afterHit(noise)
AtomicSet(ready, true)
}