Commit eedc7dbf authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Simplify compilation state callback mechanism.

R=titzer@chromium.org

Change-Id: If459225345f8a94eb566334e15331f7741c952d4
Reviewed-on: https://chromium-review.googlesource.com/1183103
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55271}
parent 20122d46
...@@ -67,10 +67,14 @@ class CompilationState { ...@@ -67,10 +67,14 @@ class CompilationState {
CompilationState(internal::Isolate*, const ModuleEnv&); CompilationState(internal::Isolate*, const ModuleEnv&);
~CompilationState(); ~CompilationState();
// Needs to be set before {AddCompilationUnits} is run, which triggers // Set the number of compilations unit expected to be executed. Needs to be
// background compilation. // set before {AddCompilationUnits} is run, which triggers background
// compilation.
void SetNumberOfFunctionsToCompile(size_t num_functions); void SetNumberOfFunctionsToCompile(size_t num_functions);
void AddCallback(
// Set the callback function to be called on compilation events. Needs to be
// set before {AddCompilationUnits} is run.
void SetCallback(
std::function<void(CompilationEvent, ErrorThrower*)> callback); std::function<void(CompilationEvent, ErrorThrower*)> callback);
// Inserts new functions to compile and kicks off compilation. // Inserts new functions to compile and kicks off compilation.
...@@ -148,10 +152,8 @@ class CompilationState { ...@@ -148,10 +152,8 @@ class CompilationState {
// End of fields protected by {mutex_}. // End of fields protected by {mutex_}.
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// TODO(mstarzinger): We should make sure this allows at most one callback // Callback function to be called on compilation events.
// to exist for each {CompilationState} because reifying the error object on std::function<void(CompilationEvent, ErrorThrower*)> callback_;
// the given {ErrorThrower} can be done at most once.
std::vector<std::function<void(CompilationEvent, ErrorThrower*)>> callbacks_;
CancelableTaskManager background_task_manager_; CancelableTaskManager background_task_manager_;
CancelableTaskManager foreground_task_manager_; CancelableTaskManager foreground_task_manager_;
...@@ -2426,7 +2428,7 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep { ...@@ -2426,7 +2428,7 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
// capture the {job} pointer by copy, as it otherwise is dependent // capture the {job} pointer by copy, as it otherwise is dependent
// on the current step we are in. // on the current step we are in.
AsyncCompileJob* job = job_; AsyncCompileJob* job = job_;
compilation_state->AddCallback( compilation_state->SetCallback(
[job](CompilationEvent event, ErrorThrower* thrower) { [job](CompilationEvent event, ErrorThrower* thrower) {
// Callback is called from a foreground thread. // Callback is called from a foreground thread.
switch (event) { switch (event) {
...@@ -2766,9 +2768,10 @@ void CompilationState::SetNumberOfFunctionsToCompile(size_t num_functions) { ...@@ -2766,9 +2768,10 @@ void CompilationState::SetNumberOfFunctionsToCompile(size_t num_functions) {
} }
} }
void CompilationState::AddCallback( void CompilationState::SetCallback(
std::function<void(CompilationEvent, ErrorThrower*)> callback) { std::function<void(CompilationEvent, ErrorThrower*)> callback) {
callbacks_.push_back(callback); DCHECK_NULL(callback_);
callback_ = callback;
} }
void CompilationState::AddCompilationUnits( void CompilationState::AddCompilationUnits(
...@@ -2942,9 +2945,7 @@ void CompilationState::Abort() { ...@@ -2942,9 +2945,7 @@ void CompilationState::Abort() {
void CompilationState::NotifyOnEvent(CompilationEvent event, void CompilationState::NotifyOnEvent(CompilationEvent event,
ErrorThrower* thrower) { ErrorThrower* thrower) {
for (auto& callback_function : callbacks_) { if (callback_) callback_(event, thrower);
callback_function(event, thrower);
}
} }
void CompileJsToWasmWrappers(Isolate* isolate, void CompileJsToWasmWrappers(Isolate* isolate,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment