|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the Apache 2.0 License. |
| 3 | + |
| 4 | +#include "tasks/fan_in_tasks.h" |
| 5 | + |
| 6 | +#include <map> |
| 7 | +#include <mutex> |
| 8 | +#include <stdexcept> |
| 9 | + |
| 10 | +#define FMT_HEADER_ONLY |
| 11 | +#include <fmt/format.h> |
| 12 | + |
| 13 | +namespace ccf::tasks |
| 14 | +{ |
| 15 | + struct FanInTasks::PImpl |
| 16 | + { |
| 17 | + std::string name; |
| 18 | + IJobBoard& job_board; |
| 19 | + |
| 20 | + // Synchronise access to pending_tasks and next_expected_task_index |
| 21 | + std::mutex pending_tasks_mutex; |
| 22 | + std::map<size_t, Task> pending_tasks; |
| 23 | + size_t next_expected_task_index = 0; |
| 24 | + |
| 25 | + std::atomic<bool> active = false; |
| 26 | + }; |
| 27 | + |
| 28 | + void FanInTasks::enqueue_on_board() |
| 29 | + { |
| 30 | + pimpl->job_board.add_task(shared_from_this()); |
| 31 | + } |
| 32 | + |
| 33 | + void FanInTasks::do_task_implementation() |
| 34 | + { |
| 35 | + std::vector<Task> current_batch; |
| 36 | + |
| 37 | + { |
| 38 | + std::lock_guard<std::mutex> lock(pimpl->pending_tasks_mutex); |
| 39 | + pimpl->active.store(true); |
| 40 | + |
| 41 | + auto it = pimpl->pending_tasks.find(pimpl->next_expected_task_index); |
| 42 | + while (it != pimpl->pending_tasks.end()) |
| 43 | + { |
| 44 | + current_batch.push_back(it->second); |
| 45 | + pimpl->pending_tasks.erase(it); |
| 46 | + |
| 47 | + ++pimpl->next_expected_task_index; |
| 48 | + it = pimpl->pending_tasks.find(pimpl->next_expected_task_index); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + for (auto& task : current_batch) |
| 53 | + { |
| 54 | + task->do_task(); |
| 55 | + } |
| 56 | + |
| 57 | + { |
| 58 | + std::lock_guard<std::mutex> lock(pimpl->pending_tasks_mutex); |
| 59 | + pimpl->active.store(false); |
| 60 | + |
| 61 | + auto it = pimpl->pending_tasks.find(pimpl->next_expected_task_index); |
| 62 | + if (it != pimpl->pending_tasks.end()) |
| 63 | + { |
| 64 | + // While we were executing the previous batch, a call to fan_in_task |
| 65 | + // provided the _next_ contiguous task. We're now responsible for |
| 66 | + // re-enqueuing this task |
| 67 | + enqueue_on_board(); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + FanInTasks::FanInTasks( |
| 73 | + [[maybe_unused]] FanInTasks::Private force_private_constructor, |
| 74 | + IJobBoard& job_board_, |
| 75 | + const std::string& name_) : |
| 76 | + pimpl(std::make_unique<FanInTasks::PImpl>(name_, job_board_)) |
| 77 | + {} |
| 78 | + |
| 79 | + FanInTasks::~FanInTasks() = default; |
| 80 | + |
| 81 | + std::string_view FanInTasks::get_name() const |
| 82 | + { |
| 83 | + return pimpl->name; |
| 84 | + } |
| 85 | + |
| 86 | + void FanInTasks::add_task(size_t task_index, Task task) |
| 87 | + { |
| 88 | + { |
| 89 | + std::lock_guard<std::mutex> lock(pimpl->pending_tasks_mutex); |
| 90 | + |
| 91 | + if (task_index < pimpl->next_expected_task_index) |
| 92 | + { |
| 93 | + throw std::runtime_error(fmt::format( |
| 94 | + "[{}] Received task {} ({}) out-of-order - already advanced next " |
| 95 | + "expected " |
| 96 | + "to {}", |
| 97 | + get_name(), |
| 98 | + task_index, |
| 99 | + task->get_name(), |
| 100 | + pimpl->next_expected_task_index)); |
| 101 | + } |
| 102 | + |
| 103 | + auto it = pimpl->pending_tasks.find(task_index); |
| 104 | + if (it != pimpl->pending_tasks.end()) |
| 105 | + { |
| 106 | + throw std::runtime_error(fmt::format( |
| 107 | + "[{}] Received duplicate task {} ({}) - already have pending task {}", |
| 108 | + get_name(), |
| 109 | + task_index, |
| 110 | + task->get_name(), |
| 111 | + it->second == nullptr ? std::string("nullptr") : |
| 112 | + it->second->get_name())); |
| 113 | + } |
| 114 | + |
| 115 | + pimpl->pending_tasks.emplace(task_index, task); |
| 116 | + |
| 117 | + if (!pimpl->active.load()) |
| 118 | + { |
| 119 | + if (task_index == pimpl->next_expected_task_index) |
| 120 | + { |
| 121 | + enqueue_on_board(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + std::shared_ptr<FanInTasks> FanInTasks::create( |
| 128 | + IJobBoard& job_board_, const std::string& name_) |
| 129 | + { |
| 130 | + return std::make_shared<FanInTasks>(Private{}, job_board_, name_); |
| 131 | + } |
| 132 | +} |
0 commit comments