Skip to content

Commit f44b66d

Browse files
committed
Support versions of Go prior to the addition of sync.Pool.
1 parent e26a35c commit f44b66d

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

stack.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ func findSigpanic() *runtime.Func {
183183
func() int {
184184
defer func() {
185185
if p := recover(); p != nil {
186-
pcs := pcStackPool.Get().([]uintptr)
187-
pcs = pcs[:cap(pcs)]
186+
pcs := getUintptrs()
188187
n := runtime.Callers(2, pcs)
189188
for _, pc := range pcs[:n] {
190189
f := runtime.FuncForPC(pc)
@@ -193,7 +192,7 @@ func findSigpanic() *runtime.Func {
193192
break
194193
}
195194
}
196-
pcStackPool.Put(pcs)
195+
putUintptrs(pcs)
197196
}
198197
}()
199198
// intentional division by zero fault
@@ -208,19 +207,14 @@ var (
208207
spOnce sync.Once
209208
)
210209

211-
var pcStackPool = sync.Pool{
212-
New: func() interface{} { return make([]uintptr, 1000) },
213-
}
214-
215210
// Trace returns a CallStack for the current goroutine with element 0
216211
// identifying the calling function.
217212
func Trace() CallStack {
218213
spOnce.Do(func() {
219214
sigpanic = findSigpanic()
220215
})
221216

222-
pcs := pcStackPool.Get().([]uintptr)
223-
pcs = pcs[:cap(pcs)]
217+
pcs := getUintptrs()
224218

225219
n := runtime.Callers(2, pcs)
226220
cs := make([]Call, n)
@@ -236,7 +230,7 @@ func Trace() CallStack {
236230
}
237231
}
238232

239-
pcStackPool.Put(pcs)
233+
putUintptrs(pcs)
240234

241235
return cs
242236
}

stack12.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// +build !go1.3
2+
3+
package stack
4+
5+
import (
6+
"runtime"
7+
)
8+
9+
func getUintptrs() []uintptr {
10+
select {
11+
case s := <-pcStackPool:
12+
return s[:cap(s)]
13+
default:
14+
return make([]uintptr, 1000)
15+
}
16+
}
17+
18+
func putUintptrs(s []uintptr) {
19+
select {
20+
case pcStackPool <- s:
21+
default:
22+
}
23+
}
24+
25+
var pcStackPool = make(chan []uintptr, runtime.GOMAXPROCS(n))

stack13.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build go1.3
2+
3+
package stack
4+
5+
import (
6+
"sync"
7+
)
8+
9+
func getUintptrs() []uintptr {
10+
s := pcStackPool.Get().([]uintptr)
11+
s = s[:cap(s)]
12+
return s
13+
}
14+
15+
func putUintptrs(s []uintptr) {
16+
pcStackPool.Put(s)
17+
}
18+
19+
var pcStackPool = sync.Pool{
20+
New: func() interface{} { return make([]uintptr, 1000) },
21+
}

0 commit comments

Comments
 (0)