RCTUIManagerUtils.h
3.89 KB
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
96
97
98
99
100
101
102
103
104
105
106
/**
* 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 <Foundation/Foundation.h>
#import <React/RCTAssert.h>
#import <React/RCTDefines.h>
/**
* Queues Problem Intro:
* UIManager queue is a special queue because it has a special relationship with
* the Main queue.
*
* This particular relationship comes from two key factors:
* 1. UIManager initiates execution of many blocks on the Main queue;
* 2. In some cases, we want to initiate (and wait for) some UIManager's work *synchronously* from
* the Main queue.
*
* So, how can we meet these criteria?
* "Pseudo UIManager queue" comes to rescue!
*
* "Pseudo UIManager queue" means the safe execution of typical UIManager's work
* on the Main queue while the UIManager queue is explicitly blocked for preventing
* simultaneous/concurrent memory access.
*
* So, how can we technically do this?
* 1. `RCTAssertUIManagerQueue` is okay with execution on both actual UIManager and
* Pseudo UIManager queues.
* 2. Both `RCTExecuteOnUIManagerQueue` and `RCTUnsafeExecuteOnUIManagerQueueSync`
* execute given block *synchronously* if they were called on actual UIManager
* or Pseudo UIManager queues.
* 3. `RCTExecuteOnMainQueue` executes given block *synchronously* if we already on
* the Main queue.
* 4. `RCTUnsafeExecuteOnUIManagerQueueSync` is smart enough to do the trick:
* It detects calling on the Main queue and in this case, instead of doing
* trivial *synchronous* dispatch, it does:
* - Block the Main queue;
* - Dispatch the special block on UIManager queue to block the queue and
* concurrent memory access;
* - Execute the given block on the Main queue;
* - Unblock the UIManager queue.
*
* Imagine the analogy: We have two queues: the Main one and UIManager one.
* And these queues are two lanes of railway that go in parallel. Then,
* at some point, we merge UIManager lane with the Main lane, and all cars use
* the unified the Main lane.
* And then we split lanes again.
*
* This solution assumes that the code running on UIManager queue will never
* *explicitly* block the Main queue via calling `RCTUnsafeExecuteOnMainQueueSync`.
* Otherwise, it can cause a deadlock.
*/
/**
* Returns UIManager queue.
*/
RCT_EXTERN dispatch_queue_t RCTGetUIManagerQueue(void);
/**
* Default name for the UIManager queue.
*/
RCT_EXTERN char *const RCTUIManagerQueueName;
/**
* Check if we are currently on UIManager queue.
* Please do not use this unless you really know what you're doing.
*/
RCT_EXTERN BOOL RCTIsUIManagerQueue(void);
/**
* Check if we are currently on Pseudo UIManager queue.
* Please do not use this unless you really know what you're doing.
*/
RCT_EXTERN BOOL RCTIsPseudoUIManagerQueue(void);
/**
* *Asynchronously* executes the specified block on the UIManager queue.
* Unlike `dispatch_async()` this will execute the block immediately
* if we're already on the UIManager queue.
*/
RCT_EXTERN void RCTExecuteOnUIManagerQueue(dispatch_block_t block);
/**
* *Synchorously* executes the specified block on the UIManager queue.
* Unlike `dispatch_sync()` this will execute the block immediately
* if we're already on the UIManager queue.
* Please do not use this unless you really know what you're doing.
*/
RCT_EXTERN void RCTUnsafeExecuteOnUIManagerQueueSync(dispatch_block_t block);
/**
* Convenience macro for asserting that we're running on UIManager queue.
*/
#define RCTAssertUIManagerQueue() RCTAssert(RCTIsUIManagerQueue() || RCTIsPseudoUIManagerQueue(), \
@"This function must be called on the UIManager queue")
/**
* Returns new unique root view tag.
*/
RCT_EXTERN NSNumber *RCTAllocateRootViewTag(void);