Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 594d295

Browse files
committedDec 16, 2023
Convert deprecated functions to use modern atomic variable handling
Replaced deprecated atomic functions with the recommended C11 equivalents.
1 parent ee39300 commit 594d295

21 files changed

+128
-187
lines changed
 

‎src/BlocksRuntime/runtime.c

+2-11
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,11 @@
3232
#define __has_builtin(builtin) 0
3333
#endif
3434

35-
#if __has_builtin(__sync_bool_compare_and_swap)
36-
#define OSAtomicCompareAndSwapInt(_Old, _New, _Ptr) \
37-
__sync_bool_compare_and_swap(_Ptr, _Old, _New)
38-
#else
39-
#define _CRT_SECURE_NO_WARNINGS 1
40-
#include <Windows.h>
35+
#include <stdatomic.h>
4136
static __inline bool OSAtomicCompareAndSwapInt(int oldi, int newi,
4237
int volatile *dst) {
43-
// fixme barrier is overkill -- see objc-os.h
44-
int original = InterlockedCompareExchange((LONG volatile *)dst, newi, oldi);
45-
return (original == oldi);
38+
return atomic_compare_exchange_weak((_Atomic(int)*)dst, &oldi, newi);
4639
}
47-
#endif
48-
4940
/***********************
5041
Globals
5142
************************/

‎src/allocator.c

+19-17
Original file line numberDiff line numberDiff line change
@@ -542,31 +542,33 @@ _dispatch_alloc_maybe_madvise_page(dispatch_continuation_t c)
542542
}
543543
// They are all unallocated, so we could madvise the page. Try to
544544
// take ownership of them all.
545-
int last_locked = 0;
546-
do {
547-
if (!os_atomic_cmpxchg(&page_bitmaps[last_locked], BITMAP_C(0),
545+
for (i = 0; i < BITMAPS_PER_PAGE; i++) {
546+
if (!os_atomic_cmpxchg(&page_bitmaps[i], BITMAP_C(0),
548547
BITMAP_ALL_ONES, relaxed)) {
549548
// We didn't get one; since there is a cont allocated in
550549
// the page, we can't madvise. Give up and unlock all.
551-
goto unlock;
550+
break;
552551
}
553-
} while (++last_locked < (signed)BITMAPS_PER_PAGE);
552+
}
553+
554+
if (i >= BITMAPS_PER_PAGE) {
554555
#if DISPATCH_DEBUG
555-
//fprintf(stderr, "%s: madvised page %p for cont %p (next = %p), "
556-
// "[%u+1]=%u bitmaps at %p\n", __func__, page, c, c->do_next,
557-
// last_locked-1, BITMAPS_PER_PAGE, &page_bitmaps[0]);
558-
// Scribble to expose use-after-free bugs
559-
// madvise (syscall) flushes these stores
560-
memset(page, DISPATCH_ALLOCATOR_SCRIBBLE, DISPATCH_ALLOCATOR_PAGE_SIZE);
556+
// fprintf(stderr, "%s: madvised page %p for cont %p (next = %p), "
557+
// "[%u+1]=%u bitmaps at %p\n", __func__, page, c, c->do_next,
558+
// last_locked-1, BITMAPS_PER_PAGE, &page_bitmaps[0]);
559+
// Scribble to expose use-after-free bugs
560+
// madvise (syscall) flushes these stores
561+
memset(page, DISPATCH_ALLOCATOR_SCRIBBLE, DISPATCH_ALLOCATOR_PAGE_SIZE);
561562
#endif
562-
(void)dispatch_assume_zero(madvise(page, DISPATCH_ALLOCATOR_PAGE_SIZE,
563-
MADV_FREE));
563+
// madvise the page
564+
(void)dispatch_assume_zero(madvise(page, DISPATCH_ALLOCATOR_PAGE_SIZE,
565+
MADV_FREE));
566+
}
564567

565-
unlock:
566-
while (last_locked > 1) {
567-
page_bitmaps[--last_locked] = BITMAP_C(0);
568+
while (i > 1) {
569+
page_bitmaps[--i] = BITMAP_C(0);
568570
}
569-
if (last_locked) {
571+
if (i) {
570572
os_atomic_store(&page_bitmaps[0], BITMAP_C(0), relaxed);
571573
}
572574
return;

‎tests/Foundation/bench.mm

+33-35
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ virtual void virtfunc(void) {
8383
return arg;
8484
}
8585

86-
static volatile int32_t global;
87-
static volatile int64_t w_global;
86+
static atomic_int global;
87+
static _Atomic(int64_t) w_global;
8888

8989
#if TARGET_OS_EMBEDDED
9090
static const size_t cnt = 5000000;
@@ -191,7 +191,7 @@ static void __attribute__((noinline))
191191
main(void)
192192
{
193193
pthread_mutex_t plock = PTHREAD_MUTEX_INITIALIZER;
194-
OSSpinLock slock = OS_SPINLOCK_INIT;
194+
os_unfair_lock slock = OS_UNFAIR_LOCK_INIT;
195195
BasicObject *bo;
196196
BasicClass *bc;
197197
pthread_t pthr_pause;
@@ -219,8 +219,7 @@ static void __attribute__((noinline))
219219
cycles_per_nanosecond = (long double)freq / (long double)NSEC_PER_SEC;
220220

221221
#if BENCH_SLOW
222-
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
223-
assert(pool);
222+
@autoreleasepool {
224223
#endif
225224

226225
/* Malloc has different logic for threaded apps. */
@@ -371,9 +370,7 @@ static void __attribute__((noinline))
371370
}
372371
print_result2(s, "\"description\" ObjC call:");
373372

374-
[pool release];
375-
376-
pool = NULL;
373+
} // For the autorelease pool
377374
#endif
378375

379376
s = mach_absolute_time();
@@ -554,30 +551,30 @@ __asm__ __volatile__ ("svc 0x80" : "+r" (_r0)
554551

555552
s = mach_absolute_time();
556553
for (i = cnt; i; i--) {
557-
__sync_lock_test_and_set(&global, 0);
554+
atomic_xchg(&global, 0);
558555
}
559556
print_result(s, "Atomic xchg:");
560557

561558
s = mach_absolute_time();
562559
for (i = cnt; i; i--) {
563-
__sync_val_compare_and_swap(&global, 1, 0);
560+
atomic_cmpxchg(&global, 1, 0);
564561
}
565562
print_result(s, "Atomic cmpxchg:");
566563

567564
s = mach_absolute_time();
568565
for (i = cnt; i; i--) {
569-
__sync_fetch_and_add(&global, 1);
566+
atomic_fetch_add(&global, 1);
570567
}
571568
print_result(s, "Atomic increment:");
572569

573570
{
574-
global = 0;
575-
volatile int32_t *g = &global;
571+
global = ATOMIC_VAR_INIT(0);
572+
atomic_int *g = &global;
576573

577574
s = mach_absolute_time();
578575
for (i = cnt; i; i--) {
579576
uint32_t result;
580-
__sync_and_and_fetch(g, 1);
577+
atomic_fetch_and(g, 1);
581578
result = *g;
582579
if (result) {
583580
abort();
@@ -587,57 +584,58 @@ __asm__ __volatile__ ("svc 0x80" : "+r" (_r0)
587584
}
588585

589586
{
590-
global = 0;
591-
volatile int32_t *g = &global;
587+
global = ATOMIC_VAR_INIT(0);
588+
atomic_int *g = &global;
592589

593590
s = mach_absolute_time();
594591
for (i = cnt; i; i--) {
595592
uint32_t result;
596-
result = __sync_and_and_fetch(g, 1);
593+
result = atomic_fetch_and(g, 1);
597594
if (result) {
598595
abort();
599596
}
600597
}
601598
print_result(s, "Atomic and-and-fetch, using result:");
602599
}
603600

604-
global = 0;
601+
global = ATOMIC_VAR_INIT(0);
605602

606603
s = mach_absolute_time();
607604
for (i = cnt; i; i--) {
608-
OSAtomicIncrement32Barrier(&global);
605+
__c11_atomic_fetch_add(&global, 1, memory_order_seq_cst);
609606
}
610-
print_result(s, "OSAtomicIncrement32Barrier:");
607+
print_result(s, "atomic_fetch_add with memory_order_seq_cst barrier:");
611608

612-
global = 0;
609+
global = ATOMIC_VAR_INIT(0);
613610

614611
s = mach_absolute_time();
615612
for (i = cnt; i; i--) {
616-
OSAtomicIncrement32(&global);
613+
__c11_atomic_fetch_add(&global, 1, memory_order_relaxed);
617614
}
618-
print_result(s, "OSAtomicIncrement32:");
615+
print_result(s, "atomic_fetch_add with memory_order_relaxed barrier:");
619616

620-
w_global = 0;
617+
w_global = ATOMIC_VAR_INIT(0);
621618

622619
s = mach_absolute_time();
623620
for (i = cnt; i; i--) {
624-
OSAtomicIncrement64Barrier(&w_global);
621+
__c11_atomic_fetch_add(&wglobal, 1, memory_order_seq_cst);
625622
}
626-
print_result(s, "OSAtomicIncrement64Barrier:");
623+
print_result(s, "64-bit atomic_fetch_add with memory_order_seq_cst barrier:");
627624

628-
w_global = 0;
625+
w_global = ATOMIC_VAR_INIT(0);
629626

630627
s = mach_absolute_time();
631628
for (i = cnt; i; i--) {
632-
OSAtomicIncrement64(&w_global);
629+
__c11_atomic_fetch_add(&wglobal, 1, memory_order_relaxed);
633630
}
634-
print_result(s, "OSAtomicIncrement64:");
631+
print_result(s, "64-bit atomic_fetch_add with memory_order_seq_cst barrier:");
635632

636-
global = 0;
633+
global = ATOMIC_VAR_INIT(0);
637634

638635
s = mach_absolute_time();
639636
for (i = cnt; i; i--) {
640-
while (!__sync_bool_compare_and_swap(&global, 0, 1)) {
637+
atomic_int zero = ATOMIC_VAR_INIT(0);
638+
while (!atomic_compare_exchange_weak(&global, &zero, 1)) {
641639
do {
642640
#if defined(__i386__) || defined(__x86_64__)
643641
__asm__ __volatile__ ("pause");
@@ -646,16 +644,16 @@ __asm__ __volatile__ ("svc 0x80" : "+r" (_r0)
646644
#endif
647645
} while (global);
648646
}
649-
global = 0;
647+
global = ATOMIC_VAR_INIT(0);
650648
}
651649
print_result(s, "Inlined spin lock/unlock:");
652650

653651
s = mach_absolute_time();
654652
for (i = cnt; i; i--) {
655-
OSSpinLockLock(&slock);
656-
OSSpinLockUnlock(&slock);
653+
os_unfair_lock_lock(&slock);
654+
os_unfair_lock_unlock(&slock);
657655
}
658-
print_result(s, "OSSpinLock/Unlock:");
656+
print_result(s, "os_unfair_lock_lock/unlock:");
659657

660658
s = mach_absolute_time();
661659
for (i = cnt; i; i--) {

‎tests/Foundation/dispatch_apply_gc.m

+5-6
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,25 @@
3030
#else
3131
const size_t final = 1000, desclen = 8892;
3232
#endif
33-
NSAutoreleasePool *pool = nil;
3433

3534
static void
3635
work(void* ctxt __attribute__((unused)))
3736
{
38-
pool = [[NSAutoreleasePool alloc] init];
37+
@autoreleasepool {
3938
NSMutableArray *a = [NSMutableArray array];
40-
OSSpinLock sl = OS_SPINLOCK_INIT, *l = &sl;
39+
os_unfair_lock sl = OS_UNFAIR_LOCK_INIT, *l = &sl;
4140

4241
dispatch_apply(final, dispatch_get_global_queue(0, 0), ^(size_t i){
4342
NSDecimalNumber *n = [NSDecimalNumber decimalNumberWithDecimal:
4443
[[NSNumber numberWithInteger:i] decimalValue]];
45-
OSSpinLockLock(l);
44+
os_unfair_lock_lock(l);
4645
[a addObject:n];
47-
OSSpinLockUnlock(l);
46+
os_unfair_lock_unlock(l);
4847
});
4948
test_long("count", [a count], final);
5049
test_long("description length", [[a description] length], desclen);
5150
a = nil;
52-
[pool drain];
51+
}
5352
test_stop_after_delay((void*)(intptr_t)1);
5453
}
5554

‎tests/Foundation/nsoperation.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ - (void)main
5454
{
5555
dispatch_test_start("NSOperation");
5656

57-
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
57+
@ autoreleasepool {
5858

5959
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
6060
test_ptr_notnull("NSOperationQueue", queue);
@@ -67,7 +67,7 @@ - (void)main
6767

6868
[[NSRunLoop mainRunLoop] run];
6969

70-
[pool release];
70+
}
7171

7272
return 0;
7373
}

‎tests/dispatch_after.c

-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
#endif
2626
#include <stdlib.h>
2727
#include <assert.h>
28-
#ifdef __APPLE__
29-
#include <libkern/OSAtomic.h>
30-
#endif
3128

3229
#include <bsdtests.h>
3330
#include <Block.h>

‎tests/dispatch_apply.c

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@
3232
#endif
3333
#include <stdlib.h>
3434
#include <assert.h>
35-
#ifdef __APPLE__
36-
#include <libkern/OSAtomic.h>
37-
#endif
35+
#include <stdatomic.h>
3836
#include <sys/types.h>
3937

4038
#include <bsdtests.h>
4139
#include "dispatch_test.h"
4240

43-
static volatile int32_t busy_threads_started, busy_threads_finished;
41+
static atomic_int busy_threads_started, busy_threads_finished;
4442

4543
/*
4644
* Keep a thread busy, spinning on the CPU.
@@ -57,7 +55,7 @@ static void busythread(void *ignored)
5755
/* prevent i and j been optimized out */
5856
volatile uint64_t i = 0, j = 0;
5957

60-
OSAtomicIncrement32(&busy_threads_started);
58+
__c11_atomic_fetch_add(&busy_threads_started, 1, memory_order_relaxed);
6159

6260
while(!all_done)
6361
{
@@ -67,7 +65,7 @@ static void busythread(void *ignored)
6765
}
6866
(void)j;
6967

70-
OSAtomicIncrement32(&busy_threads_finished);
68+
__c11_atomic_fetch_add(&busy_threads_finished, 1, memory_order_relaxed);
7169
}
7270

7371
/*
@@ -103,12 +101,13 @@ static void test_apply_contended(dispatch_queue_t dq)
103101
usleep(1);
104102
}
105103

106-
volatile __block int32_t count = 0;
104+
105+
__block atomic_int count = ATOMIC_VAR_INIT(0);
107106
const int32_t final = 32;
108107

109108
int32_t before = busy_threads_started;
110109
dispatch_apply(final, dq, ^(size_t i __attribute__((unused))) {
111-
OSAtomicIncrement32(&count);
110+
__c11_atomic_fetch_add(&count, 1, memory_order_relaxed);
112111
});
113112
int32_t after = busy_threads_finished;
114113

@@ -129,22 +128,22 @@ main(void)
129128
{
130129
dispatch_test_start("Dispatch Apply");
131130

132-
volatile __block int32_t count = 0;
131+
__block atomic_int count = ATOMIC_VAR_INIT(0);
133132
const int32_t final = 32;
134133

135134
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
136135
test_ptr_notnull("dispatch_get_global_queue", queue);
137136

138137
dispatch_apply(final, queue, ^(size_t i __attribute__((unused))) {
139-
OSAtomicIncrement32(&count);
138+
__c11_atomic_fetch_add(&count, 1, memory_order_relaxed);
140139
});
141140
test_long("count", count, final);
142141

143142
count = 0; // rdar://problem/9294578
144143
dispatch_apply(final, queue, ^(size_t i __attribute__((unused))) {
145144
dispatch_apply(final, queue, ^(size_t ii __attribute__((unused))) {
146145
dispatch_apply(final, queue, ^(size_t iii __attribute__((unused))) {
147-
OSAtomicIncrement32(&count);
146+
__c11_atomic_fetch_add(&count, 1, memory_order_relaxed);
148147
});
149148
});
150149
});

‎tests/dispatch_cascade.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ cascade(void* context)
108108
dispatch_async_f(queues[idx], context, cascade);
109109
}
110110

111-
if (__sync_sub_and_fetch(&iterations, 1) == 0) {
111+
if (atomic_fetch_sub(&iterations, 1, memory_order_relaxed) - 1 == 0) {
112112
done = 1;
113113
histogram();
114114
dispatch_async_f(dispatch_get_main_queue(), NULL, cleanup);

‎tests/dispatch_cf_main.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@
2121
#include <dispatch/dispatch.h>
2222
#include <stdio.h>
2323
#include <CoreFoundation/CoreFoundation.h>
24-
#ifdef __APPLE__
25-
#include <libkern/OSAtomic.h>
26-
#endif
24+
#include <stdatomic.h>
2725

2826
#include <bsdtests.h>
2927
#include "dispatch_test.h"
3028

3129
const int32_t final = 10;
32-
static volatile int32_t count;
30+
static atomic_int count = ATOMIC_VAR_INIT(0);
3331

3432
static void
3533
work(void* ctxt __attribute__((unused)))
3634
{
37-
int32_t c = OSAtomicIncrement32(&count);
35+
int32_t c = __c11_atomic_fetch_add(&count, 1, memory_order_relaxed)+1;
3836
if (c < final-1) {
3937
dispatch_async_f(dispatch_get_main_queue(), NULL, work);
4038
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopDefaultMode, ^{

‎tests/dispatch_concur.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include <bsdtests.h>
3838
#include "dispatch_test.h"
3939

40-
static volatile size_t done, concur;
40+
static _Atomic(size_t) done, concur;
4141
static int use_group_async;
4242
static uint32_t activecpu;
4343
static uint32_t min_acceptable_concurrency;
@@ -57,15 +57,15 @@ static void
5757
work(void* ctxt __attribute__((unused)))
5858
{
5959
usleep(1000);
60-
__sync_add_and_fetch(&done, 1);
60+
atomic_fetch_add(&done, 1, memory_order_relaxed);
6161

6262
if (!use_group_async) dispatch_group_leave(gw);
6363
}
6464

6565
static void
6666
submit_work(void* ctxt)
6767
{
68-
size_t c = __sync_add_and_fetch(&concur, 1), *m = (size_t *)ctxt, i;
68+
size_t c =atomic_fetch_add(&concur, 1, memory_order_relaxed) + 1, *m = (size_t *)ctxt, i;
6969
if (c > *m) *m = c;
7070

7171
for (i = 0; i < workers; ++i) {
@@ -78,7 +78,7 @@ submit_work(void* ctxt)
7878
}
7979

8080
usleep(10000);
81-
__sync_sub_and_fetch(&concur, 1);
81+
atomic_fetch_sub(&concur, 1, memory_order_relaxed);
8282

8383
if (!use_group_async) dispatch_group_leave(g);
8484
}
@@ -132,11 +132,11 @@ test_concur_async(size_t n, size_t qw)
132132
static void
133133
sync_work(void* ctxt)
134134
{
135-
size_t c = __sync_add_and_fetch(&concur, 1), *m = (size_t *)ctxt;
135+
size_t c = atomic_fetch_add(&concur, 1, memory_order_relaxed) + 1, *m = (size_t *)ctxt;
136136
if (c > *m) *m = c;
137137

138138
usleep(10000);
139-
__sync_sub_and_fetch(&concur, 1);
139+
atomic_fetch_sub(&concur, 1, memory_order_relaxed);
140140
}
141141

142142
static void
@@ -172,11 +172,11 @@ test_concur_sync(size_t n, size_t qw)
172172
static void
173173
apply_work(void* ctxt, size_t i)
174174
{
175-
size_t c = __sync_add_and_fetch(&concur, 1), *m = ((size_t *)ctxt) + i;
175+
size_t c = atomic_fetch_add(&concur, 1, memory_order_relaxed) + 1, *m = ((size_t *)ctxt) + i;
176176
if (c > *m) *m = c;
177177

178178
usleep(100000);
179-
__sync_sub_and_fetch(&concur, 1);
179+
atomic_fetch_sub(&concur, 1, memory_order_relaxed);
180180
}
181181

182182
static void

‎tests/dispatch_context_for_key.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <dispatch/dispatch.h>
2222
#include <stdlib.h>
2323
#include <stdio.h>
24+
#include <stdatomic.h>
2425
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
2526
#include <unistd.h>
2627
#endif
@@ -33,14 +34,14 @@
3334

3435
static const char *ctxts[] = {"ctxt for app", "ctxt for key 1",
3536
"ctxt for key 2", "ctxt for key 1 bis", "ctxt for key 4"};
36-
volatile long ctxts_destroyed;
37+
atomic_long ctxts_destroyed;
3738
static dispatch_group_t g;
3839

3940
static void
4041
destructor(void *ctxt)
4142
{
4243
fprintf(stderr, "destructor of %s\n", (char*)ctxt);
43-
(void)__sync_add_and_fetch(&ctxts_destroyed, 1);
44+
atomic_fetch_sub(&ctxts_destroyed, 1, memory_order_relaxed);
4445
dispatch_group_leave(g);
4546
}
4647

‎tests/dispatch_deadname.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ test_mach_debug_port(mach_port_t name, const char *str, unsigned int line)
9898
#endif
9999

100100
static dispatch_group_t g;
101-
static volatile long sent, received;
101+
static atomic_long sent, received;
102102

103103
void
104104
test_dead_name(void)
@@ -202,7 +202,7 @@ test_receive_and_dead_name(void)
202202
ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV, mp, 0,
203203
dispatch_get_global_queue(0, 0));
204204
dispatch_source_set_event_handler(ds, ^{
205-
__sync_add_and_fetch(&received, 1);
205+
atomic_fetch_add(&received, 1, memory_order_relaxed);
206206
usleep(100000); // rdar://problem/7676437 race with send source re-arm
207207
mach_msg_empty_rcv_t msg = { .header = {
208208
.msgh_size = sizeof(mach_msg_empty_rcv_t),
@@ -263,7 +263,7 @@ send_until_timeout(mach_port_t mp)
263263
test_mach_error("mach_msg(MACH_SEND_MSG)", kr, KERN_SUCCESS);
264264
if (kr) test_stop();
265265
}
266-
} while (!kr && __sync_add_and_fetch(&sent, 1) < TEST_SP_MSGCOUNT);
266+
} while (!kr && (atomic_fetch_add(&sent, 1, memory_order_relaxed) + 1) < TEST_SP_MSGCOUNT);
267267
test_mach_debug_port(mp);
268268
return kr;
269269
}
@@ -275,8 +275,8 @@ test_send_possible(void) // rdar://problem/8758200
275275
kern_return_t kr;
276276
mach_port_t mp;
277277

278-
sent = 0;
279-
received = 0;
278+
sent = ATOMIC_VAR_INIT(0);
279+
received = ATOMIC_VAR_INIT(0);
280280
dispatch_group_enter(g);
281281
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
282282
test_mach_error("mach_port_allocate", kr, KERN_SUCCESS);
@@ -338,7 +338,7 @@ test_send_possible(void) // rdar://problem/8758200
338338
test_mach_error("mach_msg(MACH_RCV_MSG)", kr, KERN_SUCCESS);
339339
if (kr) test_stop();
340340
}
341-
} while (!kr && __sync_add_and_fetch(&received, 1));
341+
} while (!kr && (atomic_fetch_add(&received, 1, memory_order_relaxed) + 1));
342342
test_mach_debug_port(mp);
343343
});
344344
dispatch_source_set_cancel_handler(ds, ^{
@@ -390,7 +390,7 @@ static boolean_t
390390
test_mig_callback(mach_msg_header_t *message __attribute__((unused)),
391391
mach_msg_header_t *reply)
392392
{
393-
__sync_add_and_fetch(&received, 1);
393+
atomic_fetch_add(&received, 1, memory_order_relaxed);
394394
reply->msgh_remote_port = 0;
395395
return false;
396396
}
@@ -402,7 +402,7 @@ test_mig_server_large_msg(void) // rdar://problem/8422992
402402
kern_return_t kr;
403403
mach_port_t mp;
404404

405-
received = 0;
405+
received = ATOMIC_VAR_INIT(0);
406406
dispatch_group_enter(g);
407407
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
408408
test_mach_error("mach_port_allocate", kr, KERN_SUCCESS);

‎tests/dispatch_group.c

-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
#include <stdio.h>
2727
#include <stdlib.h>
2828
#include <assert.h>
29-
#ifdef __APPLE__
30-
#include <libkern/OSAtomic.h>
31-
#endif
3229
#include <math.h>
3330
#include <bsdtests.h>
3431
#include "dispatch_test.h"

‎tests/dispatch_overcommit.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
#endif
3131
#include <stdlib.h>
3232
#include <assert.h>
33-
#ifdef __APPLE__
34-
#include <libkern/OSAtomic.h>
35-
#endif
33+
#include <stdatomic.h>
3634

3735
#include <bsdtests.h>
3836
#include "dispatch_test.h"
3937

40-
int32_t count = 0;
38+
atomic_int count = ATOMIC_VAR_INIT(0);
4139
const int32_t final = 32;
4240

4341
int
@@ -56,7 +54,7 @@ main(void)
5654
dispatch_set_target_queue(queue, dispatch_get_global_queue(0, DISPATCH_QUEUE_OVERCOMMIT));
5755

5856
dispatch_async(queue, ^{
59-
OSAtomicIncrement32(&count);
57+
__c11_atomic_fetch_add(&count, 1, memory_order_relaxed);
6058
if (count == final) {
6159
test_long("count", count, final);
6260
test_stop();

‎tests/dispatch_priority.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <bsdtests.h>
4242
#include "dispatch_test.h"
4343

44-
static volatile int done;
44+
static atomic_int done;
4545

4646
#ifdef DISPATCH_QUEUE_PRIORITY_BACKGROUND // <rdar://problem/7439794>
4747
#define USE_BACKGROUND_PRIORITY 1
@@ -75,7 +75,7 @@ static union {
7575
char padding[64];
7676
} counts[PRIORITIES];
7777

78-
static volatile long iterations;
78+
static atomic_long iterations;
7979
static long total;
8080
static size_t prio0, priorities = PRIORITIES;
8181

@@ -143,13 +143,13 @@ cpubusy(void* context)
143143
if (done) break;
144144
}
145145

146-
volatile long *count = context;
147-
long iterdone = __sync_sub_and_fetch(&iterations, 1);
146+
atomic_long * count = context;
147+
long iterdone = atomic_fetch_sub(&iterations, 1, memory_order_relaxed) - 1;
148148

149149
if (iterdone >= 0) {
150-
__sync_add_and_fetch(count, 1);
150+
atomic_fetch_add(count, 1, memory_order_relaxed);
151151
if (!iterdone) {
152-
__sync_add_and_fetch(&done, 1);
152+
atomic_fetch_add(done, 1, memory_order_relaxed);
153153
usleep(100000);
154154
histogram();
155155
dispatch_time_t delay = DISPATCH_TIME_NOW;
@@ -165,7 +165,7 @@ submit_work(dispatch_queue_t queue, void* context)
165165
{
166166
int i;
167167

168-
for (i = n_blocks(); i; --i) {
168+
for (i = n_blocks(); i > 0; --i) {
169169
dispatch_async_f(queue, context, cpubusy);
170170
}
171171

‎tests/dispatch_proc.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
#include <assert.h>
2828
#include <spawn.h>
2929
#include <signal.h>
30-
#ifdef __APPLE__
31-
#include <libkern/OSAtomic.h>
32-
#endif
30+
#include <stdatomic.h>
3331

3432
#include <bsdtests.h>
3533
#include "dispatch_test.h"

‎tests/dispatch_readsync.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,43 @@
4848
#endif
4949

5050
static dispatch_group_t g;
51-
static volatile size_t r_count, w_count, workers, readers, writers, crw, count, drain;
51+
static _Atomic(size_t) r_count, w_count, workers, readers, writers, crw, count, drain;
5252

5353
static void
5454
writer(void *ctxt)
5555
{
56-
size_t w = __sync_add_and_fetch(&writers, 1), *m = (size_t *)ctxt;
56+
size_t w = atomic_fetch_add(&writers, 1, memory_order_relaxed) + 1, *m = (size_t *)ctxt;
5757
if (w > *m) *m = w;
5858

5959
usleep(10000);
6060
size_t busy = BUSY;
61-
while (busy--) if (readers) __sync_add_and_fetch(&crw, 1);
61+
while (busy--) if (readers) atomic_fetch_add(&crw, 1, memory_order_relaxed);
6262

63-
if (__sync_sub_and_fetch(&w_count, 1) == 0) {
63+
if (atomic_fetch_sub(&w_count, 1, memory_order_relaxed) == 0) {
6464
if (r_count == 0) {
6565
dispatch_async(dispatch_get_main_queue(), ^{test_stop();});
6666
}
6767
}
68-
__sync_sub_and_fetch(&writers, 1);
68+
atomic_fetch_sub(&writers, 1, memory_order_relaxed)
6969
dispatch_group_leave(g);
7070
}
7171

7272
static void
7373
reader(void *ctxt)
7474
{
75-
size_t r = __sync_add_and_fetch(&readers, 1), *m = (size_t *)ctxt;
75+
size_t r = atomic_fetch_add(&readers, 1, memory_order_relaxed) + 1, *m = (size_t *)ctxt;
7676
if (r > *m) *m = r;
7777

7878
usleep(10000);
7979
size_t busy = BUSY;
80-
while (busy--) if (writers) __sync_add_and_fetch(&crw, 1);
80+
while (busy--) if (writers) atomic_fetch_add(&crw, 1, memory_order_relaxed);
8181

82-
if (__sync_sub_and_fetch(&r_count, 1) == 0) {
82+
if (atomic_fetch_sub(&r_count, 1, memory_order_relaxed) - 1 == 0) {
8383
if (r_count == 0) {
8484
dispatch_async(dispatch_get_main_queue(), ^{test_stop();});
8585
}
8686
}
87-
__sync_sub_and_fetch(&readers, 1);
87+
atomic_fetch_sub(&readers, 1, memory_order_relaxed);
8888
}
8989

9090
static void
@@ -101,12 +101,12 @@ test_readsync(dispatch_queue_t rq, dispatch_queue_t wq, size_t n)
101101
dispatch_group_async(g,
102102
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,
103103
DISPATCH_QUEUE_OVERCOMMIT), ^{
104-
__sync_add_and_fetch(&workers, 1);
104+
atomic_fetch_add(&workers, 1, memory_order_relaxed);
105105
do {
106106
usleep(100000);
107107
} while (workers < n);
108108
for (;;) {
109-
size_t idx = __sync_add_and_fetch(&count, 1);
109+
size_t idx = atomic_fetch_add(&count, 1, memory_order_relaxed) + 1;
110110
if (idx > LAPS) break;
111111
dispatch_sync_f(rq, mr, reader);
112112
if (!(idx % INTERVAL)) {
@@ -116,10 +116,10 @@ test_readsync(dispatch_queue_t rq, dispatch_queue_t wq, size_t n)
116116
dispatch_sync_f(rq, mr, reader);
117117
if (!(idx % (INTERVAL*10))) {
118118
// Let the queue drain
119-
__sync_add_and_fetch(&drain, 1);
119+
atomic_fetch_sub(&drain, 1, memory_order_relaxed);
120120
usleep(10000);
121121
dispatch_barrier_sync(wq, ^{});
122-
__sync_sub_and_fetch(&drain, 1);
122+
atomic_fetch_sub(&drain, 1, memory_order_relaxed);
123123
} else while (drain) usleep(1000);
124124
}
125125
});

‎tests/dispatch_timer_short.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#include <stdlib.h>
2222
#include <assert.h>
2323
#include <stdio.h>
24+
#include <stdatomic.h>
2425
#include <string.h>
2526
#include <math.h>
2627
#ifdef __APPLE__
2728
#include <mach/mach_time.h>
28-
#include <libkern/OSAtomic.h>
2929
#endif
3030

3131
#include <dispatch/dispatch.h>
@@ -42,7 +42,7 @@ static dispatch_source_t t[N];
4242
static dispatch_queue_t q;
4343
static dispatch_group_t g;
4444

45-
static volatile int32_t count;
45+
static atomic_uint count;
4646
static mach_timebase_info_data_t tbi;
4747
static uint64_t start, last;
4848

@@ -52,7 +52,7 @@ static
5252
void
5353
test_fin(void *cxt)
5454
{
55-
uint32_t finalCount = (uint32_t)count;
55+
unsigned int finalCount = count;
5656
fprintf(stderr, "Called back every %llu us on average\n",
5757
(delay/finalCount)/NSEC_PER_USEC);
5858
test_long_less_than("Frequency", 1,
@@ -110,7 +110,7 @@ test_short_timer(void)
110110
fprintf(stderr, "First timer callback (after %4llu ms)\n",
111111
elapsed_ms(start));
112112
}
113-
OSAtomicIncrement32(&count);
113+
__c11_atomic_fetch_add(&count, 1, memory_order_relaxed);
114114
if (elapsed_ms(last) >= 100) {
115115
fprintf(stderr, "%5d timer callbacks (after %4llu ms)\n", count,
116116
elapsed_ms(start));

‎tests/dispatch_vm.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
2525
#include <unistd.h>
2626
#endif
27-
#ifdef __APPLE__
28-
#include <libkern/OSAtomic.h>
29-
#endif
27+
#include <stdatomic.h>
3028
#include <assert.h>
3129
#ifdef __ANDROID__
3230
#include <linux/sysctl.h>
@@ -61,8 +59,8 @@
6159
#endif
6260

6361
static char **pages;
64-
static volatile int32_t handler_call_count;
65-
static volatile int32_t page_count;
62+
static atomic_int handler_call_count;
63+
static atomic_int page_count;
6664
static int32_t max_page_count;
6765
static dispatch_source_t vm_source;
6866
static dispatch_queue_t vm_queue;
@@ -140,7 +138,7 @@ main(void)
140138
test_skip("Memory pressure at start of test");
141139
cleanup();
142140
}
143-
if (OSAtomicIncrement32Barrier(&handler_call_count) != NOTIFICATIONS) {
141+
if (__c11_atomic_fetch_add(&handler_call_count, 1, memory_order_seq_cst) + 1 != NOTIFICATIONS) {
144142
log_msg("Ignoring vm pressure notification\n");
145143
interval = 1;
146144
return;
@@ -165,7 +163,8 @@ main(void)
165163
}
166164
bzero(p, ALLOC_SIZE);
167165
pages[page_count] = p;
168-
if (!(OSAtomicIncrement32Barrier(&page_count) % interval)) {
166+
__c11_atomic_fetch_add(&handler_call_count, 1, memory_order_seq_cst);
167+
if (!(__c11_atomic_fetch_add(&handler_call_count, 1, memory_order_seq_cst) + 1) % interval) {
169168
log_msg("Allocated %ldMB\n", pg2mb(page_count));
170169
usleep(200000);
171170
}

‎tests/generic_unix_port.h

-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
#include <limits.h>
22
#include <sys/param.h>
33

4-
static inline int32_t
5-
OSAtomicIncrement32(volatile int32_t *var)
6-
{
7-
return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_RELAXED)+1;
8-
}
9-
10-
static inline int32_t
11-
OSAtomicIncrement32Barrier(volatile int32_t *var)
12-
{
13-
return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_SEQ_CST)+1;
14-
}
15-
16-
static inline int32_t
17-
OSAtomicAdd32(int32_t val, volatile int32_t *var)
18-
{
19-
return __c11_atomic_fetch_add((_Atomic(int)*)var, val, __ATOMIC_RELAXED)+val;
20-
}
21-
224
// Simulation of mach_absolute_time related infrastructure
235
// For now, use gettimeofday.
246
// Consider using clockgettime(CLOCK_MONOTONIC) instead.

‎tests/generic_win_port.h

-18
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ struct mach_timebase_info {
2323
typedef struct mach_timebase_info *mach_timebase_info_t;
2424
typedef struct mach_timebase_info mach_timebase_info_data_t;
2525

26-
static inline int32_t
27-
OSAtomicIncrement32(volatile int32_t *var)
28-
{
29-
return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_RELAXED)+1;
30-
}
31-
32-
static inline int32_t
33-
OSAtomicIncrement32Barrier(volatile int32_t *var)
34-
{
35-
return __c11_atomic_fetch_add((_Atomic(int)*)var, 1, __ATOMIC_SEQ_CST)+1;
36-
}
37-
38-
static inline int32_t
39-
OSAtomicAdd32(int32_t val, volatile int32_t *var)
40-
{
41-
return __c11_atomic_fetch_add((_Atomic(int)*)var, val, __ATOMIC_RELAXED)+val;
42-
}
43-
4426
WCHAR *
4527
argv_to_command_line(char **argv);
4628

0 commit comments

Comments
 (0)
Please sign in to comment.