Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J committed Oct 2, 2024
1 parent 6f1e7b0 commit 484894d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions seqlock/seq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (n *Node[K, V]) Value() V {
continue
}

// u.Load()
u.Load()
value := n.value

if seq == n.lock.Load() {
Expand Down Expand Up @@ -95,36 +95,37 @@ func reader(node *Node[int, int], num_iterations int, cdone chan bool) {
cdone <- true
}

func writer(node *Node[int, int], num_iterations int, cdone chan bool) {
for i := 0; i < 2*num_iterations; i++ {
func writer(node *Node[int, int], num_iterations int, done atomic.Bool) {
for !done.Load() {
node.Lock()
node.SetValue(1)
node.SetValue(2)
node.SetValue(3)
node.SetValue(0)
node.Unlock()
}
cdone <- true
}

func HammerRWMutex(numReaders, num_iterations int) {
cdone := make(chan bool)
node := &Node[int, int]{}
go writer(node, num_iterations, cdone)
var done atomic.Bool
go writer(node, num_iterations, done)
var i int
for i = 0; i < numReaders/2; i++ {
go reader(node, num_iterations, cdone)
}
go writer(node, num_iterations, cdone)
go writer(node, num_iterations, done)
for ; i < numReaders; i++ {
go reader(node, num_iterations, cdone)
}
// Wait for the 2 writers and all readers to finish.
for i := 0; i < 2+numReaders; i++ {
for i := 0; i < numReaders; i++ {
<-cdone
}
done.Store(true)
}

func TestNode_Seqlock(t *testing.T) {
HammerRWMutex(100, 10000000)
HammerRWMutex(100, 5000000)
}

0 comments on commit 484894d

Please sign in to comment.