diff options
Diffstat (limited to 'src/runtime/lfstack_test.go')
| -rw-r--r-- | src/runtime/lfstack_test.go | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/src/runtime/lfstack_test.go b/src/runtime/lfstack_test.go index d0a1b6ba06..e36297e541 100644 --- a/src/runtime/lfstack_test.go +++ b/src/runtime/lfstack_test.go @@ -16,6 +16,17 @@ type MyNode struct { data int } +// allocMyNode allocates nodes that are stored in an lfstack +// outside the Go heap. +// We require lfstack objects to live outside the heap so that +// checkptr passes on the unsafe shenanigans used. +func allocMyNode(data int) *MyNode { + n := (*MyNode)(PersistentAlloc(unsafe.Sizeof(MyNode{}))) + LFNodeValidate(&n.LFNode) + n.data = data + return n +} + func fromMyNode(node *MyNode) *LFNode { return (*LFNode)(unsafe.Pointer(node)) } @@ -30,22 +41,17 @@ func TestLFStack(t *testing.T) { stack := new(uint64) global = stack // force heap allocation - // Need to keep additional references to nodes, the stack is not all that type-safe. - var nodes []*MyNode - // Check the stack is initially empty. if LFStackPop(stack) != nil { t.Fatalf("stack is not empty") } // Push one element. - node := &MyNode{data: 42} - nodes = append(nodes, node) + node := allocMyNode(42) LFStackPush(stack, fromMyNode(node)) // Push another. - node = &MyNode{data: 43} - nodes = append(nodes, node) + node = allocMyNode(43) LFStackPush(stack, fromMyNode(node)) // Pop one element. @@ -75,8 +81,6 @@ func TestLFStack(t *testing.T) { } } -var stress []*MyNode - func TestLFStackStress(t *testing.T) { const K = 100 P := 4 * GOMAXPROCS(-1) @@ -86,15 +90,11 @@ func TestLFStackStress(t *testing.T) { } // Create 2 stacks. stacks := [2]*uint64{new(uint64), new(uint64)} - // Need to keep additional references to nodes, - // the lock-free stack is not type-safe. - stress = nil // Push K elements randomly onto the stacks. sum := 0 for i := 0; i < K; i++ { sum += i - node := &MyNode{data: i} - stress = append(stress, node) + node := allocMyNode(i) LFStackPush(stacks[i%2], fromMyNode(node)) } c := make(chan bool, P) @@ -134,7 +134,4 @@ func TestLFStackStress(t *testing.T) { if sum2 != sum { t.Fatalf("Wrong sum %d/%d", sum2, sum) } - - // Let nodes be collected now. - stress = nil } |
