Skip to content

Commit 79532a2

Browse files
authored
core/bloombits: use atomic type (#26993)
1 parent 881fed0 commit 79532a2

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

core/bloombits/matcher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type Matcher struct {
8383
retrievals chan chan *Retrieval // Retriever processes waiting for task allocations
8484
deliveries chan *Retrieval // Retriever processes waiting for task response deliveries
8585

86-
running uint32 // Atomic flag whether a session is live or not
86+
running atomic.Bool // Atomic flag whether a session is live or not
8787
}
8888

8989
// NewMatcher creates a new pipeline for retrieving bloom bit streams and doing
@@ -146,10 +146,10 @@ func (m *Matcher) addScheduler(idx uint) {
146146
// channel is closed.
147147
func (m *Matcher) Start(ctx context.Context, begin, end uint64, results chan uint64) (*MatcherSession, error) {
148148
// Make sure we're not creating concurrent sessions
149-
if atomic.SwapUint32(&m.running, 1) == 1 {
149+
if m.running.Swap(true) {
150150
return nil, errors.New("matcher already running")
151151
}
152-
defer atomic.StoreUint32(&m.running, 0)
152+
defer m.running.Store(false)
153153

154154
// Initiate a new matching round
155155
session := &MatcherSession{

core/bloombits/matcher_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func testMatcher(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, in
160160
}
161161
}
162162
// Track the number of retrieval requests made
163-
var requested uint32
163+
var requested atomic.Uint32
164164

165165
// Start the matching session for the filter and the retriever goroutines
166166
quit := make(chan struct{})
@@ -208,15 +208,15 @@ func testMatcher(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, in
208208
session.Close()
209209
close(quit)
210210

211-
if retrievals != 0 && requested != retrievals {
212-
t.Errorf("filter = %v blocks = %v intermittent = %v: request count mismatch, have #%v, want #%v", filter, blocks, intermittent, requested, retrievals)
211+
if retrievals != 0 && requested.Load() != retrievals {
212+
t.Errorf("filter = %v blocks = %v intermittent = %v: request count mismatch, have #%v, want #%v", filter, blocks, intermittent, requested.Load(), retrievals)
213213
}
214-
return requested
214+
return requested.Load()
215215
}
216216

217217
// startRetrievers starts a batch of goroutines listening for section requests
218218
// and serving them.
219-
func startRetrievers(session *MatcherSession, quit chan struct{}, retrievals *uint32, batch int) {
219+
func startRetrievers(session *MatcherSession, quit chan struct{}, retrievals *atomic.Uint32, batch int) {
220220
requests := make(chan chan *Retrieval)
221221

222222
for i := 0; i < 10; i++ {
@@ -238,7 +238,7 @@ func startRetrievers(session *MatcherSession, quit chan struct{}, retrievals *ui
238238
for i, section := range task.Sections {
239239
if rand.Int()%4 != 0 { // Handle occasional missing deliveries
240240
task.Bitsets[i] = generateBitset(task.Bit, section)
241-
atomic.AddUint32(retrievals, 1)
241+
retrievals.Add(1)
242242
}
243243
}
244244
request <- task

core/bloombits/scheduler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ func testScheduler(t *testing.T, clients int, fetchers int, requests int) {
4545
fetch := make(chan *request, 16)
4646
defer close(fetch)
4747

48-
var delivered uint32
48+
var delivered atomic.Uint32
4949
for i := 0; i < fetchers; i++ {
5050
go func() {
5151
defer fetchPend.Done()
5252

5353
for req := range fetch {
54-
atomic.AddUint32(&delivered, 1)
54+
delivered.Add(1)
5555

5656
f.deliver([]uint64{
5757
req.section + uint64(requests), // Non-requested data (ensure it doesn't go out of bounds)
@@ -97,7 +97,7 @@ func testScheduler(t *testing.T, clients int, fetchers int, requests int) {
9797
}
9898
pend.Wait()
9999

100-
if have := atomic.LoadUint32(&delivered); int(have) != requests {
100+
if have := delivered.Load(); int(have) != requests {
101101
t.Errorf("request count mismatch: have %v, want %v", have, requests)
102102
}
103103
}

0 commit comments

Comments
 (0)