2017-09-26 20:55:58 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-09-26 20:55:58 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-09-26 20:55:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTUIManagerUtils.h"
|
|
|
|
|
2017-11-08 00:10:52 +00:00
|
|
|
#import <libkern/OSAtomic.h>
|
|
|
|
|
2017-09-26 20:55:58 +00:00
|
|
|
#import "RCTAssert.h"
|
|
|
|
|
|
|
|
char *const RCTUIManagerQueueName = "com.facebook.react.ShadowQueue";
|
|
|
|
|
2017-10-09 04:23:52 +00:00
|
|
|
static BOOL pseudoUIManagerQueueFlag = NO;
|
|
|
|
|
2017-09-26 20:55:58 +00:00
|
|
|
dispatch_queue_t RCTGetUIManagerQueue(void)
|
|
|
|
{
|
|
|
|
static dispatch_queue_t shadowQueue;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
if ([NSOperation instancesRespondToSelector:@selector(qualityOfService)]) {
|
|
|
|
dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0);
|
|
|
|
shadowQueue = dispatch_queue_create(RCTUIManagerQueueName, attr);
|
|
|
|
} else {
|
|
|
|
shadowQueue = dispatch_queue_create(RCTUIManagerQueueName, DISPATCH_QUEUE_SERIAL);
|
|
|
|
dispatch_set_target_queue(shadowQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return shadowQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL RCTIsUIManagerQueue()
|
|
|
|
{
|
|
|
|
static void *queueKey = &queueKey;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
dispatch_queue_set_specific(RCTGetUIManagerQueue(), queueKey, queueKey, NULL);
|
|
|
|
});
|
|
|
|
return dispatch_get_specific(queueKey) == queueKey;
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:23:52 +00:00
|
|
|
BOOL RCTIsPseudoUIManagerQueue()
|
|
|
|
{
|
|
|
|
if (RCTIsMainQueue()) {
|
|
|
|
return pseudoUIManagerQueueFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2017-09-26 20:55:58 +00:00
|
|
|
void RCTExecuteOnUIManagerQueue(dispatch_block_t block)
|
|
|
|
{
|
2017-10-09 04:23:52 +00:00
|
|
|
if (RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue()) {
|
2017-09-26 20:55:58 +00:00
|
|
|
block();
|
|
|
|
} else {
|
|
|
|
dispatch_async(RCTGetUIManagerQueue(), ^{
|
|
|
|
block();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RCTUnsafeExecuteOnUIManagerQueueSync(dispatch_block_t block)
|
|
|
|
{
|
2017-10-09 04:23:52 +00:00
|
|
|
if (RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue()) {
|
2017-09-26 20:55:58 +00:00
|
|
|
block();
|
|
|
|
} else {
|
2017-10-09 04:23:52 +00:00
|
|
|
if (RCTIsMainQueue()) {
|
|
|
|
dispatch_semaphore_t mainQueueBlockingSemaphore = dispatch_semaphore_create(0);
|
|
|
|
dispatch_semaphore_t uiManagerQueueBlockingSemaphore = dispatch_semaphore_create(0);
|
|
|
|
|
|
|
|
// Dispatching block which blocks UI Manager queue.
|
|
|
|
dispatch_async(RCTGetUIManagerQueue(), ^{
|
|
|
|
// Initiating `block` execution on main queue.
|
|
|
|
dispatch_semaphore_signal(mainQueueBlockingSemaphore);
|
|
|
|
// Waiting for finishing `block`.
|
|
|
|
dispatch_semaphore_wait(uiManagerQueueBlockingSemaphore, DISPATCH_TIME_FOREVER);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Waiting for block on UIManager queue.
|
|
|
|
dispatch_semaphore_wait(mainQueueBlockingSemaphore, DISPATCH_TIME_FOREVER);
|
|
|
|
pseudoUIManagerQueueFlag = YES;
|
|
|
|
// `block` execution while UIManager queue is blocked by semaphore.
|
2017-09-26 20:55:58 +00:00
|
|
|
block();
|
2017-10-09 04:23:52 +00:00
|
|
|
pseudoUIManagerQueueFlag = NO;
|
|
|
|
// Signalling UIManager block.
|
|
|
|
dispatch_semaphore_signal(uiManagerQueueBlockingSemaphore);
|
|
|
|
} else {
|
|
|
|
dispatch_sync(RCTGetUIManagerQueue(), ^{
|
|
|
|
block();
|
|
|
|
});
|
|
|
|
}
|
2017-09-26 20:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 00:10:52 +00:00
|
|
|
|
|
|
|
NSNumber *RCTAllocateRootViewTag()
|
|
|
|
{
|
|
|
|
// Numbering of these tags goes from 1, 11, 21, 31, ..., 100501, ...
|
|
|
|
static int64_t rootViewTagCounter = -1;
|
|
|
|
return @(OSAtomicIncrement64(&rootViewTagCounter) * 10 + 1);
|
|
|
|
}
|