diff options
Diffstat (limited to 'tests/benchmarks/ping-pong')
l--------- | tests/benchmarks/ping-pong/main.go | 1 | ||||
-rw-r--r-- | tests/benchmarks/ping-pong/stm.go | 59 |
2 files changed, 60 insertions, 0 deletions
diff --git a/tests/benchmarks/ping-pong/main.go b/tests/benchmarks/ping-pong/main.go new file mode 120000 index 0000000..f67563d --- /dev/null +++ b/tests/benchmarks/ping-pong/main.go @@ -0,0 +1 @@ +../../main.go
\ No newline at end of file diff --git a/tests/benchmarks/ping-pong/stm.go b/tests/benchmarks/ping-pong/stm.go new file mode 100644 index 0000000..6631ea8 --- /dev/null +++ b/tests/benchmarks/ping-pong/stm.go @@ -0,0 +1,59 @@ +package stm + +import ( + "flag" + "sync" +) + + + +func testPingPong(n int, afterHit func(string)) { + ball := NewBuiltinEqVar(false) + doneVar := NewVar(false) + hits := NewVar(0) + ready := NewVar(true) // The ball is ready for hitting. + var wg sync.WaitGroup + bat := func(from, to bool, noise string) { + defer wg.Done() + for !Atomically(func(tx *Tx) any { + if doneVar.Get(tx) { + return true + } + tx.Assert(ready.Get(tx)) + if ball.Get(tx) == from { + ball.Set(tx, to) + hits.Set(tx, hits.Get(tx)+1) + ready.Set(tx, false) + return false + } + return tx.Retry() + }).(bool) { + afterHit(noise) + AtomicSet(ready, true) + } + } + wg.Add(2) + go bat(false, true, "ping!") + go bat(true, false, "pong!") + Atomically(VoidOperation(func(tx *Tx) { + tx.Assert(hits.Get(tx) >= n) + doneVar.Set(tx, true) + })) + wg.Wait() +} + + +var nFlag = flag.Int( + "n", + 1_000, + "The number of iterations to execute", +) + +func MainTest() { + flag.Parse() + n := *nFlag + + for i := 0; i < n; i++ { + testPingPong(n, func(string) {}) + } +} |