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
This commit is contained in:
Adlai Holler 2017-08-01 03:51:33 -07:00 committed by Facebook Github Bot
parent c1c791fb59
commit 24df099835
1 changed files with 7 additions and 11 deletions

View File

@ -10,7 +10,7 @@
#import "RCTProfile.h" #import "RCTProfile.h"
#import <dlfcn.h> #import <dlfcn.h>
#import <libkern/OSAtomic.h> #import <stdatomic.h>
#import <mach/mach.h> #import <mach/mach.h>
#import <objc/message.h> #import <objc/message.h>
#import <objc/runtime.h> #import <objc/runtime.h>
@ -42,8 +42,7 @@ static NSString *const kProfilePrefix = @"rct_profile_";
#pragma mark - Variables #pragma mark - Variables
// This is actually a BOOL - but has to be compatible with OSAtomic static atomic_bool RCTProfileProfiling = ATOMIC_VAR_INIT(NO);
static volatile uint32_t RCTProfileProfiling;
static NSDictionary *RCTProfileInfo; static NSDictionary *RCTProfileInfo;
static NSMutableDictionary *RCTProfileOngoingEvents; static NSMutableDictionary *RCTProfileOngoingEvents;
@ -439,18 +438,17 @@ dispatch_queue_t RCTProfileGetQueue(void)
BOOL RCTProfileIsProfiling(void) BOOL RCTProfileIsProfiling(void)
{ {
return (BOOL)RCTProfileProfiling; return atomic_load(&RCTProfileProfiling);
} }
void RCTProfileInit(RCTBridge *bridge) void RCTProfileInit(RCTBridge *bridge)
{ {
// TODO: enable assert JS thread from any file (and assert here) // TODO: enable assert JS thread from any file (and assert here)
if (RCTProfileIsProfiling()) { BOOL wasProfiling = atomic_fetch_or(&RCTProfileProfiling, 1);
if (wasProfiling) {
return; return;
} }
OSAtomicOr32Barrier(1, &RCTProfileProfiling);
if (callbacks != NULL) { if (callbacks != NULL) {
systrace_buffer = callbacks->start(); systrace_buffer = callbacks->start();
} else { } else {
@ -493,13 +491,11 @@ void RCTProfileInit(RCTBridge *bridge)
void RCTProfileEnd(RCTBridge *bridge, void (^callback)(NSString *)) void RCTProfileEnd(RCTBridge *bridge, void (^callback)(NSString *))
{ {
// assert JavaScript thread here again // assert JavaScript thread here again
BOOL wasProfiling = atomic_fetch_and(&RCTProfileProfiling, 0);
if (!RCTProfileIsProfiling()) { if (!wasProfiling) {
return; return;
} }
OSAtomicAnd32Barrier(0, &RCTProfileProfiling);
[[NSNotificationCenter defaultCenter] postNotificationName:RCTProfileDidEndProfiling [[NSNotificationCenter defaultCenter] postNotificationName:RCTProfileDidEndProfiling
object:bridge]; object:bridge];