|
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 | + |
| 28 | + public: |
| 29 | + UITask(UITask &&other) = default; |
| 30 | + UITask &operator=(UITask &&other) = default; |
| 31 | + UITask(const UITask &) = delete; |
| 32 | + UITask &operator=(const UITask &) = delete; |
| 33 | + |
| 34 | + UITask(std::function<void()> uiWork) : _uiWork(std::move(uiWork)) {} |
| 35 | + |
| 36 | + void operator()() |
| 37 | + { |
| 38 | + if (!_uiWork) { |
| 39 | + return; |
| 40 | + } |
| 41 | + OnScopeExit onScopeExit(^{ |
| 42 | + _uiWork = nullptr; |
| 43 | + _isDone.set_value(); |
| 44 | + }); |
| 45 | + _uiWork(); |
| 46 | + } |
| 47 | + |
| 48 | + std::future<void> future() |
| 49 | + { |
| 50 | + return _isDone.get_future(); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +// Protects access to g_uiTask and g_cv. |
| 55 | +std::mutex &g_mutex() |
| 56 | +{ |
| 57 | + static std::mutex mutex; |
| 58 | + return mutex; |
| 59 | +} |
| 60 | + |
| 61 | +std::condition_variable &g_cv() |
| 62 | +{ |
| 63 | + static std::condition_variable cv; |
| 64 | + return cv; |
| 65 | +} |
| 66 | + |
| 67 | +std::mutex &g_ticket() |
| 68 | +{ |
| 69 | + static std::mutex ticket; |
| 70 | + return ticket; |
| 71 | +} |
| 72 | + |
| 73 | +std::optional<UITask> &g_uiTask() |
| 74 | +{ |
| 75 | + static std::optional<UITask> uiTaskQueue; |
| 76 | + return uiTaskQueue; |
| 77 | +} |
| 78 | + |
| 79 | +// Must be called holding g_mutex(); |
| 80 | +bool hasUITask() |
| 81 | +{ |
| 82 | + return g_uiTask().has_value(); |
| 83 | +} |
| 84 | + |
| 85 | +// Must be called holding g_mutex(); |
| 86 | +UITask takeUITask() |
| 87 | +{ |
| 88 | + react_native_assert(hasUITask()); |
| 89 | + auto uiTask = std::move(*g_uiTask()); |
| 90 | + g_uiTask() = std::nullopt; |
| 91 | + return uiTask; |
| 92 | +} |
| 93 | + |
| 94 | +// Must be called holding g_mutex(); |
| 95 | +UITask &postUITask(std::function<void()> &&uiWork) |
| 96 | +{ |
| 97 | + react_native_assert(!hasUITask()); |
| 98 | + g_uiTask() = UITask(uiWork); |
| 99 | + g_cv().notify_one(); |
| 100 | + return *g_uiTask(); |
| 101 | +} |
| 102 | + |
| 103 | +bool g_isRunningUITask = false; |
| 104 | +void runUITask(UITask &uiTask) |
| 105 | +{ |
| 106 | + react_native_assert([[NSThread currentThread] isMainThread]); |
| 107 | + g_isRunningUITask = true; |
| 108 | + OnScopeExit onScopeExit([]() { g_isRunningUITask = false; }); |
| 109 | + uiTask(); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * This method is resilient to multiple javascript threads. |
| 114 | + * This can happen when multiple react instances interleave. |
| 115 | + * |
| 116 | + * The extension from 1 js thread to n: All js threads race to |
| 117 | + * get a ticket to post a ui task. The first one to get the ticket |
| 118 | + * will post the ui task, and go to sleep. The cooridnator or |
| 119 | + * main queue will execute that ui task, waking up the js thread |
| 120 | + * and releasing that ticket. Another js thread will get the ticket. |
| 121 | + * |
| 122 | + * For simplicity, we will just use this algorithm for all bg threads. |
| 123 | + * Not just the js thread. |
| 124 | + */ |
| 125 | +void saferExecuteSynchronouslyOnSameThread_CAN_DEADLOCK( |
| 126 | + const RuntimeExecutor &runtimeExecutor, |
| 127 | + std::function<void(jsi::Runtime &runtime)> &&runtimeWork) |
| 128 | +{ |
| 129 | + react_native_assert([[NSThread currentThread] isMainThread] && !g_isRunningUITask); |
| 130 | + |
| 131 | + jsi::Runtime *runtime = nullptr; |
| 132 | + std::promise<void> runtimeWorkDone; |
| 133 | + |
| 134 | + runtimeExecutor([&runtime, runtimeWorkDoneFuture = runtimeWorkDone.get_future().share()](jsi::Runtime &rt) { |
| 135 | + { |
| 136 | + std::lock_guard<std::mutex> lock(g_mutex()); |
| 137 | + runtime = &rt; |
| 138 | + g_cv().notify_one(); |
| 139 | + } |
| 140 | + |
| 141 | + runtimeWorkDoneFuture.wait(); |
| 142 | + }); |
| 143 | + |
| 144 | + while (true) { |
| 145 | + std::unique_lock<std::mutex> lock(g_mutex()); |
| 146 | + g_cv().wait(lock, [&] { return runtime != nullptr || hasUITask(); }); |
| 147 | + if (runtime != nullptr) { |
| 148 | + break; |
| 149 | + } |
| 150 | + |
| 151 | + auto uiTask = takeUITask(); |
| 152 | + lock.unlock(); |
| 153 | + runUITask(uiTask); |
| 154 | + } |
| 155 | + |
| 156 | + OnScopeExit onScopeExit([&]() { runtimeWorkDone.set_value(); }); |
| 157 | + runtimeWork(*runtime); |
| 158 | +} |
| 159 | + |
13 | 160 | /*
|
14 | 161 | * Schedules `runtimeWork` to be executed on the same thread using the
|
15 | 162 | * `RuntimeExecutor`, and blocks on its completion.
|
|
26 | 173 | * - [JS thread] Signal runtime capture block is finished:
|
27 | 174 | * resolve(runtimeCaptureBlockDone);
|
28 | 175 | */
|
29 |
| -void executeSynchronouslyOnSameThread_CAN_DEADLOCK( |
| 176 | +void legacyExecuteSynchronouslyOnSameThread_CAN_DEADLOCK( |
30 | 177 | const RuntimeExecutor &runtimeExecutor,
|
31 | 178 | std::function<void(jsi::Runtime &)> &&runtimeWork)
|
32 | 179 | {
|
@@ -58,4 +205,54 @@ void executeSynchronouslyOnSameThread_CAN_DEADLOCK(
|
58 | 205 | runtimeCaptureBlockDone.get_future().wait();
|
59 | 206 | }
|
60 | 207 |
|
| 208 | +} // namespace |
| 209 | + |
| 210 | +void executeSynchronouslyOnSameThread_CAN_DEADLOCK( |
| 211 | + const RuntimeExecutor &runtimeExecutor, |
| 212 | + std::function<void(jsi::Runtime &)> &&runtimeWork) |
| 213 | +{ |
| 214 | + if (ReactNativeFeatureFlags::enableMainQueueCoordinatorOnIOS()) { |
| 215 | + saferExecuteSynchronouslyOnSameThread_CAN_DEADLOCK(runtimeExecutor, std::move(runtimeWork)); |
| 216 | + } else { |
| 217 | + legacyExecuteSynchronouslyOnSameThread_CAN_DEADLOCK(runtimeExecutor, std::move(runtimeWork)); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +/** |
| 222 | + * This method is resilient to multiple javascript threads. |
| 223 | + * This can happen when multiple react instances interleave. |
| 224 | + * |
| 225 | + * The extension from 1 js thread to n: All js threads race to |
| 226 | + * get a ticket to post a ui task. The first one to get the ticket |
| 227 | + * will post the ui task, and go to sleep. The cooridnator or |
| 228 | + * main queue will execute that ui task, waking up the js thread |
| 229 | + * and releasing that ticket. Another js thread will get the ticket. |
| 230 | + * |
| 231 | + * For simplicity, we will just use this method for all bg threads. |
| 232 | + * Not just the js thread. |
| 233 | + */ |
| 234 | +void unsafeExecuteOnMainThreadSync(std::function<void()> work) |
| 235 | +{ |
| 236 | + std::lock_guard<std::mutex> ticket(g_ticket()); |
| 237 | + |
| 238 | + std::future<void> isDone; |
| 239 | + { |
| 240 | + std::lock_guard<std::mutex> lock(g_mutex()); |
| 241 | + isDone = postUITask(std::move(work)).future(); |
| 242 | + } |
| 243 | + |
| 244 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 245 | + std::unique_lock<std::mutex> lock(g_mutex()); |
| 246 | + if (!hasUITask()) { |
| 247 | + return; |
| 248 | + } |
| 249 | + |
| 250 | + auto uiTask = takeUITask(); |
| 251 | + lock.unlock(); |
| 252 | + runUITask(uiTask); |
| 253 | + }); |
| 254 | + |
| 255 | + isDone.wait(); |
| 256 | +} |
| 257 | + |
61 | 258 | } // namespace facebook::react
|
0 commit comments