lfstack_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package runtime_test
  5. import (
  6. "math/rand"
  7. . "runtime"
  8. "testing"
  9. "unsafe"
  10. )
  11. type MyNode struct {
  12. LFNode
  13. data int
  14. }
  15. func fromMyNode(node *MyNode) *LFNode {
  16. return (*LFNode)(unsafe.Pointer(node))
  17. }
  18. func toMyNode(node *LFNode) *MyNode {
  19. return (*MyNode)(unsafe.Pointer(node))
  20. }
  21. func TestLFStack(t *testing.T) {
  22. stack := new(uint64)
  23. // Need to keep additional referenfces to nodes, the stack is not all that type-safe.
  24. var nodes []*MyNode
  25. // Check the stack is initially empty.
  26. if LFStackPop(stack) != nil {
  27. t.Fatalf("stack is not empty")
  28. }
  29. // Push one element.
  30. node := &MyNode{data: 42}
  31. nodes = append(nodes, node)
  32. LFStackPush(stack, fromMyNode(node))
  33. // Push another.
  34. node = &MyNode{data: 43}
  35. nodes = append(nodes, node)
  36. LFStackPush(stack, fromMyNode(node))
  37. // Pop one element.
  38. node = toMyNode(LFStackPop(stack))
  39. if node == nil {
  40. t.Fatalf("stack is empty")
  41. }
  42. if node.data != 43 {
  43. t.Fatalf("no lifo")
  44. }
  45. // Pop another.
  46. node = toMyNode(LFStackPop(stack))
  47. if node == nil {
  48. t.Fatalf("stack is empty")
  49. }
  50. if node.data != 42 {
  51. t.Fatalf("no lifo")
  52. }
  53. // Check the stack is empty again.
  54. if LFStackPop(stack) != nil {
  55. t.Fatalf("stack is not empty")
  56. }
  57. if *stack != 0 {
  58. t.Fatalf("stack is not empty")
  59. }
  60. }
  61. var stress []*MyNode
  62. func TestLFStackStress(t *testing.T) {
  63. const K = 100
  64. P := 4 * GOMAXPROCS(-1)
  65. N := 100000
  66. if testing.Short() {
  67. N /= 10
  68. }
  69. // Create 2 stacks.
  70. stacks := [2]*uint64{new(uint64), new(uint64)}
  71. // Need to keep additional references to nodes,
  72. // the lock-free stack is not type-safe.
  73. stress = nil
  74. // Push K elements randomly onto the stacks.
  75. sum := 0
  76. for i := 0; i < K; i++ {
  77. sum += i
  78. node := &MyNode{data: i}
  79. stress = append(stress, node)
  80. LFStackPush(stacks[i%2], fromMyNode(node))
  81. }
  82. c := make(chan bool, P)
  83. for p := 0; p < P; p++ {
  84. go func() {
  85. r := rand.New(rand.NewSource(rand.Int63()))
  86. // Pop a node from a random stack, then push it onto a random stack.
  87. for i := 0; i < N; i++ {
  88. node := toMyNode(LFStackPop(stacks[r.Intn(2)]))
  89. if node != nil {
  90. LFStackPush(stacks[r.Intn(2)], fromMyNode(node))
  91. }
  92. }
  93. c <- true
  94. }()
  95. }
  96. for i := 0; i < P; i++ {
  97. <-c
  98. }
  99. // Pop all elements from both stacks, and verify that nothing lost.
  100. sum2 := 0
  101. cnt := 0
  102. for i := 0; i < 2; i++ {
  103. for {
  104. node := toMyNode(LFStackPop(stacks[i]))
  105. if node == nil {
  106. break
  107. }
  108. cnt++
  109. sum2 += node.data
  110. node.Next = nil
  111. }
  112. }
  113. if cnt != K {
  114. t.Fatalf("Wrong number of nodes %d/%d", cnt, K)
  115. }
  116. if sum2 != sum {
  117. t.Fatalf("Wrong sum %d/%d", sum2, sum)
  118. }
  119. // Let nodes be collected now.
  120. stress = nil
  121. }