mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Define main entities (#54334)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54334 # Changelog: [Internal] This defines the main entities for the console.createTask(): - ConsoleTaskOrchestrator: global stack that can be used as a source of pending tasks. - ConsoleTaskContext: RAII object, captures the context for a specific task. Lifetime is bound to the lifetime of the task object in JavaScript. - ConsoleTask: RAII-like object. Initialized only during the callback run of `task.run(...)`. I couldn't find a better way to solve this without having a static singleton. Native modules don't have access to ReactInstance object, so we won't be able to access this global stack from `performance.measure` implementation, for example. Not using the word `async` anywhere in the naming, because the current implementation doesn't support async stack traces. Reviewed By: sbuggay Differential Revision: D85481864 fbshipit-source-id: 4a0b317cccb8c2492647f06472b00fc0daa2c8a3
This commit is contained in:
1 parent
72834e6326
commit
98be2d2f92
6 files changed
+332
No files matched your search
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "ConsoleTask.h"
|
||||
#include "ConsoleTaskOrchestrator.h"
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
ConsoleTask::ConsoleTask(std::shared_ptr<ConsoleTaskContext> taskContext)
|
||||
: taskContext_(std::move(taskContext)),
|
||||
orchestrator_(ConsoleTaskOrchestrator::getInstance()) {
|
||||
if (taskContext_) {
|
||||
orchestrator_.startTask(taskContext_->id());
|
||||
}
|
||||
}
|
||||
|
||||
ConsoleTask::~ConsoleTask() {
|
||||
if (taskContext_) {
|
||||
orchestrator_.finishTask(taskContext_->id());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
class ConsoleTaskContext;
|
||||
class RuntimeTargetDelegate;
|
||||
class ConsoleTaskOrchestrator;
|
||||
|
||||
class ConsoleTask {
|
||||
public:
|
||||
/**
|
||||
* \param runtimeTargetDelegate The delegate to the corresponding runtime.
|
||||
* \param taskContext The context that tracks the task.
|
||||
*/
|
||||
explicit ConsoleTask(std::shared_ptr<ConsoleTaskContext> taskContext);
|
||||
~ConsoleTask();
|
||||
|
||||
ConsoleTask(const ConsoleTask &) = default;
|
||||
ConsoleTask &operator=(const ConsoleTask &) = delete;
|
||||
|
||||
ConsoleTask(ConsoleTask &&) = default;
|
||||
ConsoleTask &operator=(ConsoleTask &&) = delete;
|
||||
|
||||
private:
|
||||
std::shared_ptr<ConsoleTaskContext> taskContext_;
|
||||
ConsoleTaskOrchestrator &orchestrator_;
|
||||
};
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "ConsoleTaskContext.h"
|
||||
#include "ConsoleTaskOrchestrator.h"
|
||||
#include "RuntimeTarget.h"
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
ConsoleTaskContext::ConsoleTaskContext(
|
||||
jsi::Runtime& runtime,
|
||||
RuntimeTargetDelegate& runtimeTargetDelegate,
|
||||
std::string name)
|
||||
: runtimeTargetDelegate_(runtimeTargetDelegate),
|
||||
name_(std::move(name)),
|
||||
orchestrator_(ConsoleTaskOrchestrator::getInstance()) {
|
||||
stackTrace_ = runtimeTargetDelegate_.captureStackTrace(runtime);
|
||||
}
|
||||
|
||||
ConsoleTaskContext::~ConsoleTaskContext() {
|
||||
orchestrator_.cancelTask(id());
|
||||
}
|
||||
|
||||
ConsoleTaskId ConsoleTaskContext::id() const {
|
||||
return ConsoleTaskId{(void*)this};
|
||||
}
|
||||
|
||||
std::optional<folly::dynamic> ConsoleTaskContext::getSerializedStackTrace()
|
||||
const {
|
||||
auto maybeValue = runtimeTargetDelegate_.serializeStackTrace(*stackTrace_);
|
||||
if (maybeValue) {
|
||||
maybeValue.value()["description"] = name_;
|
||||
}
|
||||
|
||||
return maybeValue;
|
||||
}
|
||||
|
||||
std::function<std::optional<folly::dynamic>()>
|
||||
ConsoleTaskContext::getSerializedStackTraceProvider() const {
|
||||
return [selfWeak = weak_from_this()]() -> std::optional<folly::dynamic> {
|
||||
if (auto self = selfWeak.lock()) {
|
||||
return self->getSerializedStackTrace();
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
};
|
||||
}
|
||||
|
||||
void ConsoleTaskContext::schedule() {
|
||||
orchestrator_.scheduleTask(id(), weak_from_this());
|
||||
}
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <folly/dynamic.h>
|
||||
#include <jsi/jsi.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
class ConsoleTaskOrchestrator;
|
||||
class RuntimeTargetDelegate;
|
||||
|
||||
class ConsoleTaskId {
|
||||
public:
|
||||
ConsoleTaskId() = default;
|
||||
~ConsoleTaskId() = default;
|
||||
|
||||
ConsoleTaskId(const ConsoleTaskId &) = default;
|
||||
ConsoleTaskId &operator=(const ConsoleTaskId &) = default;
|
||||
|
||||
ConsoleTaskId(ConsoleTaskId &&) = default;
|
||||
ConsoleTaskId &operator=(ConsoleTaskId &&) = default;
|
||||
|
||||
bool operator==(const ConsoleTaskId &) const = default;
|
||||
inline operator bool() const
|
||||
{
|
||||
return (bool)id_;
|
||||
}
|
||||
|
||||
explicit inline operator void *() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
private:
|
||||
explicit inline ConsoleTaskId(void *id) : id_(id)
|
||||
{
|
||||
assert(id_ != nullptr);
|
||||
}
|
||||
|
||||
void *id_{nullptr};
|
||||
|
||||
friend class ConsoleTaskContext;
|
||||
};
|
||||
|
||||
class ConsoleTaskContext : public std::enable_shared_from_this<ConsoleTaskContext> {
|
||||
public:
|
||||
ConsoleTaskContext(jsi::Runtime &runtime, RuntimeTargetDelegate &runtimeTargetDelegate, std::string name);
|
||||
~ConsoleTaskContext();
|
||||
|
||||
// Can't be moved or copied: the address of `ConsoleTaskContext` is used to
|
||||
// identify this task and all corresponding invocations.
|
||||
ConsoleTaskContext(const ConsoleTaskContext &) = delete;
|
||||
ConsoleTaskContext &operator=(const ConsoleTaskContext &) = delete;
|
||||
|
||||
ConsoleTaskContext(ConsoleTaskContext &&) = delete;
|
||||
ConsoleTaskContext &operator=(ConsoleTaskContext &&) = delete;
|
||||
|
||||
/**
|
||||
* Unique identifier that is calculated based on the address of
|
||||
* ConsoleTaskContext.
|
||||
*/
|
||||
ConsoleTaskId id() const;
|
||||
|
||||
/**
|
||||
* Returns the serialized stack trace that was captured during the allocation
|
||||
* of ConsoleTaskContext.
|
||||
*/
|
||||
std::optional<folly::dynamic> getSerializedStackTrace() const;
|
||||
|
||||
/**
|
||||
* Returns a function that returns the serialized stack trace, if available.
|
||||
*/
|
||||
std::function<std::optional<folly::dynamic>()> getSerializedStackTraceProvider() const;
|
||||
|
||||
void schedule();
|
||||
|
||||
private:
|
||||
RuntimeTargetDelegate &runtimeTargetDelegate_;
|
||||
std::string name_;
|
||||
ConsoleTaskOrchestrator &orchestrator_;
|
||||
std::unique_ptr<StackTrace> stackTrace_;
|
||||
};
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct hash<facebook::react::jsinspector_modern::ConsoleTaskId> {
|
||||
size_t operator()(const facebook::react::jsinspector_modern::ConsoleTaskId &id) const
|
||||
{
|
||||
return std::hash<void *>{}(static_cast<void *>(id));
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "ConsoleTaskOrchestrator.h"
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
/* static */ ConsoleTaskOrchestrator& ConsoleTaskOrchestrator::getInstance() {
|
||||
static ConsoleTaskOrchestrator instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void ConsoleTaskOrchestrator::scheduleTask(
|
||||
ConsoleTaskId taskId,
|
||||
std::weak_ptr<ConsoleTaskContext> taskContext) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
tasks_.emplace(taskId, taskContext);
|
||||
}
|
||||
|
||||
void ConsoleTaskOrchestrator::cancelTask(ConsoleTaskId id) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
tasks_.erase(id);
|
||||
}
|
||||
|
||||
void ConsoleTaskOrchestrator::startTask(ConsoleTaskId id) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
stack_.push(id);
|
||||
}
|
||||
|
||||
void ConsoleTaskOrchestrator::finishTask(ConsoleTaskId id) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
assert(stack_.top() == id);
|
||||
|
||||
stack_.pop();
|
||||
}
|
||||
|
||||
std::shared_ptr<ConsoleTaskContext> ConsoleTaskOrchestrator::top() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (stack_.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto it = tasks_.find(stack_.top());
|
||||
if (it == tasks_.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return it->second.lock();
|
||||
}
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stack>
|
||||
|
||||
#include "ConsoleTaskContext.h"
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
class ConsoleTaskOrchestrator {
|
||||
public:
|
||||
static ConsoleTaskOrchestrator &getInstance();
|
||||
|
||||
~ConsoleTaskOrchestrator() = default;
|
||||
|
||||
ConsoleTaskOrchestrator(const ConsoleTaskOrchestrator &) = delete;
|
||||
ConsoleTaskOrchestrator &operator=(const ConsoleTaskOrchestrator &) = delete;
|
||||
|
||||
ConsoleTaskOrchestrator(ConsoleTaskOrchestrator &&) = delete;
|
||||
ConsoleTaskOrchestrator &operator=(ConsoleTaskOrchestrator &&) = delete;
|
||||
|
||||
void scheduleTask(ConsoleTaskId taskId, std::weak_ptr<ConsoleTaskContext> taskContext);
|
||||
void cancelTask(ConsoleTaskId taskId);
|
||||
|
||||
void startTask(ConsoleTaskId taskId);
|
||||
void finishTask(ConsoleTaskId taskId);
|
||||
std::shared_ptr<ConsoleTaskContext> top() const;
|
||||
|
||||
private:
|
||||
ConsoleTaskOrchestrator() = default;
|
||||
|
||||
std::stack<ConsoleTaskId> stack_;
|
||||
std::unordered_map<ConsoleTaskId, std::weak_ptr<ConsoleTaskContext>> tasks_;
|
||||
/**
|
||||
* Protects the stack_ and tasks_ members.
|
||||
*/
|
||||
mutable std::mutex mutex_;
|
||||
};
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
Reference in new issue
Block a user