diff --git a/ReactiveObjC/NSObject+RACPropertySubscribing.m b/ReactiveObjC/NSObject+RACPropertySubscribing.m index 29e3df89e..d7b4b6294 100644 --- a/ReactiveObjC/NSObject+RACPropertySubscribing.m +++ b/ReactiveObjC/NSObject+RACPropertySubscribing.m @@ -17,7 +17,6 @@ #import "RACSubscriber.h" #import "RACSignal+Operations.h" #import "RACTuple.h" -#import @implementation NSObject (RACPropertySubscribing) diff --git a/ReactiveObjC/RACCommand.m b/ReactiveObjC/RACCommand.m index 4342f80ba..db4c6ba98 100644 --- a/ReactiveObjC/RACCommand.m +++ b/ReactiveObjC/RACCommand.m @@ -17,14 +17,14 @@ #import "RACScheduler.h" #import "RACSequence.h" #import "RACSignal+Operations.h" -#import +#import NSErrorDomain const RACCommandErrorDomain = @"RACCommandErrorDomain"; NSString * const RACUnderlyingCommandErrorKey = @"RACUnderlyingCommandErrorKey"; @interface RACCommand () { // Atomic backing variable for `allowsConcurrentExecution`. - volatile uint32_t _allowsConcurrentExecution; + atomic_uint _allowsConcurrentExecution; } /// A subject that sends added execution signals. @@ -53,9 +53,9 @@ - (BOOL)allowsConcurrentExecution { - (void)setAllowsConcurrentExecution:(BOOL)allowed { if (allowed) { - OSAtomicOr32Barrier(1, &_allowsConcurrentExecution); + atomic_fetch_or(&_allowsConcurrentExecution, 1); } else { - OSAtomicAnd32Barrier(0, &_allowsConcurrentExecution); + atomic_fetch_and(&_allowsConcurrentExecution, 0); } [self.allowsConcurrentExecutionSubject sendNext:@(_allowsConcurrentExecution)]; diff --git a/ReactiveObjC/RACDisposable.m b/ReactiveObjC/RACDisposable.m index 3cba07f4f..55e92964e 100644 --- a/ReactiveObjC/RACDisposable.m +++ b/ReactiveObjC/RACDisposable.m @@ -8,7 +8,7 @@ #import "RACDisposable.h" #import "RACScopedDisposable.h" -#import +#import @interface RACDisposable () { // A copied block of type void (^)(void) containing the logic for disposal, @@ -16,7 +16,7 @@ @interface RACDisposable () { // NULL if the receiver is already disposed. // // This should only be used atomically. - void * volatile _disposeBlock; + _Atomic(void *) _disposeBlock; } @end @@ -35,7 +35,7 @@ - (instancetype)init { self = [super init]; _disposeBlock = (__bridge void *)self; - OSMemoryBarrier(); + atomic_thread_fence(memory_order_seq_cst); return self; } @@ -46,7 +46,7 @@ - (instancetype)initWithBlock:(void (^)(void))block { self = [super init]; _disposeBlock = (void *)CFBridgingRetain([block copy]); - OSMemoryBarrier(); + atomic_thread_fence(memory_order_seq_cst); return self; } @@ -69,7 +69,7 @@ - (void)dispose { while (YES) { void *blockPtr = _disposeBlock; - if (OSAtomicCompareAndSwapPtrBarrier(blockPtr, NULL, &_disposeBlock)) { + if (atomic_compare_exchange_strong(&_disposeBlock, &blockPtr, NULL)) { if (blockPtr != (__bridge void *)self) { disposeBlock = CFBridgingRelease(blockPtr); } diff --git a/ReactiveObjC/RACDynamicSequence.m b/ReactiveObjC/RACDynamicSequence.m index 177a22514..863a48f1f 100644 --- a/ReactiveObjC/RACDynamicSequence.m +++ b/ReactiveObjC/RACDynamicSequence.m @@ -7,7 +7,7 @@ // #import "RACDynamicSequence.h" -#import +#import // Determines how RACDynamicSequences will be deallocated before the next one is // shifted onto the autorelease pool. @@ -114,10 +114,10 @@ + (RACSequence *)sequenceWithLazyDependency:(id (^)(void))dependencyBlock headBl } - (void)dealloc { - static volatile int32_t directDeallocCount = 0; + static atomic_int directDeallocCount = 0; - if (OSAtomicIncrement32(&directDeallocCount) >= DEALLOC_OVERFLOW_GUARD) { - OSAtomicAdd32(-DEALLOC_OVERFLOW_GUARD, &directDeallocCount); + if (atomic_fetch_add(&directDeallocCount, 1) + 1 >= DEALLOC_OVERFLOW_GUARD) { + atomic_fetch_add(&directDeallocCount, -DEALLOC_OVERFLOW_GUARD); // Put this sequence's tail onto the autorelease pool so we stop // recursing. diff --git a/ReactiveObjC/RACDynamicSignal.m b/ReactiveObjC/RACDynamicSignal.m index 3bee2f7c2..53d6d2e9e 100644 --- a/ReactiveObjC/RACDynamicSignal.m +++ b/ReactiveObjC/RACDynamicSignal.m @@ -12,7 +12,6 @@ #import "RACPassthroughSubscriber.h" #import "RACScheduler+Private.h" #import "RACSubscriber.h" -#import @interface RACDynamicSignal () diff --git a/ReactiveObjC/RACMulticastConnection.m b/ReactiveObjC/RACMulticastConnection.m index f59824500..3b6183535 100644 --- a/ReactiveObjC/RACMulticastConnection.m +++ b/ReactiveObjC/RACMulticastConnection.m @@ -11,7 +11,7 @@ #import "RACDisposable.h" #import "RACSerialDisposable.h" #import "RACSubject.h" -#import +#import @interface RACMulticastConnection () { RACSubject *_signal; @@ -24,7 +24,7 @@ @interface RACMulticastConnection () { // // If the swap is unsuccessful it means that `_sourceSignal` has already been // connected and the caller has no action to take. - int32_t volatile _hasConnected; + _Atomic(BOOL) _hasConnected; } @property (nonatomic, readonly, strong) RACSignal *sourceSignal; @@ -51,7 +51,8 @@ - (instancetype)initWithSourceSignal:(RACSignal *)source subject:(RACSubject *)s #pragma mark Connecting - (RACDisposable *)connect { - BOOL shouldConnect = OSAtomicCompareAndSwap32Barrier(0, 1, &_hasConnected); + BOOL expected = NO; + BOOL shouldConnect = atomic_compare_exchange_strong(&_hasConnected, &expected, YES); if (shouldConnect) { self.serialDisposable.disposable = [self.sourceSignal subscribe:_signal]; @@ -61,11 +62,11 @@ - (RACDisposable *)connect { } - (RACSignal *)autoconnect { - __block volatile int32_t subscriberCount = 0; + __block atomic_int subscriberCount = 0; return [[RACSignal createSignal:^(id subscriber) { - OSAtomicIncrement32Barrier(&subscriberCount); + atomic_fetch_add(&subscriberCount, 1); RACDisposable *subscriptionDisposable = [self.signal subscribe:subscriber]; RACDisposable *connectionDisposable = [self connect]; @@ -73,7 +74,7 @@ - (RACSignal *)autoconnect { return [RACDisposable disposableWithBlock:^{ [subscriptionDisposable dispose]; - if (OSAtomicDecrement32Barrier(&subscriberCount) == 0) { + if (atomic_fetch_sub(&subscriberCount, 1) - 1 == 0) { [connectionDisposable dispose]; } }]; diff --git a/ReactiveObjC/RACSignal+Operations.m b/ReactiveObjC/RACSignal+Operations.m index 87967adfd..15c10de5e 100644 --- a/ReactiveObjC/RACSignal+Operations.m +++ b/ReactiveObjC/RACSignal+Operations.m @@ -26,7 +26,7 @@ #import "RACSubscriber.h" #import "RACTuple.h" #import "RACUnit.h" -#import +#import #import NSErrorDomain const RACSignalErrorDomain = @"RACSignalErrorDomain"; @@ -646,7 +646,7 @@ - (RACDisposable *)setKeyPath:(NSString *)keyPath onObject:(NSObject *)object ni // Purposely not retaining 'object', since we want to tear down the binding // when it deallocates normally. - __block void * volatile objectPtr = (__bridge void *)object; + __block _Atomic(void *) objectPtr = (__bridge void *)object; RACDisposable *subscriptionDisposable = [self subscribeNext:^(id x) { // Possibly spec, possibly compiler bug, but this __bridge cast does not @@ -698,7 +698,7 @@ - (RACDisposable *)setKeyPath:(NSString *)keyPath onObject:(NSObject *)object ni while (YES) { void *ptr = objectPtr; - if (OSAtomicCompareAndSwapPtrBarrier(ptr, NULL, &objectPtr)) { + if (atomic_compare_exchange_strong(&objectPtr, &ptr, NULL)) { break; } } @@ -1048,17 +1048,17 @@ - (RACSignal *)subscribeOn:(RACScheduler *)scheduler { - (RACSignal *)deliverOnMainThread { return [[RACSignal createSignal:^(id subscriber) { - __block volatile int32_t queueLength = 0; + __block atomic_int queueLength = 0; void (^performOnMainThread)(dispatch_block_t) = ^(dispatch_block_t block) { - int32_t queued = OSAtomicIncrement32(&queueLength); + int32_t queued = atomic_fetch_add(&queueLength, 1) + 1; if (NSThread.isMainThread && queued == 1) { block(); - OSAtomicDecrement32(&queueLength); + atomic_fetch_sub(&queueLength, 1); } else { dispatch_async(dispatch_get_main_queue(), ^{ block(); - OSAtomicDecrement32(&queueLength); + atomic_fetch_sub(&queueLength, 1); }); } }; diff --git a/ReactiveObjC/RACSignal.m b/ReactiveObjC/RACSignal.m index 84607f681..eb79e6039 100644 --- a/ReactiveObjC/RACSignal.m +++ b/ReactiveObjC/RACSignal.m @@ -21,7 +21,7 @@ #import "RACSubject.h" #import "RACSubscriber+Private.h" #import "RACTuple.h" -#import +#import @implementation RACSignal @@ -108,12 +108,12 @@ - (RACSignal *)bind:(RACSignalBindBlock (^)(void))block { return [[RACSignal createSignal:^(id subscriber) { RACSignalBindBlock bindingBlock = block(); - __block volatile int32_t signalCount = 1; // indicates self + __block atomic_int signalCount = 1; // indicates self RACCompoundDisposable *compoundDisposable = [RACCompoundDisposable compoundDisposable]; void (^completeSignal)(RACDisposable *) = ^(RACDisposable *finishedDisposable) { - if (OSAtomicDecrement32Barrier(&signalCount) == 0) { + if (atomic_fetch_sub(&signalCount, 1) - 1 == 0) { [subscriber sendCompleted]; [compoundDisposable dispose]; } else { @@ -122,7 +122,7 @@ - (RACSignal *)bind:(RACSignalBindBlock (^)(void))block { }; void (^addSignal)(RACSignal *) = ^(RACSignal *signal) { - OSAtomicIncrement32Barrier(&signalCount); + atomic_fetch_add(&signalCount, 1); RACSerialDisposable *selfDisposable = [[RACSerialDisposable alloc] init]; [compoundDisposable addDisposable:selfDisposable]; diff --git a/ReactiveObjC/extobjc/EXTRuntimeExtensions.m b/ReactiveObjC/extobjc/EXTRuntimeExtensions.m index 99c10e980..3a2dca635 100644 --- a/ReactiveObjC/extobjc/EXTRuntimeExtensions.m +++ b/ReactiveObjC/extobjc/EXTRuntimeExtensions.m @@ -11,7 +11,6 @@ #import #import -#import #import #import #import diff --git a/ReactiveObjCTests/RACMulticastConnectionSpec.m b/ReactiveObjCTests/RACMulticastConnectionSpec.m index 5ce04ba5f..2d0abc0be 100644 --- a/ReactiveObjCTests/RACMulticastConnectionSpec.m +++ b/ReactiveObjCTests/RACMulticastConnectionSpec.m @@ -15,7 +15,6 @@ #import "RACSubscriber.h" #import "RACReplaySubject.h" #import "RACScheduler.h" -#import QuickSpecBegin(RACMulticastConnectionSpec) diff --git a/ReactiveObjCTests/RACSchedulerSpec.m b/ReactiveObjCTests/RACSchedulerSpec.m index 754a8c83e..0199d46d5 100644 --- a/ReactiveObjCTests/RACSchedulerSpec.m +++ b/ReactiveObjCTests/RACSchedulerSpec.m @@ -15,7 +15,6 @@ #import "RACDisposable.h" #import #import "RACTestExampleScheduler.h" -#import // This shouldn't be used directly. Use the `expectCurrentSchedulers` block // below instead. diff --git a/ReactiveObjCTests/RACSignalSpec.m b/ReactiveObjCTests/RACSignalSpec.m index bc65fd547..8c472b68c 100644 --- a/ReactiveObjCTests/RACSignalSpec.m +++ b/ReactiveObjCTests/RACSignalSpec.m @@ -33,7 +33,7 @@ #import "RACTestScheduler.h" #import "RACTuple.h" #import "RACUnit.h" -#import +#import // Set in a beforeAll below. static NSError *RACSignalTestError; @@ -111,7 +111,7 @@ + (void)configure:(Configuration *)configuration { }; RACSignal *infiniteSignal = [RACSignal createSignal:^(id subscriber) { - __block volatile int32_t done = 0; + __block atomic_int done = 0; [RACScheduler.mainThreadScheduler schedule:^{ while (!done) { @@ -120,7 +120,7 @@ + (void)configure:(Configuration *)configuration { }]; return [RACDisposable disposableWithBlock:^{ - OSAtomicIncrement32Barrier(&done); + atomic_fetch_add(&done, 1); }]; }]; diff --git a/ReactiveObjCTests/RACSubjectSpec.m b/ReactiveObjCTests/RACSubjectSpec.m index bfe56a5b5..5e2a827aa 100644 --- a/ReactiveObjCTests/RACSubjectSpec.m +++ b/ReactiveObjCTests/RACSubjectSpec.m @@ -11,7 +11,7 @@ #import "RACSubscriberExamples.h" -#import +#import #import #import "RACBehaviorSubject.h" #import "RACCompoundDisposable.h" @@ -255,10 +255,10 @@ - (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable { // Just leak it, ain't no thang. __unsafe_unretained volatile id *values = (__unsafe_unretained id *)calloc(count, sizeof(*values)); - __block volatile int32_t nextIndex = 0; + __block atomic_int nextIndex = 0; [subject subscribeNext:^(NSNumber *value) { - int32_t indexPlusOne = OSAtomicIncrement32(&nextIndex); + int32_t indexPlusOne = atomic_fetch_add(&nextIndex, 1) + 1; values[indexPlusOne - 1] = value; }]; @@ -276,7 +276,7 @@ - (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable { [subject sendCompleted]; }); - OSMemoryBarrier(); + atomic_thread_fence(memory_order_seq_cst); NSArray *liveValues = [NSArray arrayWithObjects:(id *)values count:(NSUInteger)nextIndex]; expect(liveValues).to(haveCount(@(count))); diff --git a/ReactiveObjCTests/RACSubscriberSpec.m b/ReactiveObjCTests/RACSubscriberSpec.m index 0ff9fba90..834db099b 100644 --- a/ReactiveObjCTests/RACSubscriberSpec.m +++ b/ReactiveObjCTests/RACSubscriberSpec.m @@ -13,15 +13,15 @@ #import "RACSubscriber.h" #import "RACSubscriber+Private.h" -#import +#import QuickSpecBegin(RACSubscriberSpec) __block RACSubscriber *subscriber; __block NSMutableArray *values; -__block volatile BOOL finished; -__block volatile int32_t nextsAfterFinished; +__block _Atomic(BOOL) finished; +__block atomic_int nextsAfterFinished; __block BOOL success; __block NSError *error; @@ -36,7 +36,7 @@ error = nil; subscriber = [RACSubscriber subscriberWithNext:^(id value) { - if (finished) OSAtomicIncrement32Barrier(&nextsAfterFinished); + if (finished) atomic_fetch_add(&nextsAfterFinished, 1); [values addObject:value]; } error:^(NSError *e) { @@ -111,7 +111,7 @@ [subscriber sendCompleted]; finished = YES; - OSMemoryBarrier(); + atomic_thread_fence(memory_order_seq_cst); }); }); @@ -122,7 +122,7 @@ [subscriber sendError:nil]; finished = YES; - OSMemoryBarrier(); + atomic_thread_fence(memory_order_seq_cst); }); }); }); diff --git a/ReactiveObjCTests/RACTargetQueueSchedulerSpec.m b/ReactiveObjCTests/RACTargetQueueSchedulerSpec.m index c112bcd4a..6b12c9421 100644 --- a/ReactiveObjCTests/RACTargetQueueSchedulerSpec.m +++ b/ReactiveObjCTests/RACTargetQueueSchedulerSpec.m @@ -10,7 +10,7 @@ @import Nimble; #import "RACTargetQueueScheduler.h" -#import +#import QuickSpecBegin(RACTargetQueueSchedulerSpec) @@ -28,20 +28,20 @@ qck_it(@"should schedule blocks FIFO even when given a concurrent queue", ^{ dispatch_queue_t queue = dispatch_queue_create("test-queue", DISPATCH_QUEUE_CONCURRENT); RACScheduler *scheduler = [[RACTargetQueueScheduler alloc] initWithName:@"test-scheduler" targetQueue:queue]; - __block volatile int32_t startedCount = 0; - __block volatile uint32_t waitInFirst = 1; + __block atomic_int startedCount = 0; + __block atomic_uint waitInFirst = 1; [scheduler schedule:^{ - OSAtomicIncrement32Barrier(&startedCount); + atomic_fetch_add(&startedCount, 1); while (waitInFirst == 1) ; }]; [scheduler schedule:^{ - OSAtomicIncrement32Barrier(&startedCount); + atomic_fetch_add(&startedCount, 1); }]; expect(@(startedCount)).toEventually(equal(@1)); - OSAtomicAnd32Barrier(0, &waitInFirst); + atomic_fetch_and(&waitInFirst, 0); expect(@(startedCount)).toEventually(equal(@2)); });