Skip to content

Commit 6e9ac1f

Browse files
authored
update stack lock (#21)
1 parent 40ddf5b commit 6e9ac1f

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

generic/util.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,12 @@ func IsNil(err interface{}) bool {
191191
return true
192192
}
193193

194-
return v.IsZero()
194+
switch v.Kind() {
195+
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, reflect.Slice, reflect.Interface:
196+
return v.IsNil()
197+
default:
198+
return false
199+
}
195200
}
196201

197202
func Init(fn func()) error {

generic/util_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@ package generic
33
import (
44
"strconv"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

10+
type err1 struct {
11+
}
12+
13+
func (e err1) Error() string {
14+
return ""
15+
}
16+
817
func TestMap(t *testing.T) {
918
data := []int{1, 2, 3, 4}
1019
t.Log(Map(data, func(i int) string {
1120
return strconv.Itoa(data[i])
1221
}))
1322
}
23+
24+
func TestIsNil(t *testing.T) {
25+
assert.Equal(t, IsNil(err1{}), false)
26+
assert.Equal(t, IsNil(struct{}{}), false)
27+
assert.Equal(t, IsNil(nil), true)
28+
assert.Equal(t, IsNil(any(nil)), true)
29+
assert.Equal(t, IsNil((*struct{})(nil)), true)
30+
}

stack/stack.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var (
12-
cache = make(map[uintptr]*Frame)
12+
cache sync.Map
1313
mu sync.Mutex
1414
goRoot string
1515
)
@@ -110,22 +110,20 @@ func stack(p uintptr) *Frame {
110110
return nil
111111
}
112112

113-
v, ok := cache[p]
113+
v, ok := cache.Load(p)
114114
if ok {
115-
return v
115+
return v.(*Frame)
116116
}
117117

118118
mu.Lock()
119119
defer mu.Unlock()
120120

121-
v, ok = cache[p]
121+
v, ok = cache.Load(p)
122122
if ok {
123-
return v
123+
return v.(*Frame)
124124
}
125125

126-
defer func() {
127-
cache[p] = v
128-
}()
126+
defer func() { cache.Store(p, v) }()
129127

130128
ff := runtime.FuncForPC(p)
131129
if ff == nil {
@@ -140,7 +138,7 @@ func stack(p uintptr) *Frame {
140138
Name: ma[len(ma)-1],
141139
Pkg: strings.Join(ma[:len(ma)-1], "."),
142140
}
143-
return v
141+
return v.(*Frame)
144142
}
145143

146144
func pkgIndex(file, funcName string) int {

0 commit comments

Comments
 (0)