Skip to content

Commit 87dbe44

Browse files
rubennortefacebook-github-bot
authored andcommitted
Clean up const and mutable modifiers in RuntimeScheduler (#41028)
Summary: Pull Request resolved: #41028 This removes misleading `const` modifiers from some methods in `RuntimeScheduler` that shouldn't really use it, and removes the `mutable` modifiers that were only necessary because of that. Changelog: [internal] Reviewed By: sammy-SC Differential Revision: D50364626 fbshipit-source-id: 28ed9fa923f8e787166f702ccaecd41a635d3b3a
1 parent 87c08df commit 87dbe44

File tree

6 files changed

+49
-57
lines changed

6 files changed

+49
-57
lines changed

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ RuntimeScheduler::RuntimeScheduler(
4040
useModernRuntimeScheduler,
4141
std::move(now))) {}
4242

43-
void RuntimeScheduler::scheduleWork(RawCallback&& callback) const noexcept {
43+
void RuntimeScheduler::scheduleWork(RawCallback&& callback) noexcept {
4444
return runtimeSchedulerImpl_->scheduleWork(std::move(callback));
4545
}
4646

4747
std::shared_ptr<Task> RuntimeScheduler::scheduleTask(
4848
SchedulerPriority priority,
49-
jsi::Function&& callback) const noexcept {
49+
jsi::Function&& callback) noexcept {
5050
return runtimeSchedulerImpl_->scheduleTask(priority, std::move(callback));
5151
}
5252

5353
std::shared_ptr<Task> RuntimeScheduler::scheduleTask(
5454
SchedulerPriority priority,
55-
RawCallback&& callback) const noexcept {
55+
RawCallback&& callback) noexcept {
5656
return runtimeSchedulerImpl_->scheduleTask(priority, std::move(callback));
5757
}
5858

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@ namespace facebook::react {
1818
class RuntimeSchedulerBase {
1919
public:
2020
virtual ~RuntimeSchedulerBase() = default;
21-
// FIXME(T167271466): remove `const` modified when the RuntimeScheduler
22-
// refactor has been shipped.
23-
virtual void scheduleWork(RawCallback&& callback) const noexcept = 0;
21+
virtual void scheduleWork(RawCallback&& callback) noexcept = 0;
2422
virtual void executeNowOnTheSameThread(RawCallback&& callback) = 0;
25-
// FIXME(T167271466): remove `const` modified when the RuntimeScheduler
26-
// refactor has been shipped.
2723
virtual std::shared_ptr<Task> scheduleTask(
2824
SchedulerPriority priority,
29-
jsi::Function&& callback) const noexcept = 0;
30-
// FIXME(T167271466): remove `const` modified when the RuntimeScheduler
31-
// refactor has been shipped.
25+
jsi::Function&& callback) noexcept = 0;
3226
virtual std::shared_ptr<Task> scheduleTask(
3327
SchedulerPriority priority,
34-
RawCallback&& callback) const noexcept = 0;
28+
RawCallback&& callback) noexcept = 0;
3529
virtual void cancelTask(Task& task) noexcept = 0;
3630
virtual bool getShouldYield() const noexcept = 0;
3731
virtual bool getIsSynchronous() const noexcept = 0;
@@ -62,7 +56,7 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
6256
RuntimeScheduler(RuntimeScheduler&&) = delete;
6357
RuntimeScheduler& operator=(RuntimeScheduler&&) = delete;
6458

65-
void scheduleWork(RawCallback&& callback) const noexcept override;
59+
void scheduleWork(RawCallback&& callback) noexcept override;
6660

6761
/*
6862
* Grants access to the runtime synchronously on the caller's thread.
@@ -81,11 +75,11 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
8175
*/
8276
std::shared_ptr<Task> scheduleTask(
8377
SchedulerPriority priority,
84-
jsi::Function&& callback) const noexcept override;
78+
jsi::Function&& callback) noexcept override;
8579

8680
std::shared_ptr<Task> scheduleTask(
8781
SchedulerPriority priority,
88-
RawCallback&& callback) const noexcept override;
82+
RawCallback&& callback) noexcept override;
8983

9084
/*
9185
* Cancelled task will never be executed.

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Legacy.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ RuntimeScheduler_Legacy::RuntimeScheduler_Legacy(
2121
std::function<RuntimeSchedulerTimePoint()> now)
2222
: runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {}
2323

24-
void RuntimeScheduler_Legacy::scheduleWork(
25-
RawCallback&& callback) const noexcept {
24+
void RuntimeScheduler_Legacy::scheduleWork(RawCallback&& callback) noexcept {
2625
SystraceSection s("RuntimeScheduler::scheduleWork");
2726

2827
runtimeAccessRequests_ += 1;
@@ -38,7 +37,7 @@ void RuntimeScheduler_Legacy::scheduleWork(
3837

3938
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleTask(
4039
SchedulerPriority priority,
41-
jsi::Function&& callback) const noexcept {
40+
jsi::Function&& callback) noexcept {
4241
SystraceSection s(
4342
"RuntimeScheduler::scheduleTask",
4443
"priority",
@@ -58,7 +57,7 @@ std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleTask(
5857

5958
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleTask(
6059
SchedulerPriority priority,
61-
RawCallback&& callback) const noexcept {
60+
RawCallback&& callback) noexcept {
6261
SystraceSection s(
6362
"RuntimeScheduler::scheduleTask",
6463
"priority",
@@ -145,7 +144,7 @@ void RuntimeScheduler_Legacy::callExpiredTasks(jsi::Runtime& runtime) {
145144

146145
#pragma mark - Private
147146

148-
void RuntimeScheduler_Legacy::scheduleWorkLoopIfNecessary() const {
147+
void RuntimeScheduler_Legacy::scheduleWorkLoopIfNecessary() {
149148
if (!isWorkLoopScheduled_ && !isPerformingWork_) {
150149
isWorkLoopScheduled_ = true;
151150
runtimeExecutor_([this](jsi::Runtime& runtime) {
@@ -155,7 +154,7 @@ void RuntimeScheduler_Legacy::scheduleWorkLoopIfNecessary() const {
155154
}
156155
}
157156

158-
void RuntimeScheduler_Legacy::startWorkLoop(jsi::Runtime& runtime) const {
157+
void RuntimeScheduler_Legacy::startWorkLoop(jsi::Runtime& runtime) {
159158
SystraceSection s("RuntimeScheduler::startWorkLoop");
160159

161160
auto previousPriority = currentPriority_;
@@ -184,7 +183,7 @@ void RuntimeScheduler_Legacy::startWorkLoop(jsi::Runtime& runtime) const {
184183
void RuntimeScheduler_Legacy::executeTask(
185184
jsi::Runtime& runtime,
186185
const std::shared_ptr<Task>& task,
187-
bool didUserCallbackTimeout) const {
186+
bool didUserCallbackTimeout) {
188187
SystraceSection s(
189188
"RuntimeScheduler::executeTask",
190189
"priority",

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Legacy.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
3636
RuntimeScheduler_Legacy(RuntimeScheduler_Legacy&&) = delete;
3737
RuntimeScheduler_Legacy& operator=(RuntimeScheduler_Legacy&&) = delete;
3838

39-
void scheduleWork(RawCallback&& callback) const noexcept override;
39+
void scheduleWork(RawCallback&& callback) noexcept override;
4040

4141
/*
4242
* Grants access to the runtime synchronously on the caller's thread.
@@ -55,11 +55,11 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
5555
*/
5656
std::shared_ptr<Task> scheduleTask(
5757
SchedulerPriority priority,
58-
jsi::Function&& callback) const noexcept override;
58+
jsi::Function&& callback) noexcept override;
5959

6060
std::shared_ptr<Task> scheduleTask(
6161
SchedulerPriority priority,
62-
RawCallback&& callback) const noexcept override;
62+
RawCallback&& callback) noexcept override;
6363

6464
/*
6565
* Cancelled task will never be executed.
@@ -111,34 +111,34 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
111111
void callExpiredTasks(jsi::Runtime& runtime) override;
112112

113113
private:
114-
mutable std::priority_queue<
114+
std::priority_queue<
115115
std::shared_ptr<Task>,
116116
std::vector<std::shared_ptr<Task>>,
117117
TaskPriorityComparer>
118118
taskQueue_;
119119

120120
const RuntimeExecutor runtimeExecutor_;
121-
mutable SchedulerPriority currentPriority_{SchedulerPriority::NormalPriority};
121+
SchedulerPriority currentPriority_{SchedulerPriority::NormalPriority};
122122

123123
/*
124124
* Counter indicating how many access to the runtime have been requested.
125125
*/
126-
mutable std::atomic<uint_fast8_t> runtimeAccessRequests_{0};
126+
std::atomic<uint_fast8_t> runtimeAccessRequests_{0};
127127

128-
mutable std::atomic_bool isSynchronous_{false};
128+
std::atomic_bool isSynchronous_{false};
129129

130-
void startWorkLoop(jsi::Runtime& runtime) const;
130+
void startWorkLoop(jsi::Runtime& runtime);
131131

132132
/*
133133
* Schedules a work loop unless it has been already scheduled
134134
* This is to avoid unnecessary calls to `runtimeExecutor`.
135135
*/
136-
void scheduleWorkLoopIfNecessary() const;
136+
void scheduleWorkLoopIfNecessary();
137137

138138
void executeTask(
139139
jsi::Runtime& runtime,
140140
const std::shared_ptr<Task>& task,
141-
bool didUserCallbackTimeout) const;
141+
bool didUserCallbackTimeout);
142142

143143
/*
144144
* Returns a time point representing the current point in time. May be called
@@ -150,12 +150,12 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
150150
* Flag indicating if callback on JavaScript queue has been
151151
* scheduled.
152152
*/
153-
mutable std::atomic_bool isWorkLoopScheduled_{false};
153+
std::atomic_bool isWorkLoopScheduled_{false};
154154

155155
/*
156156
* This flag is set while performing work, to prevent re-entrancy.
157157
*/
158-
mutable std::atomic_bool isPerformingWork_{false};
158+
std::atomic_bool isPerformingWork_{false};
159159
};
160160

161161
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ RuntimeScheduler_Modern::RuntimeScheduler_Modern(
2121
std::function<RuntimeSchedulerTimePoint()> now)
2222
: runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {}
2323

24-
void RuntimeScheduler_Modern::scheduleWork(
25-
RawCallback&& callback) const noexcept {
24+
void RuntimeScheduler_Modern::scheduleWork(RawCallback&& callback) noexcept {
2625
SystraceSection s("RuntimeScheduler::scheduleWork");
2726
scheduleTask(SchedulerPriority::ImmediatePriority, std::move(callback));
2827
}
2928

3029
std::shared_ptr<Task> RuntimeScheduler_Modern::scheduleTask(
3130
SchedulerPriority priority,
32-
jsi::Function&& callback) const noexcept {
31+
jsi::Function&& callback) noexcept {
3332
SystraceSection s(
3433
"RuntimeScheduler::scheduleTask",
3534
"priority",
@@ -48,7 +47,7 @@ std::shared_ptr<Task> RuntimeScheduler_Modern::scheduleTask(
4847

4948
std::shared_ptr<Task> RuntimeScheduler_Modern::scheduleTask(
5049
SchedulerPriority priority,
51-
RawCallback&& callback) const noexcept {
50+
RawCallback&& callback) noexcept {
5251
SystraceSection s(
5352
"RuntimeScheduler::scheduleTask",
5453
"priority",
@@ -144,7 +143,7 @@ void RuntimeScheduler_Modern::callExpiredTasks(jsi::Runtime& runtime) {
144143

145144
#pragma mark - Private
146145

147-
void RuntimeScheduler_Modern::scheduleTask(std::shared_ptr<Task> task) const {
146+
void RuntimeScheduler_Modern::scheduleTask(std::shared_ptr<Task> task) {
148147
bool shouldScheduleWorkLoop = false;
149148

150149
{
@@ -167,14 +166,14 @@ void RuntimeScheduler_Modern::scheduleTask(std::shared_ptr<Task> task) const {
167166
}
168167
}
169168

170-
void RuntimeScheduler_Modern::scheduleWorkLoop() const {
169+
void RuntimeScheduler_Modern::scheduleWorkLoop() {
171170
runtimeExecutor_(
172171
[this](jsi::Runtime& runtime) { startWorkLoop(runtime, false); });
173172
}
174173

175174
void RuntimeScheduler_Modern::startWorkLoop(
176175
jsi::Runtime& runtime,
177-
bool onlyExpired) const {
176+
bool onlyExpired) {
178177
SystraceSection s("RuntimeScheduler::startWorkLoop");
179178

180179
auto previousPriority = currentPriority_;
@@ -201,7 +200,7 @@ void RuntimeScheduler_Modern::startWorkLoop(
201200

202201
std::shared_ptr<Task> RuntimeScheduler_Modern::selectTask(
203202
RuntimeSchedulerTimePoint currentTime,
204-
bool onlyExpired) const {
203+
bool onlyExpired) {
205204
// We need a unique lock here because we'll also remove executed tasks from
206205
// the top of the queue.
207206
std::unique_lock lock(schedulingMutex_);
@@ -229,7 +228,7 @@ std::shared_ptr<Task> RuntimeScheduler_Modern::selectTask(
229228
void RuntimeScheduler_Modern::executeTask(
230229
jsi::Runtime& runtime,
231230
const std::shared_ptr<Task>& task,
232-
RuntimeSchedulerTimePoint currentTime) const {
231+
RuntimeSchedulerTimePoint currentTime) {
233232
auto didUserCallbackTimeout = task->expirationTime <= currentTime;
234233

235234
SystraceSection s(

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
4343
* To be removed when we finish testing this implementation.
4444
* All callers should use scheduleTask with the right priority afte that.
4545
*/
46-
void scheduleWork(RawCallback&& callback) const noexcept override;
46+
void scheduleWork(RawCallback&& callback) noexcept override;
4747

4848
/*
4949
* Grants access to the runtime synchronously on the caller's thread.
@@ -60,15 +60,15 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
6060
*/
6161
std::shared_ptr<Task> scheduleTask(
6262
SchedulerPriority priority,
63-
jsi::Function&& callback) const noexcept override;
63+
jsi::Function&& callback) noexcept override;
6464

6565
/*
6666
* Adds a custom callback to the priority queue with the given priority.
6767
* Triggers workloop if needed.
6868
*/
6969
std::shared_ptr<Task> scheduleTask(
7070
SchedulerPriority priority,
71-
RawCallback&& callback) const noexcept override;
71+
RawCallback&& callback) noexcept override;
7272

7373
/*
7474
* Cancelled task will never be executed.
@@ -122,34 +122,34 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
122122
void callExpiredTasks(jsi::Runtime& runtime) override;
123123

124124
private:
125-
mutable std::atomic<uint_fast8_t> syncTaskRequests_{0};
125+
std::atomic<uint_fast8_t> syncTaskRequests_{0};
126126

127-
mutable std::priority_queue<
127+
std::priority_queue<
128128
std::shared_ptr<Task>,
129129
std::vector<std::shared_ptr<Task>>,
130130
TaskPriorityComparer>
131131
taskQueue_;
132132

133-
mutable std::shared_ptr<Task> currentTask_;
133+
std::shared_ptr<Task> currentTask_;
134134

135135
/**
136136
* This protects the access to `taskQueue_` and `isWorkLoopScheduled_`.
137137
*/
138138
mutable std::shared_mutex schedulingMutex_;
139139

140140
const RuntimeExecutor runtimeExecutor_;
141-
mutable SchedulerPriority currentPriority_{SchedulerPriority::NormalPriority};
141+
SchedulerPriority currentPriority_{SchedulerPriority::NormalPriority};
142142

143-
mutable std::atomic_bool isSynchronous_{false};
143+
std::atomic_bool isSynchronous_{false};
144144

145-
void scheduleWorkLoop() const;
146-
void startWorkLoop(jsi::Runtime& runtime, bool onlyExpired) const;
145+
void scheduleWorkLoop();
146+
void startWorkLoop(jsi::Runtime& runtime, bool onlyExpired);
147147

148148
std::shared_ptr<Task> selectTask(
149149
RuntimeSchedulerTimePoint currentTime,
150-
bool onlyExpired) const;
150+
bool onlyExpired);
151151

152-
void scheduleTask(std::shared_ptr<Task> task) const;
152+
void scheduleTask(std::shared_ptr<Task> task);
153153

154154
/**
155155
* Follows all the steps necessary to execute the given task (in the future,
@@ -158,7 +158,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
158158
void executeTask(
159159
jsi::Runtime& runtime,
160160
const std::shared_ptr<Task>& task,
161-
RuntimeSchedulerTimePoint currentTime) const;
161+
RuntimeSchedulerTimePoint currentTime);
162162

163163
/*
164164
* Returns a time point representing the current point in time. May be called
@@ -170,7 +170,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
170170
* Flag indicating if callback on JavaScript queue has been
171171
* scheduled.
172172
*/
173-
mutable bool isWorkLoopScheduled_{false};
173+
bool isWorkLoopScheduled_{false};
174174
};
175175

176176
} // namespace facebook::react

0 commit comments

Comments
 (0)