diff --git a/ReactiveObjC/NSObject+RACPropertySubscribing.m b/ReactiveObjC/NSObject+RACPropertySubscribing.m index dbede782c..12c840215 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 9377fb33b..e4be24662 100644 --- a/ReactiveObjC/RACCommand.m +++ b/ReactiveObjC/RACCommand.m @@ -17,7 +17,7 @@ #import "RACScheduler.h" #import "RACSequence.h" #import "RACSignal+Operations.h" -#import +#import NSString * const RACCommandErrorDomain = @"RACCommandErrorDomain"; NSString * const RACUnderlyingCommandErrorKey = @"RACUnderlyingCommandErrorKey"; @@ -26,7 +26,7 @@ @interface RACCommand () { // Atomic backing variable for `allowsConcurrentExecution`. - volatile uint32_t _allowsConcurrentExecution; + atomic_bool _allowsConcurrentExecution; } /// A subject that sends added execution signals. @@ -55,9 +55,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 9a6ef2c96..3f62a9747 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; } @@ -45,8 +45,8 @@ - (instancetype)initWithBlock:(void (^)(void))block { self = [super init]; - _disposeBlock = (void *)CFBridgingRetain([block copy]); - OSMemoryBarrier(); + _disposeBlock = (void *)CFBridgingRetain([block copy]); + 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 2e8f290aa..956f555ce 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 718da6624..ce2434fb8 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 35e98dd71..c56cb9a54 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 7cb0c16a8..2eb79b25c 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 #import @@ -700,7 +700,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 @@ -752,7 +752,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; } } @@ -1151,17 +1151,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 8aa9464fc..5d007f426 100644 --- a/ReactiveObjC/RACSignal.m +++ b/ReactiveObjC/RACSignal.m @@ -23,7 +23,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]; @@ -363,7 +363,7 @@ - (instancetype)filter:(BOOL (^)(id))block { - (instancetype)flattenMap:(RACSignal *(^)(id))block { return [RACSignal createSignal:^RACDisposable *(id subscriber) { - __block volatile int32_t subscriptionCount = 1; + __block atomic_int subscriptionCount = 1; RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable]; @@ -378,7 +378,7 @@ - (instancetype)flattenMap:(RACSignal *(^)(id))block { } NSCAssert([signal isKindOfClass:RACSignal.class], @"Expected a RACSignal, got %@", signal); - OSAtomicIncrement32(&subscriptionCount); + atomic_fetch_add(&subscriptionCount, 1); RACDisposable *innerDisposable = [signal subscribeNext:^(id x) { [subscriber sendNext:x]; @@ -386,7 +386,7 @@ - (instancetype)flattenMap:(RACSignal *(^)(id))block { [subscriber sendError:error]; [disposable dispose]; } completed:^{ - if (!OSAtomicDecrement32(&subscriptionCount)) { + if (atomic_fetch_sub(&subscriptionCount, 1) - 1 == 0) { [subscriber sendCompleted]; } }]; @@ -395,7 +395,7 @@ - (instancetype)flattenMap:(RACSignal *(^)(id))block { } error:^(NSError *error) { [subscriber sendError:error]; } completed:^{ - if (!OSAtomicDecrement32(&subscriptionCount)) { + if (atomic_fetch_sub(&subscriptionCount, 1) - 1 == 0) { [subscriber sendCompleted]; } }]; diff --git a/ReactiveObjC/extobjc/EXTRuntimeExtensions.m b/ReactiveObjC/extobjc/EXTRuntimeExtensions.m index 6a6e6f222..f6dee040d 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 78769157f..3229f69c8 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/RACSignalSpec.m b/ReactiveObjCTests/RACSignalSpec.m index 6062cba06..ce65db98a 100644 --- a/ReactiveObjCTests/RACSignalSpec.m +++ b/ReactiveObjCTests/RACSignalSpec.m @@ -121,7 +121,7 @@ + (void)configure:(Configuration *)configuration { }]; return [RACDisposable disposableWithBlock:^{ - ++done; + atomic_fetch_add(&done, 1); }]; }]; diff --git a/ReactiveObjCTests/RACSubscriberSpec.m b/ReactiveObjCTests/RACSubscriberSpec.m index 5ba5da3f9..585086c3e 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 0dca0a3a0..70f28463c 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)); });