Skip to content

Commit d402489

Browse files
authored
refactor: use atomic.Int32 type (#22)
1 parent ab0c12e commit d402489

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

once_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ func TestOnce(t *testing.T) {
2323

2424
func TestOnceConcurrent(t *testing.T) {
2525
var once Once[int32]
26-
var count int32
26+
var count atomic.Int32
2727
var wg sync.WaitGroup
2828

2929
for i := 0; i < 10; i++ {
3030
wg.Add(1)
3131
go func() {
3232
defer wg.Done()
3333
result, _ := once.Do(func() (int32, error) {
34-
newCount := atomic.AddInt32(&count, 1)
34+
newCount := count.Add(1)
3535
return newCount, nil
3636
})
37-
atomic.StoreInt32(&count, result)
37+
count.Store(result)
3838
}()
3939
}
4040
wg.Wait()
41-
assert.Equal(t, count, 1)
41+
assert.Equal(t, int(count.Load()), 1)
4242
}
4343

4444
func TestOncePanic(t *testing.T) {

wait_group_context.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
type WaitGroupContext struct {
1414
ctx context.Context
1515
done chan struct{}
16-
counter int32
17-
state int32
16+
counter atomic.Int32
17+
state atomic.Int32
1818
}
1919

2020
// NewWaitGroupContext returns a new WaitGroupContext with Context ctx.
@@ -29,10 +29,10 @@ func NewWaitGroupContext(ctx context.Context) *WaitGroupContext {
2929
// If the counter becomes zero, all goroutines blocked on Wait are released.
3030
// If the counter goes negative, Add panics.
3131
func (wgc *WaitGroupContext) Add(delta int) {
32-
counter := atomic.AddInt32(&wgc.counter, int32(delta))
33-
if counter == 0 && atomic.CompareAndSwapInt32(&wgc.state, 0, 1) {
32+
counter := wgc.counter.Add(int32(delta))
33+
if counter == 0 && wgc.state.CompareAndSwap(0, 1) {
3434
wgc.release()
35-
} else if counter < 0 && atomic.LoadInt32(&wgc.state) == 0 {
35+
} else if counter < 0 && wgc.state.Load() == 0 {
3636
panic("async: negative WaitGroupContext counter")
3737
}
3838
}

wait_group_context_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,45 @@ package async
22

33
import (
44
"context"
5+
"sync/atomic"
56
"testing"
67
"time"
78

89
"github.com/reugn/async/internal/assert"
910
)
1011

1112
func TestWaitGroupContext(t *testing.T) {
12-
result := 0
13+
var result atomic.Int32
1314
wgc := NewWaitGroupContext(context.Background())
1415
wgc.Add(2)
1516

1617
go func() {
1718
defer wgc.Done()
1819
time.Sleep(time.Millisecond * 10)
19-
result++
20+
result.Add(1)
2021
}()
2122
go func() {
2223
defer wgc.Done()
2324
time.Sleep(time.Millisecond * 20)
24-
result += 2
25+
result.Add(2)
2526
}()
2627
go func() {
2728
wgc.Wait()
28-
result += 3
29+
result.Add(3)
2930
}()
3031

3132
wgc.Wait()
3233
time.Sleep(time.Millisecond * 10)
3334

34-
assert.Equal(t, result, 6)
35+
assert.Equal(t, int(result.Load()), 6)
3536
}
3637

3738
func TestWaitGroupContextCanceled(t *testing.T) {
38-
result := 0
39+
var result atomic.Int32
3940
ctx, cancelFunc := context.WithCancel(context.Background())
4041
go func() {
4142
time.Sleep(time.Millisecond * 100)
42-
result += 10
43+
result.Add(10)
4344
cancelFunc()
4445
}()
4546
wgc := NewWaitGroupContext(ctx)
@@ -48,22 +49,22 @@ func TestWaitGroupContextCanceled(t *testing.T) {
4849
go func() {
4950
defer wgc.Done()
5051
time.Sleep(time.Millisecond * 10)
51-
result++
52+
result.Add(1)
5253
}()
5354
go func() {
5455
defer wgc.Done()
5556
time.Sleep(time.Millisecond * 300)
56-
result += 2
57+
result.Add(2)
5758
}()
5859
go func() {
5960
wgc.Wait()
60-
result += 100
61+
result.Add(100)
6162
}()
6263

6364
wgc.Wait()
6465
time.Sleep(time.Millisecond * 10)
6566

66-
assert.Equal(t, result, 111)
67+
assert.Equal(t, int(result.Load()), 111)
6768
}
6869

6970
func TestWaitGroupContextPanic(t *testing.T) {

0 commit comments

Comments
 (0)