forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCTUIManagerUtils.m
95 lines (83 loc) · 2.93 KB
/
RCTUIManagerUtils.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTUIManagerUtils.h"
#import "RCTAssert.h"
char *const RCTUIManagerQueueName = "com.facebook.react.ShadowQueue";
static BOOL pseudoUIManagerQueueFlag = NO;
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;
}
BOOL RCTIsPseudoUIManagerQueue()
{
if (RCTIsMainQueue()) {
return pseudoUIManagerQueueFlag;
}
return NO;
}
void RCTExecuteOnUIManagerQueue(dispatch_block_t block)
{
if (RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue()) {
block();
} else {
dispatch_async(RCTGetUIManagerQueue(), ^{
block();
});
}
}
void RCTUnsafeExecuteOnUIManagerQueueSync(dispatch_block_t block)
{
if (RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue()) {
block();
} else {
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.
block();
pseudoUIManagerQueueFlag = NO;
// Signalling UIManager block.
dispatch_semaphore_signal(uiManagerQueueBlockingSemaphore);
} else {
dispatch_sync(RCTGetUIManagerQueue(), ^{
block();
});
}
}
}