Skip to content

Commit 2cae75d

Browse files
committed
Update deliveries value atomically to avoid potential race condition
1 parent e2d5eb5 commit 2cae75d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

tests/dispatch_read2.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,20 @@ test_writes_reads_eagain(void) // rdar://problem/8333366
343343
test_errno("pipe", errno, 0);
344344
test_stop();
345345
}
346-
const size_t chunks = 320;
346+
const int32_t chunks = 320;
347347
const size_t siz_chunk = 32, siz = siz_chunk * chunks;
348348

349349
dispatch_queue_t q = dispatch_get_global_queue(0,0);
350350
dispatch_group_t g = dispatch_group_create();
351-
__block size_t siz_acc = 0, deliveries = 0;
351+
volatile __block int32_t deliveries = 0;
352+
__block size_t siz_acc = 0;
352353
__block void (^b)(dispatch_data_t, int);
353354
b = Block_copy(^(dispatch_data_t data, int err) {
354355
if (err) {
355356
test_errno("dispatch_read", err, 0);
356357
test_stop();
357358
}
358-
deliveries++;
359+
OSAtomicIncrement32(&deliveries);
359360
siz_acc += dispatch_data_get_size(data);
360361
if (siz_acc < siz) {
361362
dispatch_group_enter(g);
@@ -383,7 +384,7 @@ test_writes_reads_eagain(void) // rdar://problem/8333366
383384
close(in);
384385
close(*(fd+1));
385386
test_group_wait(g);
386-
test_long("dispatch_read deliveries", deliveries, chunks);
387+
test_int32("dispatch_read deliveries", deliveries, chunks);
387388
test_long("dispatch_read data size", siz_acc, siz);
388389
close(*fd);
389390
Block_release(b);

0 commit comments

Comments
 (0)