Fabric: Initializing EventBeat::OwnerBox with non-null value ahead of time

Summary:
We use `EventBeat::OwnerBox` in the `EventBeat` infra to represent weak ownership of classes that contain that, so those classes can ensure own life-time at some critical points.
Before this diff, we stored a pointer to EventDispatcher right after we create it. However, some components that we build require to have a valid pointer from the beginning (even before the EventDispatcher is created). To satisfy this requirement, we create a dummy pointer first and use it as an owner, then we merge (share control block) the pointer to an EventDispatcher into that. It works because the owner pointer never gets dereferenced (it's `void *`), so it does not matter which object it represents while it represents correct ownership.

In the future, this approach will allow us to remove the concept of `OwnerBox` completely and use just `std::weak_ptr<void>` instead.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: mdvacca

Differential Revision: D21441109

fbshipit-source-id: 7457c77bd31cd577f38a26e28e27eb7e33b6ad24
This commit is contained in:
Valentin Shergin
2020-05-10 21:29:32 -07:00
committed by Facebook GitHub Bot
parent b65b00ab6b
commit ebdd59aff8
+8 -2
View File
@@ -32,6 +32,11 @@ Scheduler::Scheduler(
auto uiManager = std::make_shared<UIManager>();
auto eventOwnerBox = std::make_shared<EventBeat::OwnerBox>();
// A dummy pointer to share a control block (and life-time) with
// an actual `owner` later.
auto owner = std::make_shared<bool const>(false);
eventOwnerBox->owner = owner;
auto eventPipe = [uiManager](
jsi::Runtime &runtime,
const EventTarget *eventTarget,
@@ -47,14 +52,15 @@ Scheduler::Scheduler(
uiManager->updateState(stateUpdate);
};
eventDispatcher_ = std::make_shared<EventDispatcher>(
auto eventDispatcher = std::make_unique<EventDispatcher const>(
eventPipe,
statePipe,
schedulerToolbox.synchronousEventBeatFactory,
schedulerToolbox.asynchronousEventBeatFactory,
eventOwnerBox);
eventOwnerBox->owner = eventDispatcher_;
eventDispatcher_ =
std::shared_ptr<EventDispatcher const>(owner, eventDispatcher.release());
componentDescriptorRegistry_ = schedulerToolbox.componentRegistryFactory(
eventDispatcher_, schedulerToolbox.contextContainer);