forked from sqreen/PyMiniRacer
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcancelable_task_runner.cc
More file actions
52 lines (42 loc) · 1.42 KB
/
cancelable_task_runner.cc
File metadata and controls
52 lines (42 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "cancelable_task_runner.h"
#include <cstdint>
#include <future>
#include <memory>
#include <utility>
#include <vector>
#include "id_maker.h"
#include "isolate_manager.h"
namespace MiniRacer {
void CancelableTaskBase::SetFuture(std::future<void> fut) {
future_promise_.set_value(std::move(fut));
}
void CancelableTaskBase::Await() {
future_promise_.get_future().get();
}
CancelableTaskManager::CancelableTaskManager(IsolateManager* isolate_manager)
: isolate_manager_(isolate_manager),
task_id_maker_(std::make_shared<IdMaker<CancelableTaskBase>>()) {}
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();
for (const auto& task : pending_tasks) {
task->Cancel(isolate_manager_);
}
for (const auto& task : pending_tasks) {
task->Await();
}
}
void CancelableTaskManager::Cancel(uint64_t task_id) {
const std::shared_ptr<CancelableTaskBase> task =
task_id_maker_->GetObject(task_id);
if (!task) {
// No such task found. This will commonly happen if a task is canceled
// after it has already completed.
return;
}
task->Cancel(isolate_manager_);
}
} // end namespace MiniRacer