|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | */
|
7 | 7 |
|
| 8 | +#import <Foundation/Foundation.h> |
| 9 | + |
8 | 10 | #import <ReactCommon/RuntimeExecutorSyncUIThreadUtils.h>
|
9 |
| -#include <future> |
10 |
| -#include <thread> |
| 11 | +#import <react/debug/react_native_assert.h> |
| 12 | +#import <react/featureflags/ReactNativeFeatureFlags.h> |
| 13 | +#import <react/utils/OnScopeExit.h> |
| 14 | +#import <algorithm> |
| 15 | +#import <functional> |
| 16 | +#import <future> |
| 17 | +#import <mutex> |
| 18 | +#import <optional> |
| 19 | +#import <thread> |
11 | 20 |
|
12 | 21 | namespace facebook::react {
|
| 22 | + |
| 23 | +namespace { |
| 24 | +class UITask { |
| 25 | + std::promise<void> _isDone; |
| 26 | + std::function<void()> _uiWork; |
| 27 | + long _id; |
| 28 | + static std::atomic_long _uid; |
| 29 | + |
| 30 | + public: |
| 31 | + UITask(UITask &&other) = default; |
| 32 | + UITask &operator=(UITask &&other) = default; |
| 33 | + UITask(const UITask &) = delete; |
| 34 | + UITask &operator=(const UITask &) = delete; |
| 35 | + |
| 36 | + UITask() = delete; |
| 37 | + UITask(std::function<void()> uiWork) : _isDone(), _uiWork(uiWork), _id(_uid.fetch_add(1)) {} |
| 38 | + |
| 39 | + void run() |
| 40 | + { |
| 41 | + if (!_uiWork) { |
| 42 | + return; |
| 43 | + } |
| 44 | + OnScopeExit onScopeExit(^{ |
| 45 | + _uiWork = nullptr; |
| 46 | + _isDone.set_value(); |
| 47 | + }); |
| 48 | + _uiWork(); |
| 49 | + } |
| 50 | + |
| 51 | + long id() |
| 52 | + { |
| 53 | + return _id; |
| 54 | + } |
| 55 | + |
| 56 | + std::future<void> future() |
| 57 | + { |
| 58 | + return _isDone.get_future(); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +std::atomic_long UITask::_uid; |
| 63 | + |
| 64 | +std::mutex &g_mutex() |
| 65 | +{ |
| 66 | + static std::mutex mutex; |
| 67 | + return mutex; |
| 68 | +} |
| 69 | + |
| 70 | +std::condition_variable &g_cv() |
| 71 | +{ |
| 72 | + static std::condition_variable cv; |
| 73 | + return cv; |
| 74 | +} |
| 75 | + |
| 76 | +std::mutex &g_ticket() |
| 77 | +{ |
| 78 | + static std::mutex ticket; |
| 79 | + return ticket; |
| 80 | +} |
| 81 | + |
| 82 | +std::optional<UITask> &g_uiTask() |
| 83 | +{ |
| 84 | + static std::optional<UITask> uiTaskQueue; |
| 85 | + return uiTaskQueue; |
| 86 | +} |
| 87 | + |
| 88 | +static bool g_isRunningUITask = false; |
| 89 | + |
| 90 | +void runTask(UITask &task) |
| 91 | +{ |
| 92 | + g_isRunningUITask = true; |
| 93 | + OnScopeExit onScopeExit([&]() { g_isRunningUITask = false; }); |
| 94 | + task.run(); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * This method is resilient to multiple javascript threads. |
| 99 | + * This can happen when multiple react instances interleave. |
| 100 | + * |
| 101 | + * The extension from 1 js thread to n: All js threads race to |
| 102 | + * get a ticket to post a ui task. The first one to get the ticket |
| 103 | + * will post the ui task, and go to sleep. The cooridnator or |
| 104 | + * main queue will execute that ui task, waking up the js thread |
| 105 | + * and releasing that ticket. Another js thread will get the ticket. |
| 106 | + */ |
| 107 | +void saferExecuteSynchronouslyOnSameThread_CAN_DEADLOCK( |
| 108 | + const RuntimeExecutor &runtimeExecutor, |
| 109 | + std::function<void(jsi::Runtime &runtime)> &&runtimeWork) |
| 110 | +{ |
| 111 | + react_native_assert([[NSThread currentThread] isMainThread] && !g_isRunningUITask); |
| 112 | + |
| 113 | + jsi::Runtime *runtime = nullptr; |
| 114 | + std::promise<void> runtimeWorkDone; |
| 115 | + |
| 116 | + runtimeExecutor([&](jsi::Runtime &rt) { |
| 117 | + { |
| 118 | + std::lock_guard<std::mutex> lock(g_mutex()); |
| 119 | + runtime = &rt; |
| 120 | + g_cv().notify_one(); |
| 121 | + } |
| 122 | + |
| 123 | + runtimeWorkDone.get_future().wait(); |
| 124 | + }); |
| 125 | + |
| 126 | + while (true) { |
| 127 | + std::unique_lock<std::mutex> lock(g_mutex()); |
| 128 | + g_cv().wait(lock, [&] { return runtime != nullptr || g_uiTask() != std::nullopt; }); |
| 129 | + if (runtime != nullptr) { |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + auto uiTask = std::move(*g_uiTask()); |
| 134 | + g_uiTask() = std::nullopt; |
| 135 | + lock.unlock(); |
| 136 | + runTask(uiTask); |
| 137 | + } |
| 138 | + |
| 139 | + OnScopeExit onScopeExit([&]() { runtimeWorkDone.set_value(); }); |
| 140 | + runtimeWork(*runtime); |
| 141 | +} |
| 142 | + |
13 | 143 | /*
|
14 | 144 | * Executes some `runtimeWork` on the same thread in a *synchronous* manner
|
15 | 145 | * using the `RuntimeExecutor`.
|
|
26 | 156 | * - [JS thread] Signal runtime capture block is finished:
|
27 | 157 | * resolve(runtimeCaptureBlockDone);
|
28 | 158 | */
|
29 |
| -void executeSynchronouslyOnSameThread_CAN_DEADLOCK( |
| 159 | +void legacyExecuteSynchronouslyOnSameThread_CAN_DEADLOCK( |
30 | 160 | const RuntimeExecutor &runtimeExecutor,
|
31 | 161 | std::function<void(jsi::Runtime &)> &&runtimeWork)
|
32 | 162 | {
|
@@ -55,4 +185,62 @@ void executeSynchronouslyOnSameThread_CAN_DEADLOCK(
|
55 | 185 | runtimeCaptureBlockDone.get_future().wait();
|
56 | 186 | }
|
57 | 187 |
|
| 188 | +} // namespace |
| 189 | + |
| 190 | +void executeSynchronouslyOnSameThread_CAN_DEADLOCK( |
| 191 | + const RuntimeExecutor &runtimeExecutor, |
| 192 | + std::function<void(jsi::Runtime &)> &&runtimeWork) |
| 193 | +{ |
| 194 | + if (ReactNativeFeatureFlags::enableMainQueueCoordinatorOnIOS()) { |
| 195 | + saferExecuteSynchronouslyOnSameThread_CAN_DEADLOCK(runtimeExecutor, std::move(runtimeWork)); |
| 196 | + } else { |
| 197 | + legacyExecuteSynchronouslyOnSameThread_CAN_DEADLOCK(runtimeExecutor, std::move(runtimeWork)); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * This method is resilient to multiple javascript threads. |
| 203 | + * This can happen when multiple react instances interleave. |
| 204 | + * |
| 205 | + * The extension from 1 js thread to n: All js threads race to |
| 206 | + * get a ticket to post a ui task. The first one to get the ticket |
| 207 | + * will post the ui task, and go to sleep. The cooridnator or |
| 208 | + * main queue will execute that ui task, waking up the js thread |
| 209 | + * and releasing that ticket. Another js thread will get the ticket. |
| 210 | + */ |
| 211 | +void unsafeExecuteOnMainThreadSync(std::function<void()> work) |
| 212 | +{ |
| 213 | + std::lock_guard<std::mutex> ticket(g_ticket()); |
| 214 | + react_native_assert([[NSThread currentThread].name containsString:@"JavaScript"]); |
| 215 | + { |
| 216 | + std::lock_guard lock(g_mutex()); |
| 217 | + react_native_assert(!g_uiTask()); |
| 218 | + } |
| 219 | + |
| 220 | + long tid = -1; |
| 221 | + std::future<void> isDone; |
| 222 | + |
| 223 | + // post ui task to main thread |
| 224 | + { |
| 225 | + std::lock_guard<std::mutex> lock(g_mutex()); |
| 226 | + g_uiTask() = UITask(work); |
| 227 | + tid = g_uiTask()->id(); |
| 228 | + isDone = g_uiTask()->future(); |
| 229 | + g_cv().notify_one(); |
| 230 | + } |
| 231 | + |
| 232 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 233 | + std::unique_lock<std::mutex> lock; |
| 234 | + if (!g_uiTask() || g_uiTask()->id() != tid) { |
| 235 | + return; |
| 236 | + } |
| 237 | + auto uiTask = std::move(*g_uiTask()); |
| 238 | + g_uiTask() = std::nullopt; |
| 239 | + lock.unlock(); |
| 240 | + runTask(uiTask); |
| 241 | + }); |
| 242 | + |
| 243 | + isDone.wait(); |
| 244 | +} |
| 245 | + |
58 | 246 | } // namespace facebook::react
|
0 commit comments