From 30943ded71e123886291ad393e55bfb6aa837df3 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 8 Jun 2022 02:28:37 -0600 Subject: BIG change: generic Var[T], txVar, etc. --- cmd/santa-example/main.go | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'cmd/santa-example/main.go') 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 } -- cgit v1.2.3 From 07407adf40111a06e51779976b33595c5fb44815 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 8 Jun 2022 02:45:15 -0600 Subject: Missing Operation type param in cmd/santa-example From https://github.com/anacrolix/stm/commit/6ce9dfc42c83a5d5963c23d85ffebffe0323432e --- cmd/santa-example/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd/santa-example/main.go') diff --git a/cmd/santa-example/main.go b/cmd/santa-example/main.go index 72be64f..1a93ae4 100644 --- a/cmd/santa-example/main.go +++ b/cmd/santa-example/main.go @@ -137,7 +137,7 @@ type selection struct { gate1, gate2 gate } -func chooseGroup(g *group, task string, s *selection) stm.Operation { +func chooseGroup(g *group, task string, s *selection) stm.Operation[struct{}] { return stm.VoidOperation(func(tx *stm.Tx) { s.gate1, s.gate2 = g.await(tx) s.task = task -- cgit v1.2.3 From 2ef3a53065a49ba3238203484588f1f72ccf25fd Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 8 Jun 2022 03:16:02 -0600 Subject: remove unnecessary type parameters --- bench_test.go | 8 ++++---- cmd/santa-example/main.go | 8 ++++---- doc_test.go | 2 +- external_test.go | 4 ++-- rate/ratelimit.go | 4 ++-- stm_test.go | 22 +++++++++++----------- stmutil/context.go | 2 +- 7 files changed, 25 insertions(+), 25 deletions(-) (limited to 'cmd/santa-example/main.go') diff --git a/bench_test.go b/bench_test.go index 0b84715..0d40caf 100644 --- a/bench_test.go +++ b/bench_test.go @@ -8,14 +8,14 @@ import ( ) func BenchmarkAtomicGet(b *testing.B) { - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < b.N; i++ { AtomicGet(x) } } func BenchmarkAtomicSet(b *testing.B) { - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < b.N; i++ { AtomicSet(x, 0) } @@ -24,7 +24,7 @@ func BenchmarkAtomicSet(b *testing.B) { func BenchmarkIncrementSTM(b *testing.B) { for i := 0; i < b.N; i++ { // spawn 1000 goroutines that each increment x by 1 - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < 1000; i++ { go Atomically(VoidOperation(func(tx *Tx) { cur := x.Get(tx) @@ -83,7 +83,7 @@ func BenchmarkReadVarSTM(b *testing.B) { for i := 0; i < b.N; i++ { var wg sync.WaitGroup wg.Add(1000) - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < 1000; i++ { go func() { AtomicGet(x) diff --git a/cmd/santa-example/main.go b/cmd/santa-example/main.go index 1a93ae4..dcc8067 100644 --- a/cmd/santa-example/main.go +++ b/cmd/santa-example/main.go @@ -64,7 +64,7 @@ func (g gate) operate() { func newGate(capacity int) gate { return gate{ capacity: capacity, - remaining: stm.NewVar[int](0), // gate starts out closed + remaining: stm.NewVar(0), // gate starts out closed } } @@ -77,9 +77,9 @@ type group struct { func newGroup(capacity int) *group { return &group{ capacity: capacity, - remaining: stm.NewVar[int](capacity), // group starts out with full capacity - gate1: stm.NewVar[gate](newGate(capacity)), - gate2: stm.NewVar[gate](newGate(capacity)), + remaining: stm.NewVar(capacity), // group starts out with full capacity + gate1: stm.NewVar(newGate(capacity)), + gate2: stm.NewVar(newGate(capacity)), } } diff --git a/doc_test.go b/doc_test.go index 670d07b..ae1af9a 100644 --- a/doc_test.go +++ b/doc_test.go @@ -6,7 +6,7 @@ import ( func Example() { // create a shared variable - n := stm.NewVar[int](3) + n := stm.NewVar(3) // read a variable var v int diff --git a/external_test.go b/external_test.go index d0f5ecf..201712d 100644 --- a/external_test.go +++ b/external_test.go @@ -99,9 +99,9 @@ func BenchmarkInvertedThunderingHerd(b *testing.B) { for i := 0; i < b.N; i++ { done := stm.NewBuiltinEqVar(false) tokens := stm.NewBuiltinEqVar(0) - pending := stm.NewVar[stmutil.Settish](stmutil.NewSet()) + pending := stm.NewVar(stmutil.NewSet()) for range iter.N(1000) { - ready := stm.NewVar[bool](false) + ready := stm.NewVar(false) stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { pending.Set(tx, pending.Get(tx).Add(ready)) })) diff --git a/rate/ratelimit.go b/rate/ratelimit.go index 7fde1ce..6645e04 100644 --- a/rate/ratelimit.go +++ b/rate/ratelimit.go @@ -36,9 +36,9 @@ func Every(interval time.Duration) Limit { func NewLimiter(rate Limit, burst numTokens) *Limiter { rl := &Limiter{ - max: stm.NewVar[int](burst), + max: stm.NewVar(burst), cur: stm.NewBuiltinEqVar(burst), - lastAdd: stm.NewVar[time.Time](time.Now()), + lastAdd: stm.NewVar(time.Now()), rate: rate, } if rate != Inf { diff --git a/stm_test.go b/stm_test.go index 4103563..8726d40 100644 --- a/stm_test.go +++ b/stm_test.go @@ -11,7 +11,7 @@ import ( ) func TestDecrement(t *testing.T) { - x := NewVar[int](1000) + x := NewVar(1000) for i := 0; i < 500; i++ { go Atomically(VoidOperation(func(tx *Tx) { cur := x.Get(tx) @@ -35,7 +35,7 @@ func TestDecrement(t *testing.T) { // read-only transaction aren't exempt from calling tx.inputsChanged func TestReadVerify(t *testing.T) { read := make(chan struct{}) - x, y := NewVar[int](1), NewVar[int](2) + x, y := NewVar(1), NewVar(2) // spawn a transaction that writes to x go func() { @@ -61,7 +61,7 @@ func TestReadVerify(t *testing.T) { } func TestRetry(t *testing.T) { - x := NewVar[int](10) + x := NewVar(10) // spawn 10 transactions, one every 10 milliseconds. This will decrement x // to 0 over the course of 100 milliseconds. go func() { @@ -93,7 +93,7 @@ func TestVerify(t *testing.T) { type foo struct { i int } - x := NewVar[*foo](&foo{3}) + x := NewVar(&foo{3}) read := make(chan struct{}) // spawn a transaction that modifies x @@ -128,14 +128,14 @@ func TestSelect(t *testing.T) { require.Panics(t, func() { Atomically(Select[struct{}]()) }) // with one arg, Select adds no effect - x := NewVar[int](2) + x := NewVar(2) Atomically(Select(VoidOperation(func(tx *Tx) { tx.Assert(x.Get(tx) == 2) }))) picked := Atomically(Select( // always blocks; should never be selected - func(tx *Tx)int { + func(tx *Tx) int { tx.Retry() panic("unreachable") }, @@ -179,7 +179,7 @@ func TestPanic(t *testing.T) { func TestReadWritten(t *testing.T) { // reading a variable written in the same transaction should return the // previously written value - x := NewVar[int](3) + x := NewVar(3) Atomically(VoidOperation(func(tx *Tx) { x.Set(tx, 5) tx.Assert(x.Get(tx) == 5) @@ -188,7 +188,7 @@ func TestReadWritten(t *testing.T) { func TestAtomicSetRetry(t *testing.T) { // AtomicSet should cause waiting transactions to retry - x := NewVar[int](3) + x := NewVar(3) done := make(chan struct{}) go func() { Atomically(VoidOperation(func(tx *Tx) { @@ -207,9 +207,9 @@ func TestAtomicSetRetry(t *testing.T) { func testPingPong(t testing.TB, n int, afterHit func(string)) { ball := NewBuiltinEqVar(false) - doneVar := NewVar[bool](false) - hits := NewVar[int](0) - ready := NewVar[bool](true) // The ball is ready for hitting. + 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() diff --git a/stmutil/context.go b/stmutil/context.go index 9d23e12..6f8ba9b 100644 --- a/stmutil/context.go +++ b/stmutil/context.go @@ -26,7 +26,7 @@ func ContextDoneVar(ctx context.Context) (*stm.Var[bool], func()) { v := stm.NewBuiltinEqVar(true) return v, func() {} } - v := stm.NewVar[bool](false) + v := stm.NewVar(false) go func() { <-ctx.Done() stm.AtomicSet(v, true) -- cgit v1.2.3