Skip to content

Commit e39a03a

Browse files
RSNarafacebook-github-bot
authored andcommitted
RuntimeExecutor: Refine the variables and comments again
Summary: Just refactoring an renaming the variables inside RuntimeExecutor again (in a separate diff). Just so that the logic in the subsequent diffs is easier to read: D74769326 Changelog: [Internal] Differential Revision: D74941734
1 parent 50bbec2 commit e39a03a

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

packages/react-native/ReactCommon/runtimeexecutor/ReactCommon/RuntimeExecutor.h

+29-30
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,60 @@ namespace facebook::react {
2525
using RuntimeExecutor =
2626
std::function<void(std::function<void(jsi::Runtime& runtime)>&& callback)>;
2727

28-
/*
29-
* Executes a `callback` in a *synchronous* manner on the same thread using
30-
* given `RuntimeExecutor`.
31-
* Use this method when the caller needs to *be blocked* by executing the
32-
* `callback` and requires that the callback will be executed on the same
33-
* thread.
34-
* Example order of events (when not a sync call in runtimeExecutor callback):
28+
/**
29+
* Example order of events (when not a sync call in runtimeExecutor
30+
* jsWork):
3531
* - [UI thread] Lock all mutexes at start
36-
* - [UI thread] runtimeCaptured.lock before callback
37-
* - [JS thread] Set runtimePtr in runtimeExecutor callback
38-
* - [JS thread] runtimeCaptured.unlock in runtimeExecutor callback
39-
* - [UI thread] Call callback
40-
* - [JS thread] callbackExecuted.lock in runtimeExecutor callback
41-
* - [UI thread] callbackExecuted.unlock after callback
42-
* - [UI thread] jsBlockExecuted.lock after callback
43-
* - [JS thread] jsBlockExecuted.unlock in runtimeExecutor callback
32+
* - [UI thread] Schedule "runtime capture block" on js thread
33+
* - [UI thread] Wait for runtime capture: runtimeCaptured.lock()
34+
* - [JS thread] Capture runtime by setting runtimePtr
35+
* - [JS thread] Signal runtime captured: runtimeCaptured.unlock()
36+
* - [UI thread] Call jsWork using runtimePtr
37+
* - [JS thread] Wait until jsWork done: jsWorkDone.lock()
38+
* - [UI thread] Signal jsWork done: jsWorkDone.unlock()
39+
* - [UI thread] Wait until runtime capture block finished:
40+
* runtimeCaptureBlockDone.lock()
41+
* - [JS thread] Signal runtime capture block is finished:
42+
* runtimeCaptureBlockDone.unlock()
4443
*/
4544
inline static void executeSynchronouslyOnSameThread_CAN_DEADLOCK(
4645
const RuntimeExecutor& runtimeExecutor,
47-
std::function<void(jsi::Runtime& runtime)>&& callback) noexcept {
46+
std::function<void(jsi::Runtime& runtime)>&& jsWork) noexcept {
4847
// Note: We need the third mutex to get back to the main thread before
4948
// the lambda is finished (because all mutexes are allocated on the stack).
5049

5150
std::mutex runtimeCaptured;
52-
std::mutex callbackExecuted;
53-
std::mutex jsBlockExecuted;
51+
std::mutex jsWorkDone;
52+
std::mutex runtimeCaptureBlockDone;
5453

5554
runtimeCaptured.lock();
56-
callbackExecuted.lock();
57-
jsBlockExecuted.lock();
55+
jsWorkDone.lock();
56+
runtimeCaptureBlockDone.lock();
5857

5958
jsi::Runtime* runtimePtr;
6059

6160
auto threadId = std::this_thread::get_id();
62-
63-
runtimeExecutor([&](jsi::Runtime& runtime) {
61+
auto runtimeCaptureBlock = [&](jsi::Runtime& runtime) {
6462
runtimePtr = &runtime;
6563

6664
if (threadId == std::this_thread::get_id()) {
6765
// In case of a synchronous call, we should unlock mutexes and return.
6866
runtimeCaptured.unlock();
69-
jsBlockExecuted.unlock();
67+
runtimeCaptureBlockDone.unlock();
7068
return;
7169
}
7270

7371
runtimeCaptured.unlock();
74-
// `callback` is called somewhere here.
75-
callbackExecuted.lock();
76-
jsBlockExecuted.unlock();
77-
});
72+
// `jsWork` is called somewhere here.
73+
jsWorkDone.lock();
74+
runtimeCaptureBlockDone.unlock();
75+
};
76+
runtimeExecutor(std::move(runtimeCaptureBlock));
7877

7978
runtimeCaptured.lock();
80-
callback(*runtimePtr);
81-
callbackExecuted.unlock();
82-
jsBlockExecuted.lock();
79+
jsWork(*runtimePtr);
80+
jsWorkDone.unlock();
81+
runtimeCaptureBlockDone.lock();
8382
}
8483

8584
template <typename DataT>

0 commit comments

Comments
 (0)