Skip to content

Commit 18ef5d1

Browse files
tsaichienmeta-codesync[bot]
authored andcommitted
Add IEventLoopControl APIs to hermes-interface (#55431)
Summary: Pull Request resolved: #55431 Adds `ISetEventLoopControl` to the Hermes-specific JSI. This interface specifies an user-defined, thread-safe function to schedule some task provided by the Hermes VM. The Hermes VM may use this function to "ask" the integrator to run some arbitrary task when the integrator has exclusive control of the runtime. Notably, this is useful for the Hermes implementation of Workers, where the Worker thread may ask the integrator to process an event. Changelog: [Internal] Reviewed By: lavenzg Differential Revision: D91905969 fbshipit-source-id: c006d613862611bb01860a38a99c8881acd0355c
1 parent f119cdd commit 18ef5d1

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

packages/react-native/ReactCommon/jsi/jsi/hermes-interfaces.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,61 @@ namespace debugger {
2626
class Debugger;
2727
}
2828

29+
#ifdef JSI_UNSTABLE
30+
/// IEventLoopControl is defined by the integrator to allow the Runtime to
31+
/// schedule some task to be run when convenient, and to keep track of "Task
32+
/// sources". After it is set to a Runtime, the integrator must ensure that the
33+
/// `IEventLoopControl` instance outlives the Runtime. The IEventLoopControl
34+
/// methods may be called by the Runtime from any thread, so they must be
35+
/// thread-safe and cannot perform any VM operations.
36+
struct IEventLoopControl {
37+
/// `scheduleTask` is a function used by the caller (the Runtime) to schedule
38+
/// some \p task. The scheduled task may perform VM operations. Thus, the
39+
/// integrator must only run the tasks when it had exclusive access to the
40+
/// Runtime.
41+
virtual void scheduleTask(const std::function<void()>& task) = 0;
42+
/// Used by the caller (the Runtime) to register a new source that can
43+
/// schedule new work via `scheduleTask`. This method return an uint64
44+
/// identifying the source registered. As an example, a WebWorker instance may
45+
/// schedule a message-processing task via `scheduleTask`, and thus is
46+
/// considered as a "task queue source". The Runtime may register each Worker
47+
/// using this method. This is useful for the integrator to keep track of
48+
/// active "sources".
49+
virtual uint64_t registerTaskQueueSource() = 0;
50+
/// Used by the caller to unregister a source when it is not allowed to invoke
51+
/// `scheduleTasks` anymore. The source is identified by \p sourceId, which is
52+
/// provided when the source was originally register in
53+
/// `registerTaskQueueSource`. As an example, after WebWorker instance is
54+
/// terminated, it will not schedule more tasks. The Runtime may unregister
55+
/// the Worker instance, and the integrator may exit the event-loop if there
56+
/// are no more active sources.
57+
virtual void unregisterTaskQueueSource(uint64_t sourceId) = 0;
58+
59+
protected:
60+
~IEventLoopControl() = default;
61+
};
62+
63+
/// Interface for setting the IEventLoopControl in the Runtime.
64+
struct JSI_EXPORT ISetEventLoopControl : public jsi::ICast {
65+
public:
66+
static constexpr jsi::UUID uuid{
67+
0x7b6902e6,
68+
0xfd38,
69+
0x11f0,
70+
0x8de9,
71+
0x0242ac120002};
72+
73+
/// Configures the eventloop control mechanism using \p eventLoopControl.
74+
virtual void setEventLoopControl(IEventLoopControl* eventLoopControl) = 0;
75+
/// Retrieves the IEventLoopControl if it was set previously. Otherwise,
76+
/// return nullptr.
77+
virtual IEventLoopControl* getEventLoopControl() = 0;
78+
79+
protected:
80+
~ISetEventLoopControl() = default;
81+
};
82+
#endif
83+
2984
/// Interface for Hermes-specific runtime methods.The actual implementations of
3085
/// the pure virtual methods are provided by Hermes API.
3186
class JSI_EXPORT IHermes : public jsi::ICast {

0 commit comments

Comments
 (0)