diff options
author | EuAndreh <eu@euandre.org> | 2025-01-22 12:34:24 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-22 12:41:41 -0300 |
commit | c1d151f5c09b718b521c858c223cd4243927f0c0 (patch) | |
tree | 0667d922a227a6d9977cf282100a439844f31da3 | |
parent | Unify code into default repo format (diff) | |
download | stm-c1d151f5c09b718b521c858c223cd4243927f0c0.tar.gz stm-c1d151f5c09b718b521c858c223cd4243927f0c0.tar.xz |
WIP: Turn cmd/santa-example into functional test
-rw-r--r-- | deps.mk | 9 | ||||
l--------- | tests/functional/santa-claus/main.go | 1 | ||||
-rw-r--r-- | tests/functional/santa-claus/stm.go (renamed from cmd/santa-example/main.go) | 39 |
3 files changed, 30 insertions, 19 deletions
@@ -1,13 +1,17 @@ libs.go = \ src/stm.go \ + tests/functional/santa-claus/stm.go \ tests/stm.go \ mains.go = \ + tests/functional/santa-claus/main.go \ tests/main.go \ functional-tests/lib.go = \ + tests/functional/santa-claus/stm.go \ functional-tests/main.go = \ + tests/functional/santa-claus/main.go \ fuzz-targets/lib.go = \ @@ -18,8 +22,13 @@ benchmarks/lib.go = \ benchmarks/main.go = \ src/stm.a: src/stm.go +tests/functional/santa-claus/main.a: tests/functional/santa-claus/main.go +tests/functional/santa-claus/stm.a: tests/functional/santa-claus/stm.go tests/main.a: tests/main.go tests/stm.a: tests/stm.go +tests/functional/santa-claus/main.bin: tests/functional/santa-claus/main.a tests/main.bin: tests/main.a +tests/functional/santa-claus/main.bin-check: tests/functional/santa-claus/main.bin tests/main.bin-check: tests/main.bin +tests/functional/santa-claus/main.a: tests/functional/santa-claus/$(NAME).a tests/main.a: tests/$(NAME).a diff --git a/tests/functional/santa-claus/main.go b/tests/functional/santa-claus/main.go new file mode 120000 index 0000000..f67563d --- /dev/null +++ b/tests/functional/santa-claus/main.go @@ -0,0 +1 @@ +../../main.go
\ No newline at end of file diff --git a/cmd/santa-example/main.go b/tests/functional/santa-claus/stm.go index 76c0fbc..babdbaa 100644 --- a/cmd/santa-example/main.go +++ b/tests/functional/santa-claus/stm.go @@ -27,23 +27,21 @@ // and then delay for a random interval before trying to join a group again. // // See the paper for more details regarding the solution's implementation. -package main +package stm import ( "fmt" "math/rand" "time" - - "github.com/anacrolix/stm" ) type gate struct { capacity int - remaining *stm.Var[int] + remaining *Var[int] } func (g gate) pass() { - stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { + Atomically(VoidOperation(func(tx *Tx) { rem := g.remaining.Get(tx) // wait until gate can hold us tx.Assert(rem > 0) @@ -53,9 +51,9 @@ func (g gate) pass() { func (g gate) operate() { // open gate, reseting capacity - stm.AtomicSet(g.remaining, g.capacity) + AtomicSet(g.remaining, g.capacity) // wait for gate to be full - stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { + Atomically(VoidOperation(func(tx *Tx) { rem := g.remaining.Get(tx) tx.Assert(rem == 0) })) @@ -64,27 +62,27 @@ func (g gate) operate() { func newGate(capacity int) gate { return gate{ capacity: capacity, - remaining: stm.NewVar(0), // gate starts out closed + remaining: NewVar(0), // gate starts out closed } } type group struct { capacity int - remaining *stm.Var[int] - gate1, gate2 *stm.Var[gate] + remaining *Var[int] + gate1, gate2 *Var[gate] } func newGroup(capacity int) *group { return &group{ capacity: capacity, - remaining: stm.NewVar(capacity), // group starts out with full capacity - gate1: stm.NewVar(newGate(capacity)), - gate2: stm.NewVar(newGate(capacity)), + remaining: NewVar(capacity), // group starts out with full capacity + gate1: NewVar(newGate(capacity)), + gate2: NewVar(newGate(capacity)), } } func (g *group) join() (g1, g2 gate) { - stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { + Atomically(VoidOperation(func(tx *Tx) { rem := g.remaining.Get(tx) // wait until the group can hold us tx.Assert(rem > 0) @@ -96,7 +94,7 @@ func (g *group) join() (g1, g2 gate) { return } -func (g *group) await(tx *stm.Tx) (gate, gate) { +func (g *group) await(tx *Tx) (gate, gate) { // wait for group to be empty rem := g.remaining.Get(tx) tx.Assert(rem == 0) @@ -137,8 +135,8 @@ type selection struct { gate1, gate2 gate } -func chooseGroup(g *group, task string, s *selection) stm.Operation[struct{}] { - return stm.VoidOperation(func(tx *stm.Tx) { +func chooseGroup(g *group, task string, s *selection) Operation[struct{}] { + return VoidOperation(func(tx *Tx) { s.gate1, s.gate2 = g.await(tx) s.task = task }) @@ -148,7 +146,7 @@ func spawnSanta(elves, reindeer *group) { for { fmt.Println("-------------") var s selection - stm.Atomically(stm.Select( + Atomically(Select( // prefer reindeer to elves chooseGroup(reindeer, "deliver toys", &s), chooseGroup(elves, "meet in my study", &s), @@ -160,7 +158,10 @@ func spawnSanta(elves, reindeer *group) { } } -func main() { + + +func MainTest() { + return elfGroup := newGroup(3) for i := 0; i < 10; i++ { go spawnElf(elfGroup, i) |