Skip to content

Commit 8a6098c

Browse files
replace util atomic function with standard sync/atomic operation in hotspot module (#266)
1 parent 7e1fad2 commit 8a6098c

File tree

3 files changed

+4
-47
lines changed

3 files changed

+4
-47
lines changed

core/hotspot/concurrency_stat_slot.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package hotspot
22

33
import (
4+
"sync/atomic"
5+
46
"github.com/alibaba/sentinel-golang/core/base"
57
"github.com/alibaba/sentinel-golang/logging"
6-
"github.com/alibaba/sentinel-golang/util"
78
)
89

910
const (
@@ -39,7 +40,7 @@ func (c *ConcurrencyStatSlot) OnEntryPassed(ctx *base.EntryContext) {
3940
}
4041
continue
4142
}
42-
util.IncrementAndGetInt64(concurrencyPtr)
43+
atomic.AddInt64(concurrencyPtr, 1)
4344
}
4445
}
4546

@@ -64,6 +65,6 @@ func (c *ConcurrencyStatSlot) OnCompleted(ctx *base.EntryContext) {
6465
}
6566
continue
6667
}
67-
util.DecrementAndGetInt64(concurrencyPtr)
68+
atomic.AddInt64(concurrencyPtr, -1)
6869
}
6970
}

util/atomic.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,6 @@ package util
22

33
import "sync/atomic"
44

5-
func IncrementAndGetInt64(v *int64) int64 {
6-
if v == nil {
7-
panic("Nil reference in util.IncrementAndGetInt64")
8-
}
9-
old := int64(0)
10-
for {
11-
old = atomic.LoadInt64(v)
12-
if atomic.CompareAndSwapInt64(v, old, old+1) {
13-
break
14-
}
15-
}
16-
return old + 1
17-
}
18-
19-
func DecrementAndGetInt64(v *int64) int64 {
20-
if v == nil {
21-
panic("Nil reference in util.DecrementAndGetInt64")
22-
}
23-
old := int64(0)
24-
for {
25-
old = atomic.LoadInt64(v)
26-
if atomic.CompareAndSwapInt64(v, old, old-1) {
27-
break
28-
}
29-
}
30-
return old - 1
31-
}
32-
335
type AtomicBool struct {
346
// default 0, means false
357
flag int32

util/atomic_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package util
22

33
import (
4-
"fmt"
5-
"sync"
64
"testing"
75

86
"github.com/stretchr/testify/assert"
@@ -24,17 +22,3 @@ func TestAtomicBool_GetAndSet(t *testing.T) {
2422
b.Set(true)
2523
assert.True(t, b.Get() == true, "the value is false, expect true.")
2624
}
27-
28-
func TestIncrementAndGetInt64(t *testing.T) {
29-
n := int64(0)
30-
wg := &sync.WaitGroup{}
31-
wg.Add(100)
32-
for i := 0; i < 100; i++ {
33-
go func(g *sync.WaitGroup) {
34-
IncrementAndGetInt64(&n)
35-
wg.Done()
36-
}(wg)
37-
}
38-
wg.Wait()
39-
assert.True(t, n == 100, fmt.Sprintf("current n is %d, expect 100.", n))
40-
}

0 commit comments

Comments
 (0)