Commit 4f1bf7d1 authored by Etienne Pierre-doray's avatar Etienne Pierre-doray Committed by Commit Bot

[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/+/2451209Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70353}
parent ac7af6bb
...@@ -216,6 +216,14 @@ class JobHandle { ...@@ -216,6 +216,14 @@ class JobHandle {
*/ */
virtual void Cancel() = 0; virtual void Cancel() = 0;
/*
* Forces all existing workers to yield ASAP but doesn’t 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. * Returns true if there's no work pending and no worker running.
*/ */
......
...@@ -64,6 +64,8 @@ class DefaultJobImpl { ...@@ -64,6 +64,8 @@ class DefaultJobImpl {
Join(); Join();
} }
void CancelAndDetach() { can_run_.store(false, std::memory_order_relaxed); }
bool IsCompleted() const { return !IsRunning(); } bool IsCompleted() const { return !IsRunning(); }
bool IsRunning() const { bool IsRunning() const {
uint8_t active_threads = active_threads_.load(std::memory_order_relaxed); uint8_t active_threads = active_threads_.load(std::memory_order_relaxed);
...@@ -173,6 +175,7 @@ class DefaultJobImpl<Thread>::JobHandle final : public cppgc::JobHandle { ...@@ -173,6 +175,7 @@ class DefaultJobImpl<Thread>::JobHandle final : public cppgc::JobHandle {
} }
void Join() override { job_->Join(); } void Join() override { job_->Join(); }
void Cancel() override { job_->Cancel(); } void Cancel() override { job_->Cancel(); }
void CancelAndDetach() override { job_->CancelAndDetach(); }
bool IsCompleted() override { return job_->IsCompleted(); } bool IsCompleted() override { return job_->IsCompleted(); }
bool IsRunning() override { return job_->IsRunning(); } bool IsRunning() override { return job_->IsRunning(); }
......
...@@ -122,6 +122,11 @@ void DefaultJobState::CancelAndWait() { ...@@ -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() { bool DefaultJobState::IsCompleted() {
base::MutexGuard guard(&mutex_); base::MutexGuard guard(&mutex_);
return job_task_->GetMaxConcurrency(active_workers_) == 0 && return job_task_->GetMaxConcurrency(active_workers_) == 0 &&
...@@ -220,6 +225,11 @@ void DefaultJobHandle::Cancel() { ...@@ -220,6 +225,11 @@ void DefaultJobHandle::Cancel() {
state_ = nullptr; state_ = nullptr;
} }
void DefaultJobHandle::CancelAndDetach() {
state_->CancelAndDetach();
state_ = nullptr;
}
bool DefaultJobHandle::IsCompleted() { return state_->IsCompleted(); } bool DefaultJobHandle::IsCompleted() { return state_->IsCompleted(); }
} // namespace platform } // namespace platform
......
...@@ -54,6 +54,7 @@ class V8_PLATFORM_EXPORT DefaultJobState ...@@ -54,6 +54,7 @@ class V8_PLATFORM_EXPORT DefaultJobState
void Join(); void Join();
void CancelAndWait(); void CancelAndWait();
void CancelAndDetach();
bool IsCompleted(); bool IsCompleted();
// Must be called before running |job_task_| for the first time. If it returns // 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 { ...@@ -109,6 +110,7 @@ class V8_PLATFORM_EXPORT DefaultJobHandle : public JobHandle {
void Join() override; void Join() override;
void Cancel() override; void Cancel() override;
void CancelAndDetach() override;
bool IsCompleted() override; bool IsCompleted() override;
bool IsRunning() override { return state_ != nullptr; } bool IsRunning() override { return state_ != nullptr; }
......
...@@ -111,6 +111,7 @@ class MockPlatform final : public TestPlatform { ...@@ -111,6 +111,7 @@ class MockPlatform final : public TestPlatform {
} }
void Join() override { orig_handle_->Join(); } void Join() override { orig_handle_->Join(); }
void Cancel() override { orig_handle_->Cancel(); } void Cancel() override { orig_handle_->Cancel(); }
void CancelAndDetach() override { orig_handle_->CancelAndDetach(); }
bool IsCompleted() override { return orig_handle_->IsCompleted(); } bool IsCompleted() override { return orig_handle_->IsCompleted(); }
bool IsRunning() override { return orig_handle_->IsRunning(); } bool IsRunning() override { return orig_handle_->IsRunning(); }
......
...@@ -114,6 +114,7 @@ class MockPlatform final : public TestPlatform { ...@@ -114,6 +114,7 @@ class MockPlatform final : public TestPlatform {
} }
void Join() override { orig_handle_->Join(); } void Join() override { orig_handle_->Join(); }
void Cancel() override { orig_handle_->Cancel(); } void Cancel() override { orig_handle_->Cancel(); }
void CancelAndDetach() override { orig_handle_->CancelAndDetach(); }
bool IsRunning() override { return orig_handle_->IsRunning(); } bool IsRunning() override { return orig_handle_->IsRunning(); }
bool IsCompleted() override { return orig_handle_->IsCompleted(); } bool IsCompleted() override { return orig_handle_->IsCompleted(); }
......
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