fix: sync/atomic bug, fix #1804 (#1805)

Co-authored-by: tanghuafa <tanghuafa@bytedance.com>
This commit is contained in:
Tank
2020-05-12 22:09:16 +08:00
committed by GitHub
parent bd6435c982
commit 228e225f84
3 changed files with 18 additions and 17 deletions

View File

@@ -19,9 +19,9 @@ import (
)
type Counter interface {
Count() int64
Inc(int64)
Dec(int64)
Count() int32
Inc(int32)
Dec(int32)
Snapshot() Counter
Clear()
}
@@ -33,28 +33,28 @@ func NewCounter() Counter {
}
type StandardCounter struct {
count int64
count int32
}
func (c *StandardCounter) Count() int64 {
return atomic.LoadInt64(&c.count)
func (c *StandardCounter) Count() int32 {
return atomic.LoadInt32(&c.count)
}
func (c *StandardCounter) Inc(count int64) {
atomic.AddInt64(&c.count, count)
func (c *StandardCounter) Inc(count int32) {
atomic.AddInt32(&c.count, count)
}
func (c *StandardCounter) Dec(count int64) {
atomic.AddInt64(&c.count, -count)
func (c *StandardCounter) Dec(count int32) {
atomic.AddInt32(&c.count, -count)
}
func (c *StandardCounter) Snapshot() Counter {
tmp := &StandardCounter{
count: atomic.LoadInt64(&c.count),
count: atomic.LoadInt32(&c.count),
}
return tmp
}
func (c *StandardCounter) Clear() {
atomic.StoreInt64(&c.count, 0)
atomic.StoreInt32(&c.count, 0)
}