Skip to content

Commit

Permalink
Mark AsyncCallback as noexcept (#40745)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #40745

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D49886196

fbshipit-source-id: 6b4fa888f6cffbf7863167396d13c4a00bf485a2
  • Loading branch information
javache authored and facebook-github-bot committed Oct 10, 2023
1 parent 5cec1ea commit 44f5989
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ CatalystInstanceImpl::getNativeMethodCallInvokerHolder() {
: messageQueueThread_(messageQueueThread) {}
void invokeAsync(
const std::string& methodName,
std::function<void()>&& work) override {
std::function<void()>&& work) noexcept override {
messageQueueThread_->runOnQueue(std::move(work));
}
void invokeSync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ using CallFunc = std::function<void()>;
*/
class CallInvoker {
public:
virtual void invokeAsync(CallFunc&& func) = 0;
virtual void invokeAsync(SchedulerPriority /*priority*/, CallFunc&& func) {
virtual void invokeAsync(CallFunc&& func) noexcept = 0;
virtual void invokeAsync(
SchedulerPriority /*priority*/,
CallFunc&& func) noexcept {
// When call with priority is not implemented, fall back to a regular async
// execution
invokeAsync(std::move(func));
Expand All @@ -34,7 +36,9 @@ class CallInvoker {

class NativeMethodCallInvoker {
public:
virtual void invokeAsync(const std::string& methodName, CallFunc&& func) = 0;
virtual void invokeAsync(
const std::string& methodName,
CallFunc&& func) noexcept = 0;
virtual void invokeSync(const std::string& methodName, CallFunc&& func) = 0;
virtual ~NativeMethodCallInvoker() {}
};
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native/ReactCommon/cxxreact/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ void Instance::JSCallInvoker::invokeSync(std::function<void()>&& work) {
"Synchronous native -> JS calls are currently not supported.");
}

void Instance::JSCallInvoker::invokeAsync(std::function<void()>&& work) {
void Instance::JSCallInvoker::invokeAsync(
std::function<void()>&& work) noexcept {
std::scoped_lock guard(m_mutex);

/**
Expand All @@ -288,7 +289,8 @@ void Instance::JSCallInvoker::invokeAsync(std::function<void()>&& work) {
scheduleAsync(std::move(work));
}

void Instance::JSCallInvoker::scheduleAsync(std::function<void()>&& work) {
void Instance::JSCallInvoker::scheduleAsync(
std::function<void()>&& work) noexcept {
if (auto strongNativeToJsBridge = m_nativeToJsBridge.lock()) {
strongNativeToJsBridge->runOnExecutorQueue(
[work = std::move(work)](JSExecutor* executor) {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/ReactCommon/cxxreact/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ class RN_EXPORT Instance {
bool m_shouldBuffer = true;
std::list<std::function<void()>> m_workBuffer;

void scheduleAsync(std::function<void()>&& work);
void scheduleAsync(std::function<void()>&& work) noexcept;

public:
void setNativeToJsBridgeAndFlushCalls(
std::weak_ptr<NativeToJsBridge> nativeToJsBridge);
void invokeAsync(std::function<void()>&& work) override;
void invokeAsync(std::function<void()>&& work) noexcept override;
void invokeSync(std::function<void()>&& work) override;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class JsToNativeBridge : public react::ExecutorDelegate {
moduleId, methodId, std::move(args));
}

void recordTurboModuleAsyncMethodCall() {
void recordTurboModuleAsyncMethodCall() noexcept {
m_batchHadNativeModuleOrTurboModuleCalls = true;
}

Expand Down Expand Up @@ -288,7 +288,7 @@ void NativeToJsBridge::destroy() {
}

void NativeToJsBridge::runOnExecutorQueue(
std::function<void(JSExecutor*)> task) {
std::function<void(JSExecutor*)>&& task) noexcept {
if (*m_destroyed) {
return;
}
Expand Down Expand Up @@ -326,7 +326,7 @@ NativeToJsBridge::getDecoratedNativeMethodCallInvoker(

void invokeAsync(
const std::string& methodName,
std::function<void()>&& func) override {
std::function<void()>&& func) noexcept override {
if (auto strongJsToNativeBridge = m_jsToNativeBridge.lock()) {
strongJsToNativeBridge->recordTurboModuleAsyncMethodCall();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class NativeToJsBridge {
*/
void destroy();

void runOnExecutorQueue(std::function<void(JSExecutor*)> task);
void runOnExecutorQueue(std::function<void(JSExecutor*)>&& task) noexcept;

/**
* NativeMethodCallInvoker is used by TurboModules to schedule work on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ class CallbackWrapper : public LongLivedObject {
allowRelease();
}

jsi::Function& callback() {
jsi::Function& callback() noexcept {
return callback_;
}

jsi::Runtime& runtime() {
jsi::Runtime& runtime() noexcept {
return runtime_;
}

CallInvoker& jsInvoker() {
CallInvoker& jsInvoker() noexcept {
return *(jsInvoker_);
}

std::shared_ptr<CallInvoker> jsInvokerPtr() {
std::shared_ptr<CallInvoker> jsInvokerPtr() noexcept {
return jsInvoker_;
}
};
Expand Down
19 changes: 11 additions & 8 deletions packages/react-native/ReactCommon/react/bridging/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,28 @@ class AsyncCallback {
std::move(function),
std::move(jsInvoker))) {}

void operator()(Args... args) const {
void operator()(Args... args) const noexcept {
call(std::forward<Args>(args)...);
}

void call(Args... args) const {
void call(Args... args) const noexcept {
callWithArgs(std::nullopt, std::forward<Args>(args)...);
}

void callWithPriority(SchedulerPriority priority, Args... args) const {
void callWithPriority(SchedulerPriority priority, Args... args)
const noexcept {
callWithArgs(priority, std::forward<Args>(args)...);
}

void call(
std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl) const {
void call(std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl)
const noexcept {
callWithFunction(std::nullopt, std::move(callImpl));
}

void callWithPriority(
SchedulerPriority priority,
std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl) const {
std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl)
const noexcept {
callWithFunction(priority, std::move(callImpl));
}

Expand All @@ -58,7 +60,7 @@ class AsyncCallback {
std::shared_ptr<SyncCallback<void(Args...)>> callback_;

void callWithArgs(std::optional<SchedulerPriority> priority, Args... args)
const {
const noexcept {
auto wrapper = callback_->wrapper_.lock();
if (wrapper) {
auto& jsInvoker = wrapper->jsInvoker();
Expand All @@ -78,7 +80,8 @@ class AsyncCallback {

void callWithFunction(
std::optional<SchedulerPriority> priority,
std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl) const {
std::function<void(jsi::Runtime&, jsi::Function&)>&& callImpl)
const noexcept {
auto wrapper = callback_->wrapper_.lock();
if (wrapper) {
auto& jsInvoker = wrapper->jsInvoker();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace facebook::react {

class TestCallInvoker : public CallInvoker {
public:
void invokeAsync(std::function<void()>&& fn) override {
void invokeAsync(std::function<void()>&& fn) noexcept override {
queue_.push_back(std::move(fn));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ bool isCreatingModule() const

public:
ModuleNativeMethodCallInvoker(dispatch_queue_t methodQueue) : methodQueue_(methodQueue) {}
void invokeAsync(const std::string &methodName, std::function<void()> &&work) override
void invokeAsync(const std::string &methodName, std::function<void()> &&work) noexcept override
{
if (methodQueue_ == RCTJSThread) {
work();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RuntimeScheduler::RuntimeScheduler(
std::function<RuntimeSchedulerTimePoint()> now)
: runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {}

void RuntimeScheduler::scheduleWork(RawCallback callback) const {
void RuntimeScheduler::scheduleWork(RawCallback&& callback) const noexcept {
SystraceSection s("RuntimeScheduler::scheduleWork");

runtimeAccessRequests_ += 1;
Expand All @@ -37,7 +37,7 @@ void RuntimeScheduler::scheduleWork(RawCallback callback) const {

std::shared_ptr<Task> RuntimeScheduler::scheduleTask(
SchedulerPriority priority,
jsi::Function callback) {
jsi::Function&& callback) noexcept {
auto expirationTime = now_() + timeoutForSchedulerPriority(priority);
auto task =
std::make_shared<Task>(priority, std::move(callback), expirationTime);
Expand All @@ -50,7 +50,7 @@ std::shared_ptr<Task> RuntimeScheduler::scheduleTask(

std::shared_ptr<Task> RuntimeScheduler::scheduleTask(
SchedulerPriority priority,
RawCallback callback) {
RawCallback&& callback) noexcept {
auto expirationTime = now_() + timeoutForSchedulerPriority(priority);
auto task =
std::make_shared<Task>(priority, std::move(callback), expirationTime);
Expand Down Expand Up @@ -81,7 +81,7 @@ RuntimeSchedulerTimePoint RuntimeScheduler::now() const noexcept {
return now_();
}

void RuntimeScheduler::executeNowOnTheSameThread(RawCallback callback) {
void RuntimeScheduler::executeNowOnTheSameThread(RawCallback&& callback) {
SystraceSection s("RuntimeScheduler::executeNowOnTheSameThread");

runtimeAccessRequests_ += 1;
Expand Down Expand Up @@ -166,7 +166,7 @@ void RuntimeScheduler::startWorkLoop(jsi::Runtime& runtime) const {

void RuntimeScheduler::executeTask(
jsi::Runtime& runtime,
std::shared_ptr<Task> task,
const std::shared_ptr<Task>& task,
bool didUserCallbackTimeout) const {
SystraceSection s(
"RuntimeScheduler::executeTask",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RuntimeScheduler final {
RuntimeScheduler(RuntimeScheduler&&) = delete;
RuntimeScheduler& operator=(RuntimeScheduler&&) = delete;

void scheduleWork(RawCallback callback) const;
void scheduleWork(RawCallback&& callback) const noexcept;

/*
* Grants access to the runtime synchronously on the caller's thread.
Expand All @@ -43,7 +43,7 @@ class RuntimeScheduler final {
* by dispatching a synchronous event via event emitter in your native
* component.
*/
void executeNowOnTheSameThread(RawCallback callback);
void executeNowOnTheSameThread(RawCallback&& callback);

/*
* Adds a JavaScript callback to priority queue with given priority.
Expand All @@ -53,11 +53,11 @@ class RuntimeScheduler final {
*/
std::shared_ptr<Task> scheduleTask(
SchedulerPriority priority,
jsi::Function callback);
jsi::Function&& callback) noexcept;

std::shared_ptr<Task> scheduleTask(
SchedulerPriority priority,
RawCallback callback);
RawCallback&& callback) noexcept;

/*
* Cancelled task will never be executed.
Expand Down Expand Up @@ -135,7 +135,7 @@ class RuntimeScheduler final {

void executeTask(
jsi::Runtime& runtime,
std::shared_ptr<Task> task,
const std::shared_ptr<Task>& task,
bool didUserCallbackTimeout) const;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RuntimeSchedulerCallInvoker::RuntimeSchedulerCallInvoker(
std::weak_ptr<RuntimeScheduler> runtimeScheduler)
: runtimeScheduler_(std::move(runtimeScheduler)) {}

void RuntimeSchedulerCallInvoker::invokeAsync(CallFunc&& func) {
void RuntimeSchedulerCallInvoker::invokeAsync(CallFunc&& func) noexcept {
if (auto runtimeScheduler = runtimeScheduler_.lock()) {
runtimeScheduler->scheduleWork(
[func = std::move(func)](jsi::Runtime&) { func(); });
Expand All @@ -32,7 +32,7 @@ void RuntimeSchedulerCallInvoker::invokeSync(CallFunc&& func) {

void RuntimeSchedulerCallInvoker::invokeAsync(
SchedulerPriority priority,
CallFunc&& func) {
CallFunc&& func) noexcept {
if (auto runtimeScheduler = runtimeScheduler_.lock()) {
runtimeScheduler->scheduleTask(
priority, [func = std::move(func)](jsi::Runtime&) { func(); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class RuntimeSchedulerCallInvoker : public CallInvoker {
public:
RuntimeSchedulerCallInvoker(std::weak_ptr<RuntimeScheduler> runtimeScheduler);

void invokeAsync(CallFunc&& func) override;
void invokeAsync(CallFunc&& func) noexcept override;
void invokeSync(CallFunc&& func) override;
void invokeAsync(SchedulerPriority priority, CallFunc&& func) override;
void invokeAsync(SchedulerPriority priority, CallFunc&& func) noexcept
override;

private:
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static inline SchedulerPriority fromRawValue(double value) {
}

static inline std::chrono::milliseconds timeoutForSchedulerPriority(
SchedulerPriority schedulerPriority) {
SchedulerPriority schedulerPriority) noexcept {
switch (schedulerPriority) {
case SchedulerPriority::ImmediatePriority:
return std::chrono::milliseconds(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace facebook::react {

Task::Task(
SchedulerPriority priority,
jsi::Function callback,
jsi::Function&& callback,
std::chrono::steady_clock::time_point expirationTime)
: priority(priority),
callback(std::move(callback)),
expirationTime(expirationTime) {}

Task::Task(
SchedulerPriority priority,
RawCallback callback,
RawCallback&& callback,
std::chrono::steady_clock::time_point expirationTime)
: priority(priority),
callback(std::move(callback)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ using RawCallback = std::function<void(jsi::Runtime&)>;
struct Task final : public jsi::NativeState {
Task(
SchedulerPriority priority,
jsi::Function callback,
jsi::Function&& callback,
std::chrono::steady_clock::time_point expirationTime);

Task(
SchedulerPriority priority,
RawCallback callback,
RawCallback&& callback,
std::chrono::steady_clock::time_point expirationTime);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ BridgelessJSCallInvoker::BridgelessJSCallInvoker(
RuntimeExecutor runtimeExecutor)
: runtimeExecutor_(std::move(runtimeExecutor)) {}

void BridgelessJSCallInvoker::invokeAsync(std::function<void()>&& func) {
void BridgelessJSCallInvoker::invokeAsync(
std::function<void()>&& func) noexcept {
runtimeExecutor_([func = std::move(func)](jsi::Runtime& runtime) { func(); });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace facebook::react {
class BridgelessJSCallInvoker : public CallInvoker {
public:
explicit BridgelessJSCallInvoker(RuntimeExecutor runtimeExecutor);
void invokeAsync(std::function<void()>&& func) override;
void invokeAsync(std::function<void()>&& func) noexcept override;
void invokeSync(std::function<void()>&& func) override;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BridgelessNativeMethodCallInvoker::BridgelessNativeMethodCallInvoker(

void BridgelessNativeMethodCallInvoker::invokeAsync(
const std::string& methodName,
std::function<void()>&& func) {
std::function<void()>&& func) noexcept {
messageQueueThread_->runOnQueue(std::move(func));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class BridgelessNativeMethodCallInvoker : public NativeMethodCallInvoker {
public:
explicit BridgelessNativeMethodCallInvoker(
std::shared_ptr<MessageQueueThread> messageQueueThread);
void invokeAsync(const std::string& methodName, std::function<void()>&& func)
override;
void invokeAsync(
const std::string& methodName,
std::function<void()>&& func) noexcept override;
void invokeSync(const std::string& methodName, std::function<void()>&& func)
override;

Expand Down

0 comments on commit 44f5989

Please sign in to comment.