gotrack.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2014 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. // See https://code.google.com/p/go/source/browse/CONTRIBUTORS
  5. // Licensed under the same terms as Go itself:
  6. // https://code.google.com/p/go/source/browse/LICENSE
  7. // Defensive debug-only utility to track that functions run on the
  8. // goroutine that they're supposed to.
  9. package http2
  10. import (
  11. "bytes"
  12. "errors"
  13. "fmt"
  14. "runtime"
  15. "strconv"
  16. "sync"
  17. )
  18. var DebugGoroutines = false
  19. type goroutineLock uint64
  20. func newGoroutineLock() goroutineLock {
  21. return goroutineLock(curGoroutineID())
  22. }
  23. func (g goroutineLock) check() {
  24. if !DebugGoroutines {
  25. return
  26. }
  27. if curGoroutineID() != uint64(g) {
  28. panic("running on the wrong goroutine")
  29. }
  30. }
  31. var goroutineSpace = []byte("goroutine ")
  32. func curGoroutineID() uint64 {
  33. bp := littleBuf.Get().(*[]byte)
  34. defer littleBuf.Put(bp)
  35. b := *bp
  36. b = b[:runtime.Stack(b, false)]
  37. // Parse the 4707 otu of "goroutine 4707 ["
  38. b = bytes.TrimPrefix(b, goroutineSpace)
  39. i := bytes.IndexByte(b, ' ')
  40. if i < 0 {
  41. panic(fmt.Sprintf("No space found in %q", b))
  42. }
  43. b = b[:i]
  44. n, err := parseUintBytes(b, 10, 64)
  45. if err != nil {
  46. panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
  47. }
  48. return n
  49. }
  50. var littleBuf = sync.Pool{
  51. New: func() interface{} {
  52. buf := make([]byte, 64)
  53. return &buf
  54. },
  55. }
  56. // parseUintBytes is like strconv.ParseUint, but using a []byte.
  57. func parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) {
  58. var cutoff, maxVal uint64
  59. if bitSize == 0 {
  60. bitSize = int(strconv.IntSize)
  61. }
  62. s0 := s
  63. switch {
  64. case len(s) < 1:
  65. err = strconv.ErrSyntax
  66. goto Error
  67. case 2 <= base && base <= 36:
  68. // valid base; nothing to do
  69. case base == 0:
  70. // Look for octal, hex prefix.
  71. switch {
  72. case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
  73. base = 16
  74. s = s[2:]
  75. if len(s) < 1 {
  76. err = strconv.ErrSyntax
  77. goto Error
  78. }
  79. case s[0] == '0':
  80. base = 8
  81. default:
  82. base = 10
  83. }
  84. default:
  85. err = errors.New("invalid base " + strconv.Itoa(base))
  86. goto Error
  87. }
  88. n = 0
  89. cutoff = cutoff64(base)
  90. maxVal = 1<<uint(bitSize) - 1
  91. for i := 0; i < len(s); i++ {
  92. var v byte
  93. d := s[i]
  94. switch {
  95. case '0' <= d && d <= '9':
  96. v = d - '0'
  97. case 'a' <= d && d <= 'z':
  98. v = d - 'a' + 10
  99. case 'A' <= d && d <= 'Z':
  100. v = d - 'A' + 10
  101. default:
  102. n = 0
  103. err = strconv.ErrSyntax
  104. goto Error
  105. }
  106. if int(v) >= base {
  107. n = 0
  108. err = strconv.ErrSyntax
  109. goto Error
  110. }
  111. if n >= cutoff {
  112. // n*base overflows
  113. n = 1<<64 - 1
  114. err = strconv.ErrRange
  115. goto Error
  116. }
  117. n *= uint64(base)
  118. n1 := n + uint64(v)
  119. if n1 < n || n1 > maxVal {
  120. // n+v overflows
  121. n = 1<<64 - 1
  122. err = strconv.ErrRange
  123. goto Error
  124. }
  125. n = n1
  126. }
  127. return n, nil
  128. Error:
  129. return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err}
  130. }
  131. // Return the first number n such that n*base >= 1<<64.
  132. func cutoff64(base int) uint64 {
  133. if base < 2 {
  134. return 0
  135. }
  136. return (1<<64-1)/uint64(base) + 1
  137. }