Skip to content

Commit

Permalink
atomic.Int32 -> atomic.AddInt32
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Dec 16, 2023
1 parent d1b7fe2 commit 1f6ff3c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type (
Batch struct {
queue atomic.Int32
queue int32

Prepare func(ctx context.Context) error
Commit func(ctx context.Context) (interface{}, error)
Expand Down Expand Up @@ -50,7 +50,7 @@ func (b *Batch) Do(ctx context.Context, f func(ctx context.Context) error) (res
defer b.Limit.Exit()
b.Limit.Enter()

b.queue.Add(1)
atomic.AddInt32(&b.queue, 1)

defer b.Unlock()
b.Lock()
Expand Down Expand Up @@ -80,8 +80,8 @@ func (b *Batch) Do(ctx context.Context, f func(ctx context.Context) error) (res
b.err = PanicError{Panic: p} // panic overwrites error
}

x := b.queue.Add(-1) // will only be 0 if we are the last exiting the batch
b.cnt++ // count entered
x := atomic.AddInt32(&b.queue, -1) // will only be 0 if we are the last exiting the batch
b.cnt++ // count entered

if x != 0 { // we are not the last exiting the batch, wait for others
b.Cond.Wait() // so wait for the last one to finish the job
Expand Down

0 comments on commit 1f6ff3c

Please sign in to comment.