[Jobs]: Expose CancelAndDetach()

This is useful for wasm instead of keeping around a list of handles.

Change-Id: I4ef970ba191a66303c577bbe8e6ab1327aad2e24
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2451209
Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70353}
This commit is contained in:
Etienne Pierre-doray 2020-10-05 16:27:31 -04:00 committed by Commit Bot
parent ac7af6bb7c
commit 4f1bf7d10b
6 changed files with 25 additions and 0 deletions

View File

@ -216,6 +216,14 @@ class JobHandle {
*/
virtual void Cancel() = 0;
/*
* Forces all existing workers to yield ASAP but doesnt wait for them.
* Warning, this is dangerous if the Job's callback is bound to or has access
* to state which may be deleted after this call.
* TODO(etiennep): Cleanup once implemented by all embedders.
*/
virtual void CancelAndDetach() { Cancel(); }
/**
* Returns true if there's no work pending and no worker running.
*/

View File

@ -64,6 +64,8 @@ class DefaultJobImpl {
Join();
}
void CancelAndDetach() { can_run_.store(false, std::memory_order_relaxed); }
bool IsCompleted() const { return !IsRunning(); }
bool IsRunning() const {
uint8_t active_threads = active_threads_.load(std::memory_order_relaxed);
@ -173,6 +175,7 @@ class DefaultJobImpl<Thread>::JobHandle final : public cppgc::JobHandle {
}
void Join() override { job_->Join(); }
void Cancel() override { job_->Cancel(); }
void CancelAndDetach() override { job_->CancelAndDetach(); }
bool IsCompleted() override { return job_->IsCompleted(); }
bool IsRunning() override { return job_->IsRunning(); }

View File

@ -122,6 +122,11 @@ void DefaultJobState::CancelAndWait() {
}
}
void DefaultJobState::CancelAndDetach() {
base::MutexGuard guard(&mutex_);
is_canceled_.store(true, std::memory_order_relaxed);
}
bool DefaultJobState::IsCompleted() {
base::MutexGuard guard(&mutex_);
return job_task_->GetMaxConcurrency(active_workers_) == 0 &&
@ -220,6 +225,11 @@ void DefaultJobHandle::Cancel() {
state_ = nullptr;
}
void DefaultJobHandle::CancelAndDetach() {
state_->CancelAndDetach();
state_ = nullptr;
}
bool DefaultJobHandle::IsCompleted() { return state_->IsCompleted(); }
} // namespace platform

View File

@ -54,6 +54,7 @@ class V8_PLATFORM_EXPORT DefaultJobState
void Join();
void CancelAndWait();
void CancelAndDetach();
bool IsCompleted();
// Must be called before running |job_task_| for the first time. If it returns
@ -109,6 +110,7 @@ class V8_PLATFORM_EXPORT DefaultJobHandle : public JobHandle {
void Join() override;
void Cancel() override;
void CancelAndDetach() override;
bool IsCompleted() override;
bool IsRunning() override { return state_ != nullptr; }

View File

@ -111,6 +111,7 @@ class MockPlatform final : public TestPlatform {
}
void Join() override { orig_handle_->Join(); }
void Cancel() override { orig_handle_->Cancel(); }
void CancelAndDetach() override { orig_handle_->CancelAndDetach(); }
bool IsCompleted() override { return orig_handle_->IsCompleted(); }
bool IsRunning() override { return orig_handle_->IsRunning(); }

View File

@ -114,6 +114,7 @@ class MockPlatform final : public TestPlatform {
}
void Join() override { orig_handle_->Join(); }
void Cancel() override { orig_handle_->Cancel(); }
void CancelAndDetach() override { orig_handle_->CancelAndDetach(); }
bool IsRunning() override { return orig_handle_->IsRunning(); }
bool IsCompleted() override { return orig_handle_->IsCompleted(); }