diff options
Diffstat (limited to 'cmd/santa-example/main.go')
-rw-r--r-- | cmd/santa-example/main.go | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/cmd/santa-example/main.go b/cmd/santa-example/main.go index 3c2b9ea..72be64f 100644 --- a/cmd/santa-example/main.go +++ b/cmd/santa-example/main.go @@ -39,15 +39,15 @@ import ( type gate struct { capacity int - remaining *stm.Var + remaining *stm.Var[int] } func (g gate) pass() { stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - rem := tx.Get(g.remaining).(int) + rem := g.remaining.Get(tx) // wait until gate can hold us tx.Assert(rem > 0) - tx.Set(g.remaining, rem-1) + g.remaining.Set(tx, rem-1) })) } @@ -56,7 +56,7 @@ func (g gate) operate() { stm.AtomicSet(g.remaining, g.capacity) // wait for gate to be full stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - rem := tx.Get(g.remaining).(int) + rem := g.remaining.Get(tx) tx.Assert(rem == 0) })) } @@ -64,49 +64,49 @@ func (g gate) operate() { func newGate(capacity int) gate { return gate{ capacity: capacity, - remaining: stm.NewVar(0), // gate starts out closed + remaining: stm.NewVar[int](0), // gate starts out closed } } type group struct { capacity int - remaining *stm.Var - gate1, gate2 *stm.Var + remaining *stm.Var[int] + gate1, gate2 *stm.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: stm.NewVar[int](capacity), // group starts out with full capacity + gate1: stm.NewVar[gate](newGate(capacity)), + gate2: stm.NewVar[gate](newGate(capacity)), } } func (g *group) join() (g1, g2 gate) { stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - rem := tx.Get(g.remaining).(int) + rem := g.remaining.Get(tx) // wait until the group can hold us tx.Assert(rem > 0) - tx.Set(g.remaining, rem-1) + g.remaining.Set(tx, rem-1) // return the group's gates - g1 = tx.Get(g.gate1).(gate) - g2 = tx.Get(g.gate2).(gate) + g1 = g.gate1.Get(tx) + g2 = g.gate2.Get(tx) })) return } func (g *group) await(tx *stm.Tx) (gate, gate) { // wait for group to be empty - rem := tx.Get(g.remaining).(int) + rem := g.remaining.Get(tx) tx.Assert(rem == 0) // get the group's gates - g1 := tx.Get(g.gate1).(gate) - g2 := tx.Get(g.gate2).(gate) + g1 := g.gate1.Get(tx) + g2 := g.gate2.Get(tx) // reset group - tx.Set(g.remaining, g.capacity) - tx.Set(g.gate1, newGate(g.capacity)) - tx.Set(g.gate2, newGate(g.capacity)) + g.remaining.Set(tx, g.capacity) + g.gate1.Set(tx, newGate(g.capacity)) + g.gate2.Set(tx, newGate(g.capacity)) return g1, g2 } |