Skip to content

Commit 5fd7de8

Browse files
committed
Apply OSAtomic deprecation in iOS 10.0 patch from ReactiveCocoa#178
1 parent 1af6617 commit 5fd7de8

15 files changed

+50
-54
lines changed

ReactiveObjC/NSObject+RACPropertySubscribing.m

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#import "RACSubscriber.h"
1818
#import "RACSignal+Operations.h"
1919
#import "RACTuple.h"
20-
#import <libkern/OSAtomic.h>
2120

2221
@implementation NSObject (RACPropertySubscribing)
2322

ReactiveObjC/RACCommand.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
#import "RACScheduler.h"
1818
#import "RACSequence.h"
1919
#import "RACSignal+Operations.h"
20-
#import <libkern/OSAtomic.h>
20+
#import <stdatomic.h>
2121

2222
NSErrorDomain const RACCommandErrorDomain = @"RACCommandErrorDomain";
2323
NSString * const RACUnderlyingCommandErrorKey = @"RACUnderlyingCommandErrorKey";
2424

2525
@interface RACCommand () {
2626
// Atomic backing variable for `allowsConcurrentExecution`.
27-
volatile uint32_t _allowsConcurrentExecution;
27+
atomic_uint _allowsConcurrentExecution;
2828
}
2929

3030
/// A subject that sends added execution signals.
@@ -53,9 +53,9 @@ - (BOOL)allowsConcurrentExecution {
5353

5454
- (void)setAllowsConcurrentExecution:(BOOL)allowed {
5555
if (allowed) {
56-
OSAtomicOr32Barrier(1, &_allowsConcurrentExecution);
56+
atomic_fetch_or(&_allowsConcurrentExecution, 1);
5757
} else {
58-
OSAtomicAnd32Barrier(0, &_allowsConcurrentExecution);
58+
atomic_fetch_and(&_allowsConcurrentExecution, 0);
5959
}
6060

6161
[self.allowsConcurrentExecutionSubject sendNext:@(_allowsConcurrentExecution)];

ReactiveObjC/RACDisposable.m

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
#import "RACDisposable.h"
1010
#import "RACScopedDisposable.h"
11-
#import <libkern/OSAtomic.h>
11+
#import <stdatomic.h>
1212

1313
@interface RACDisposable () {
1414
// A copied block of type void (^)(void) containing the logic for disposal,
1515
// a pointer to `self` if no logic should be performed upon disposal, or
1616
// NULL if the receiver is already disposed.
1717
//
1818
// This should only be used atomically.
19-
void * volatile _disposeBlock;
19+
_Atomic(void *) _disposeBlock;
2020
}
2121

2222
@end
@@ -35,7 +35,7 @@ - (instancetype)init {
3535
self = [super init];
3636

3737
_disposeBlock = (__bridge void *)self;
38-
OSMemoryBarrier();
38+
atomic_thread_fence(memory_order_seq_cst);
3939

4040
return self;
4141
}
@@ -46,7 +46,7 @@ - (instancetype)initWithBlock:(void (^)(void))block {
4646
self = [super init];
4747

4848
_disposeBlock = (void *)CFBridgingRetain([block copy]);
49-
OSMemoryBarrier();
49+
atomic_thread_fence(memory_order_seq_cst);
5050

5151
return self;
5252
}
@@ -69,7 +69,7 @@ - (void)dispose {
6969

7070
while (YES) {
7171
void *blockPtr = _disposeBlock;
72-
if (OSAtomicCompareAndSwapPtrBarrier(blockPtr, NULL, &_disposeBlock)) {
72+
if (atomic_compare_exchange_strong(&_disposeBlock, &blockPtr, NULL)) {
7373
if (blockPtr != (__bridge void *)self) {
7474
disposeBlock = CFBridgingRelease(blockPtr);
7575
}

ReactiveObjC/RACDynamicSequence.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
#import "RACDynamicSequence.h"
10-
#import <libkern/OSAtomic.h>
10+
#import <stdatomic.h>
1111

1212
// Determines how RACDynamicSequences will be deallocated before the next one is
1313
// shifted onto the autorelease pool.
@@ -114,10 +114,10 @@ + (RACSequence *)sequenceWithLazyDependency:(id (^)(void))dependencyBlock headBl
114114
}
115115

116116
- (void)dealloc {
117-
static volatile int32_t directDeallocCount = 0;
117+
static atomic_int directDeallocCount = 0;
118118

119-
if (OSAtomicIncrement32(&directDeallocCount) >= DEALLOC_OVERFLOW_GUARD) {
120-
OSAtomicAdd32(-DEALLOC_OVERFLOW_GUARD, &directDeallocCount);
119+
if (atomic_fetch_add(&directDeallocCount, 1) + 1 >= DEALLOC_OVERFLOW_GUARD) {
120+
atomic_fetch_add(&directDeallocCount, -DEALLOC_OVERFLOW_GUARD);
121121

122122
// Put this sequence's tail onto the autorelease pool so we stop
123123
// recursing.

ReactiveObjC/RACDynamicSignal.m

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#import "RACPassthroughSubscriber.h"
1313
#import "RACScheduler+Private.h"
1414
#import "RACSubscriber.h"
15-
#import <libkern/OSAtomic.h>
1615

1716
@interface RACDynamicSignal ()
1817

ReactiveObjC/RACMulticastConnection.m

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import "RACDisposable.h"
1212
#import "RACSerialDisposable.h"
1313
#import "RACSubject.h"
14-
#import <libkern/OSAtomic.h>
14+
#import <stdatomic.h>
1515

1616
@interface RACMulticastConnection () {
1717
RACSubject *_signal;
@@ -24,7 +24,7 @@ @interface RACMulticastConnection () {
2424
//
2525
// If the swap is unsuccessful it means that `_sourceSignal` has already been
2626
// connected and the caller has no action to take.
27-
int32_t volatile _hasConnected;
27+
_Atomic(BOOL) _hasConnected;
2828
}
2929

3030
@property (nonatomic, readonly, strong) RACSignal *sourceSignal;
@@ -51,7 +51,8 @@ - (instancetype)initWithSourceSignal:(RACSignal *)source subject:(RACSubject *)s
5151
#pragma mark Connecting
5252

5353
- (RACDisposable *)connect {
54-
BOOL shouldConnect = OSAtomicCompareAndSwap32Barrier(0, 1, &_hasConnected);
54+
BOOL expected = NO;
55+
BOOL shouldConnect = atomic_compare_exchange_strong(&_hasConnected, &expected, YES);
5556

5657
if (shouldConnect) {
5758
self.serialDisposable.disposable = [self.sourceSignal subscribe:_signal];
@@ -61,19 +62,19 @@ - (RACDisposable *)connect {
6162
}
6263

6364
- (RACSignal *)autoconnect {
64-
__block volatile int32_t subscriberCount = 0;
65+
__block atomic_int subscriberCount = 0;
6566

6667
return [[RACSignal
6768
createSignal:^(id<RACSubscriber> subscriber) {
68-
OSAtomicIncrement32Barrier(&subscriberCount);
69+
atomic_fetch_add(&subscriberCount, 1);
6970

7071
RACDisposable *subscriptionDisposable = [self.signal subscribe:subscriber];
7172
RACDisposable *connectionDisposable = [self connect];
7273

7374
return [RACDisposable disposableWithBlock:^{
7475
[subscriptionDisposable dispose];
7576

76-
if (OSAtomicDecrement32Barrier(&subscriberCount) == 0) {
77+
if (atomic_fetch_sub(&subscriberCount, 1) - 1 == 0) {
7778
[connectionDisposable dispose];
7879
}
7980
}];

ReactiveObjC/RACSignal+Operations.m

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#import "RACSubscriber.h"
2727
#import "RACTuple.h"
2828
#import "RACUnit.h"
29-
#import <libkern/OSAtomic.h>
29+
#import <stdatomic.h>
3030
#import <objc/runtime.h>
3131

3232
NSErrorDomain const RACSignalErrorDomain = @"RACSignalErrorDomain";
@@ -646,7 +646,7 @@ - (RACDisposable *)setKeyPath:(NSString *)keyPath onObject:(NSObject *)object ni
646646

647647
// Purposely not retaining 'object', since we want to tear down the binding
648648
// when it deallocates normally.
649-
__block void * volatile objectPtr = (__bridge void *)object;
649+
__block _Atomic(void *) objectPtr = (__bridge void *)object;
650650

651651
RACDisposable *subscriptionDisposable = [self subscribeNext:^(id x) {
652652
// Possibly spec, possibly compiler bug, but this __bridge cast does not
@@ -698,7 +698,7 @@ - (RACDisposable *)setKeyPath:(NSString *)keyPath onObject:(NSObject *)object ni
698698

699699
while (YES) {
700700
void *ptr = objectPtr;
701-
if (OSAtomicCompareAndSwapPtrBarrier(ptr, NULL, &objectPtr)) {
701+
if (atomic_compare_exchange_strong(&objectPtr, &ptr, NULL)) {
702702
break;
703703
}
704704
}
@@ -1048,17 +1048,17 @@ - (RACSignal *)subscribeOn:(RACScheduler *)scheduler {
10481048

10491049
- (RACSignal *)deliverOnMainThread {
10501050
return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
1051-
__block volatile int32_t queueLength = 0;
1051+
__block atomic_int queueLength = 0;
10521052

10531053
void (^performOnMainThread)(dispatch_block_t) = ^(dispatch_block_t block) {
1054-
int32_t queued = OSAtomicIncrement32(&queueLength);
1054+
int32_t queued = atomic_fetch_add(&queueLength, 1) + 1;
10551055
if (NSThread.isMainThread && queued == 1) {
10561056
block();
1057-
OSAtomicDecrement32(&queueLength);
1057+
atomic_fetch_sub(&queueLength, 1);
10581058
} else {
10591059
dispatch_async(dispatch_get_main_queue(), ^{
10601060
block();
1061-
OSAtomicDecrement32(&queueLength);
1061+
atomic_fetch_sub(&queueLength, 1);
10621062
});
10631063
}
10641064
};

ReactiveObjC/RACSignal.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#import "RACSubject.h"
2222
#import "RACSubscriber+Private.h"
2323
#import "RACTuple.h"
24-
#import <libkern/OSAtomic.h>
24+
#import <stdatomic.h>
2525

2626
@implementation RACSignal
2727

@@ -108,12 +108,12 @@ - (RACSignal *)bind:(RACSignalBindBlock (^)(void))block {
108108
return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
109109
RACSignalBindBlock bindingBlock = block();
110110

111-
__block volatile int32_t signalCount = 1; // indicates self
111+
__block atomic_int signalCount = 1; // indicates self
112112

113113
RACCompoundDisposable *compoundDisposable = [RACCompoundDisposable compoundDisposable];
114114

115115
void (^completeSignal)(RACDisposable *) = ^(RACDisposable *finishedDisposable) {
116-
if (OSAtomicDecrement32Barrier(&signalCount) == 0) {
116+
if (atomic_fetch_sub(&signalCount, 1) - 1 == 0) {
117117
[subscriber sendCompleted];
118118
[compoundDisposable dispose];
119119
} else {
@@ -122,7 +122,7 @@ - (RACSignal *)bind:(RACSignalBindBlock (^)(void))block {
122122
};
123123

124124
void (^addSignal)(RACSignal *) = ^(RACSignal *signal) {
125-
OSAtomicIncrement32Barrier(&signalCount);
125+
atomic_fetch_add(&signalCount, 1);
126126

127127
RACSerialDisposable *selfDisposable = [[RACSerialDisposable alloc] init];
128128
[compoundDisposable addDisposable:selfDisposable];

ReactiveObjC/extobjc/EXTRuntimeExtensions.m

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#import <ctype.h>
1313
#import <Foundation/Foundation.h>
14-
#import <libkern/OSAtomic.h>
1514
#import <objc/message.h>
1615
#import <pthread.h>
1716
#import <stdio.h>

ReactiveObjCTests/RACMulticastConnectionSpec.m

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#import "RACSubscriber.h"
1616
#import "RACReplaySubject.h"
1717
#import "RACScheduler.h"
18-
#import <libkern/OSAtomic.h>
1918

2019
QuickSpecBegin(RACMulticastConnectionSpec)
2120

ReactiveObjCTests/RACSchedulerSpec.m

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#import "RACDisposable.h"
1616
#import <ReactiveObjC/EXTScope.h>
1717
#import "RACTestExampleScheduler.h"
18-
#import <libkern/OSAtomic.h>
1918

2019
// This shouldn't be used directly. Use the `expectCurrentSchedulers` block
2120
// below instead.

ReactiveObjCTests/RACSignalSpec.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#import "RACTestScheduler.h"
3434
#import "RACTuple.h"
3535
#import "RACUnit.h"
36-
#import <libkern/OSAtomic.h>
36+
#import <stdatomic.h>
3737

3838
// Set in a beforeAll below.
3939
static NSError *RACSignalTestError;
@@ -111,7 +111,7 @@ + (void)configure:(Configuration *)configuration {
111111
};
112112

113113
RACSignal *infiniteSignal = [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
114-
__block volatile int32_t done = 0;
114+
__block atomic_int done = 0;
115115

116116
[RACScheduler.mainThreadScheduler schedule:^{
117117
while (!done) {
@@ -120,7 +120,7 @@ + (void)configure:(Configuration *)configuration {
120120
}];
121121

122122
return [RACDisposable disposableWithBlock:^{
123-
OSAtomicIncrement32Barrier(&done);
123+
atomic_fetch_add(&done, 1);
124124
}];
125125
}];
126126

ReactiveObjCTests/RACSubjectSpec.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#import "RACSubscriberExamples.h"
1313

14-
#import <libkern/OSAtomic.h>
14+
#import <stdatomic.h>
1515
#import <ReactiveObjC/EXTScope.h>
1616
#import "RACBehaviorSubject.h"
1717
#import "RACCompoundDisposable.h"
@@ -255,10 +255,10 @@ - (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable {
255255

256256
// Just leak it, ain't no thang.
257257
__unsafe_unretained volatile id *values = (__unsafe_unretained id *)calloc(count, sizeof(*values));
258-
__block volatile int32_t nextIndex = 0;
258+
__block atomic_int nextIndex = 0;
259259

260260
[subject subscribeNext:^(NSNumber *value) {
261-
int32_t indexPlusOne = OSAtomicIncrement32(&nextIndex);
261+
int32_t indexPlusOne = atomic_fetch_add(&nextIndex, 1) + 1;
262262
values[indexPlusOne - 1] = value;
263263
}];
264264

@@ -276,7 +276,7 @@ - (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable {
276276
[subject sendCompleted];
277277
});
278278

279-
OSMemoryBarrier();
279+
atomic_thread_fence(memory_order_seq_cst);
280280

281281
NSArray *liveValues = [NSArray arrayWithObjects:(id *)values count:(NSUInteger)nextIndex];
282282
expect(liveValues).to(haveCount(@(count)));

ReactiveObjCTests/RACSubscriberSpec.m

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
#import "RACSubscriber.h"
1515
#import "RACSubscriber+Private.h"
16-
#import <libkern/OSAtomic.h>
16+
#import <stdatomic.h>
1717

1818
QuickSpecBegin(RACSubscriberSpec)
1919

2020
__block RACSubscriber *subscriber;
2121
__block NSMutableArray *values;
2222

23-
__block volatile BOOL finished;
24-
__block volatile int32_t nextsAfterFinished;
23+
__block _Atomic(BOOL) finished;
24+
__block atomic_int nextsAfterFinished;
2525

2626
__block BOOL success;
2727
__block NSError *error;
@@ -36,7 +36,7 @@
3636
error = nil;
3737

3838
subscriber = [RACSubscriber subscriberWithNext:^(id value) {
39-
if (finished) OSAtomicIncrement32Barrier(&nextsAfterFinished);
39+
if (finished) atomic_fetch_add(&nextsAfterFinished, 1);
4040

4141
[values addObject:value];
4242
} error:^(NSError *e) {
@@ -111,7 +111,7 @@
111111
[subscriber sendCompleted];
112112

113113
finished = YES;
114-
OSMemoryBarrier();
114+
atomic_thread_fence(memory_order_seq_cst);
115115
});
116116
});
117117

@@ -122,7 +122,7 @@
122122
[subscriber sendError:nil];
123123

124124
finished = YES;
125-
OSMemoryBarrier();
125+
atomic_thread_fence(memory_order_seq_cst);
126126
});
127127
});
128128
});

0 commit comments

Comments
 (0)