From 24df099835dc6482fcf46c5fa9ed033de4e9d4a9 Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Tue, 1 Aug 2017 03:51:33 -0700 Subject: [PATCH] RCTProfile: Use C atomics instead of OSAtomic Summary: This will save us precious microseconds and it's prettier to look at. Closes https://github.com/facebook/react-native/pull/15276 Differential Revision: D5531823 Pulled By: javache fbshipit-source-id: f8a97ec2a03e3cfdf1801457a481ec62a9371eeb --- React/Profiler/RCTProfile.m | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/React/Profiler/RCTProfile.m b/React/Profiler/RCTProfile.m index 10ce57128..a69e73a62 100644 --- a/React/Profiler/RCTProfile.m +++ b/React/Profiler/RCTProfile.m @@ -10,7 +10,7 @@ #import "RCTProfile.h" #import -#import +#import #import #import #import @@ -42,8 +42,7 @@ static NSString *const kProfilePrefix = @"rct_profile_"; #pragma mark - Variables -// This is actually a BOOL - but has to be compatible with OSAtomic -static volatile uint32_t RCTProfileProfiling; +static atomic_bool RCTProfileProfiling = ATOMIC_VAR_INIT(NO); static NSDictionary *RCTProfileInfo; static NSMutableDictionary *RCTProfileOngoingEvents; @@ -439,18 +438,17 @@ dispatch_queue_t RCTProfileGetQueue(void) BOOL RCTProfileIsProfiling(void) { - return (BOOL)RCTProfileProfiling; + return atomic_load(&RCTProfileProfiling); } void RCTProfileInit(RCTBridge *bridge) { // TODO: enable assert JS thread from any file (and assert here) - if (RCTProfileIsProfiling()) { + BOOL wasProfiling = atomic_fetch_or(&RCTProfileProfiling, 1); + if (wasProfiling) { return; } - OSAtomicOr32Barrier(1, &RCTProfileProfiling); - if (callbacks != NULL) { systrace_buffer = callbacks->start(); } else { @@ -493,13 +491,11 @@ void RCTProfileInit(RCTBridge *bridge) void RCTProfileEnd(RCTBridge *bridge, void (^callback)(NSString *)) { // assert JavaScript thread here again - - if (!RCTProfileIsProfiling()) { + BOOL wasProfiling = atomic_fetch_and(&RCTProfileProfiling, 0); + if (!wasProfiling) { return; } - OSAtomicAnd32Barrier(0, &RCTProfileProfiling); - [[NSNotificationCenter defaultCenter] postNotificationName:RCTProfileDidEndProfiling object:bridge];