aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmarks/inverted-thundering-herd/stm.go
blob: 1f240db204def47f255babe4eef0ef3de98eb34d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package stm

import (
	"flag"
)



const maxTokens = 25

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++ {
		continue // FIXME
		done := NewBuiltinEqVar(false)
		tokens := NewBuiltinEqVar(0)
		pending := NewVar(NewSet[*Var[bool]]())
		for i := 0; i < 1000; i++ {
			ready := NewVar(false)
			Atomically(VoidOperation(func(tx *Tx) {
				pending.Set(tx, pending.Get(tx).Add(ready))
			}))
			go func() {
				Atomically(VoidOperation(func(tx *Tx) {
					tx.Assert(ready.Get(tx))
					set := pending.Get(tx)
					if !set.Contains(ready) {
						panic("couldn't find ourselves in pending")
					}
					pending.Set(tx, set.Delete(ready))
				}))
				//b.Log("waiter finished")
			}()
		}
		go func() {
			for Atomically(func(tx *Tx) bool {
				if done.Get(tx) {
					return false
				}
				tx.Assert(tokens.Get(tx) < maxTokens)
				tokens.Set(tx, tokens.Get(tx)+1)
				return true
			}) {
			}
		}()
		go func() {
			for Atomically(func(tx *Tx) bool {
				tx.Assert(tokens.Get(tx) > 0)
				tokens.Set(tx, tokens.Get(tx)-1)
				pending.Get(tx).Range(func(ready *Var[bool]) bool {
					if !ready.Get(tx) {
						ready.Set(tx, true)
						return false
					}
					return true
				})
				return !done.Get(tx)
			}) {
			}
		}()
		Atomically(VoidOperation(func(tx *Tx) {
			tx.Assert(pending.Get(tx).(Lenner).Len() == 0)
		}))
		AtomicSet(done, true)
	}
}