gotrack.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. func (g goroutineLock) checkNotOn() {
  32. if !DebugGoroutines {
  33. return
  34. }
  35. if curGoroutineID() == uint64(g) {
  36. panic("running on the wrong goroutine")
  37. }
  38. }
  39. var goroutineSpace = []byte("goroutine ")
  40. func curGoroutineID() uint64 {
  41. bp := littleBuf.Get().(*[]byte)
  42. defer littleBuf.Put(bp)
  43. b := *bp
  44. b = b[:runtime.Stack(b, false)]
  45. // Parse the 4707 out of "goroutine 4707 ["
  46. b = bytes.TrimPrefix(b, goroutineSpace)
  47. i := bytes.IndexByte(b, ' ')
  48. if i < 0 {
  49. panic(fmt.Sprintf("No space found in %q", b))
  50. }
  51. b = b[:i]
  52. n, err := parseUintBytes(b, 10, 64)
  53. if err != nil {
  54. panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
  55. }
  56. return n
  57. }
  58. var littleBuf = sync.Pool{
  59. New: func() interface{} {
  60. buf := make([]byte, 64)
  61. return &buf
  62. },
  63. }
  64. // parseUintBytes is like strconv.ParseUint, but using a []byte.
  65. func parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) {
  66. var cutoff, maxVal uint64
  67. if bitSize == 0 {
  68. bitSize = int(strconv.IntSize)
  69. }
  70. s0 := s
  71. switch {
  72. case len(s) < 1:
  73. err = strconv.ErrSyntax
  74. goto Error
  75. case 2 <= base && base <= 36:
  76. // valid base; nothing to do
  77. case base == 0:
  78. // Look for octal, hex prefix.
  79. switch {
  80. case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
  81. base = 16
  82. s = s[2:]
  83. if len(s) < 1 {
  84. err = strconv.ErrSyntax
  85. goto Error
  86. }
  87. case s[0] == '0':
  88. base = 8
  89. default:
  90. base = 10
  91. }
  92. default:
  93. err = errors.New("invalid base " + strconv.Itoa(base))
  94. goto Error
  95. }
  96. n = 0
  97. cutoff = cutoff64(base)
  98. maxVal = 1<<uint(bitSize) - 1
  99. for i := 0; i < len(s); i++ {
  100. var v byte
  101. d := s[i]
  102. switch {
  103. case '0' <= d && d <= '9':
  104. v = d - '0'
  105. case 'a' <= d && d <= 'z':
  106. v = d - 'a' + 10
  107. case 'A' <= d && d <= 'Z':
  108. v = d - 'A' + 10
  109. default:
  110. n = 0
  111. err = strconv.ErrSyntax
  112. goto Error
  113. }
  114. if int(v) >= base {
  115. n = 0
  116. err = strconv.ErrSyntax
  117. goto Error
  118. }
  119. if n >= cutoff {
  120. // n*base overflows
  121. n = 1<<64 - 1
  122. err = strconv.ErrRange
  123. goto Error
  124. }
  125. n *= uint64(base)
  126. n1 := n + uint64(v)
  127. if n1 < n || n1 > maxVal {
  128. // n+v overflows
  129. n = 1<<64 - 1
  130. err = strconv.ErrRange
  131. goto Error
  132. }
  133. n = n1
  134. }
  135. return n, nil
  136. Error:
  137. return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err}
  138. }
  139. // Return the first number n such that n*base >= 1<<64.
  140. func cutoff64(base int) uint64 {
  141. if base < 2 {
  142. return 0
  143. }
  144. return (1<<64-1)/uint64(base) + 1
  145. }