2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2021-12-31 02:08:43 +03:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2017-09-26 23:55:58 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03: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 23:55:58 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTUIManagerUtils.h"
|
|
|
|
|
2020-09-09 22:45:38 +03:00
|
|
|
#import <stdatomic.h>
|
2017-11-08 03:10:52 +03:00
|
|
|
|
2017-09-26 23:55:58 +03:00
|
|
|
#import "RCTAssert.h"
|
|
|
|
|
|
|
|
char *const RCTUIManagerQueueName = "com.facebook.react.ShadowQueue";
|
|
|
|
|
2017-10-09 07:23:52 +03:00
|
|
|
static BOOL pseudoUIManagerQueueFlag = NO;
|
|
|
|
|
2017-09-26 23:55:58 +03:00
|
|
|
dispatch_queue_t RCTGetUIManagerQueue(void)
|
|
|
|
{
|
|
|
|
static dispatch_queue_t shadowQueue;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
if ([NSOperation instancesRespondToSelector:@selector(qualityOfService)]) {
|
2020-03-09 08:58:01 +03:00
|
|
|
dispatch_queue_attr_t attr =
|
|
|
|
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0);
|
2017-09-26 23:55:58 +03:00
|
|
|
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 07:23:52 +03:00
|
|
|
BOOL RCTIsPseudoUIManagerQueue()
|
|
|
|
{
|
|
|
|
if (RCTIsMainQueue()) {
|
|
|
|
return pseudoUIManagerQueueFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2017-09-26 23:55:58 +03:00
|
|
|
void RCTExecuteOnUIManagerQueue(dispatch_block_t block)
|
|
|
|
{
|
2017-10-09 07:23:52 +03:00
|
|
|
if (RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue()) {
|
2017-09-26 23:55:58 +03:00
|
|
|
block();
|
|
|
|
} else {
|
|
|
|
dispatch_async(RCTGetUIManagerQueue(), ^{
|
|
|
|
block();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RCTUnsafeExecuteOnUIManagerQueueSync(dispatch_block_t block)
|
|
|
|
{
|
2017-10-09 07:23:52 +03:00
|
|
|
if (RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue()) {
|
2017-09-26 23:55:58 +03:00
|
|
|
block();
|
|
|
|
} else {
|
2017-10-09 07:23:52 +03: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 23:55:58 +03:00
|
|
|
block();
|
2017-10-09 07:23:52 +03:00
|
|
|
pseudoUIManagerQueueFlag = NO;
|
|
|
|
// Signalling UIManager block.
|
|
|
|
dispatch_semaphore_signal(uiManagerQueueBlockingSemaphore);
|
|
|
|
} else {
|
|
|
|
dispatch_sync(RCTGetUIManagerQueue(), ^{
|
|
|
|
block();
|
|
|
|
});
|
|
|
|
}
|
2017-09-26 23:55:58 +03:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 03:10:52 +03:00
|
|
|
|
|
|
|
NSNumber *RCTAllocateRootViewTag()
|
|
|
|
{
|
|
|
|
// Numbering of these tags goes from 1, 11, 21, 31, ..., 100501, ...
|
2020-09-09 22:45:38 +03:00
|
|
|
static _Atomic int64_t rootViewTagCounter = 0;
|
|
|
|
return @(atomic_fetch_add_explicit(&rootViewTagCounter, 1, memory_order_relaxed) * 10 + 1);
|
2017-11-08 03:10:52 +03:00
|
|
|
}
|