forked from sqreen/PyMiniRacer
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
490 additions
and
462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,52 @@ | ||
#include "cancelable_task_runner.h" | ||
|
||
#include <cstdint> | ||
#include <future> | ||
#include <memory> | ||
#include <mutex> | ||
#include <utility> | ||
#include <vector> | ||
#include "id_maker.h" | ||
#include "isolate_manager.h" | ||
|
||
namespace MiniRacer { | ||
|
||
CancelableTaskState::CancelableTaskState() : state_(State::kNotStarted) {} | ||
|
||
void CancelableTaskState::Cancel(IsolateManager* isolate_manager) { | ||
const std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
if (state_ == State::kCanceled || state_ == State::kCompleted) { | ||
return; | ||
} | ||
|
||
if (state_ == State::kRunning) { | ||
isolate_manager->TerminateOngoingTask(); | ||
} | ||
void CancelableTaskBase::SetFuture(std::future<void> fut) { | ||
future_promise_.set_value(std::move(fut)); | ||
} | ||
|
||
state_ = State::kCanceled; | ||
void CancelableTaskBase::Await() { | ||
future_promise_.get_future().get(); | ||
} | ||
|
||
auto CancelableTaskState::SetRunningIfNotCanceled() -> bool { | ||
const std::lock_guard<std::mutex> lock(mutex_); | ||
if (state_ == State::kCanceled) { | ||
return false; | ||
} | ||
CancelableTaskManager::CancelableTaskManager(IsolateManager* isolate_manager) | ||
: isolate_manager_(isolate_manager), | ||
task_id_maker_(std::make_shared<IdMaker<CancelableTaskBase>>()) {} | ||
|
||
state_ = State::kRunning; | ||
return true; | ||
} | ||
CancelableTaskManager::~CancelableTaskManager() { | ||
// Normally, completed or canceled tasks will clean themselves out of the | ||
// IdMaker. However, some tasks may still be pending upon teardown. Let's | ||
// explicitly cancel and await any stragglers: | ||
const std::vector<std::shared_ptr<CancelableTaskBase>> pending_tasks = | ||
task_id_maker_->GetObjects(); | ||
|
||
auto CancelableTaskState::SetCompleteIfNotCanceled() -> bool { | ||
const std::lock_guard<std::mutex> lock(mutex_); | ||
if (state_ == State::kCanceled) { | ||
return false; | ||
for (const auto& task : pending_tasks) { | ||
task->Cancel(isolate_manager_); | ||
} | ||
|
||
state_ = State::kCompleted; | ||
return true; | ||
for (const auto& task : pending_tasks) { | ||
task->Await(); | ||
} | ||
} | ||
|
||
CancelableTaskRunner::CancelableTaskRunner( | ||
std::shared_ptr<IsolateManager> isolate_manager) | ||
: isolate_manager_(std::move(isolate_manager)), | ||
task_id_maker_(std::make_shared<IdMaker<CancelableTaskState>>()) {} | ||
|
||
void CancelableTaskRunner::Cancel(uint64_t task_id) { | ||
const std::shared_ptr<CancelableTaskState> task_state = | ||
void CancelableTaskManager::Cancel(uint64_t task_id) { | ||
const std::shared_ptr<CancelableTaskBase> task = | ||
task_id_maker_->GetObject(task_id); | ||
if (!task_state) { | ||
if (!task) { | ||
// No such task found. This will commonly happen if a task is canceled | ||
// after it has already completed. | ||
return; | ||
} | ||
task_state->Cancel(isolate_manager_.get()); | ||
task->Cancel(isolate_manager_); | ||
} | ||
|
||
} // end namespace MiniRacer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.