Skip to content

Commit

Permalink
Implement the new task scheduler and remove the old one
Browse files Browse the repository at this point in the history
  • Loading branch information
TrentHouliston committed Aug 6, 2024
1 parent 3d15e17 commit a5d12d3
Show file tree
Hide file tree
Showing 10 changed files with 705 additions and 657 deletions.
13 changes: 6 additions & 7 deletions src/PowerPlant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ void PowerPlant::log(const LogLevel& level, std::stringstream& message) {
log(level, message.str());
}

void PowerPlant::add_idle_task(const NUClear::id_t& id,
const util::ThreadPoolDescriptor& pool_descriptor,
std::function<void()>&& task) {
scheduler.add_idle_task(id, pool_descriptor, std::move(task));
void PowerPlant::add_idle_task(const util::ThreadPoolDescriptor& pool_descriptor,
const std::shared_ptr<threading::Reaction>& task) {
scheduler.add_idle_task(pool_descriptor, task);
}

void PowerPlant::remove_idle_task(const NUClear::id_t& id, const util::ThreadPoolDescriptor& pool_descriptor) {
scheduler.remove_idle_task(id, pool_descriptor);
void PowerPlant::remove_idle_task(const util::ThreadPoolDescriptor& pool_descriptor, const NUClear::id_t& id) {
scheduler.remove_idle_task(pool_descriptor, id);
}

void PowerPlant::shutdown() {
Expand All @@ -128,7 +127,7 @@ void PowerPlant::shutdown() {
emit(std::make_unique<dsl::word::Shutdown>());

// Shutdown the scheduler
scheduler.shutdown();
scheduler.stop();
}

bool PowerPlant::running() const {
Expand Down
20 changes: 9 additions & 11 deletions src/PowerPlant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "LogLevel.hpp"
#include "id.hpp"
#include "threading/ReactionTask.hpp"
#include "threading/TaskScheduler.hpp"
#include "threading/scheduler/Scheduler.hpp"
#include "util/FunctionFusion.hpp"
#include "util/demangle.hpp"

Expand Down Expand Up @@ -158,27 +158,25 @@ class PowerPlant {
* Adds an idle task to the task scheduler.
*
* This function adds an idle task to the task scheduler, which will be executed when the thread pool associated
* with the given `pool_id` has no other tasks to execute. The `task` parameter is a callable object that
* represents the idle task to be executed.
* with the given `pool_id` has no other tasks to execute. The `task` parameter is a Reaction from which a task will
* be submitted when the pool is idle.
*
* @param id The ID of the task.
* @param pool_descriptor The descriptor for the thread pool to test for idle
* @param task The idle task to be executed.
* @param reaction The reaction to be executed when idle
*/
void add_idle_task(const NUClear::id_t& id,
const util::ThreadPoolDescriptor& pool_descriptor,
std::function<void()>&& task);
void add_idle_task(const util::ThreadPoolDescriptor& pool_descriptor,
const std::shared_ptr<threading::Reaction>& reaction);

/**
* Removes an idle task from the task scheduler.
*
* This function removes an idle task from the task scheduler. The `id` and `pool_id` parameters are used to
* identify the idle task to be removed.
*
* @param id The ID of the task.
* @param pool_descriptor The descriptor for the thread pool to test for idle
* @param id The reaction id of the task to be removed
*/
void remove_idle_task(const NUClear::id_t& id, const util::ThreadPoolDescriptor& pool_descriptor);
void remove_idle_task(const util::ThreadPoolDescriptor& pool_descriptor, const NUClear::id_t& id);

/**
* Submits a new task to the ThreadPool to be queued and then executed.
Expand Down Expand Up @@ -320,7 +318,7 @@ class PowerPlant {
/// A list of tasks that must be run when the powerplant starts up
std::vector<std::function<void()>> tasks;
/// Our TaskScheduler that handles distributing task to the pool threads
threading::TaskScheduler scheduler;
threading::scheduler::Scheduler scheduler;
/// Our vector of Reactors, will get destructed when this vector is
std::vector<std::unique_ptr<NUClear::Reactor>> reactors;
/// True if the powerplant is running
Expand Down
6 changes: 2 additions & 4 deletions src/dsl/word/Idle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ namespace dsl {

// Our unbinder to remove this reaction
reaction->unbinders.push_back([pool_descriptor](const threading::Reaction& r) { //
r.reactor.powerplant.remove_idle_task(r.id, pool_descriptor);
r.reactor.powerplant.remove_idle_task(pool_descriptor, r.id);
});

reaction->reactor.powerplant.add_idle_task(reaction->id, pool_descriptor, [reaction] {
reaction->reactor.powerplant.submit(reaction->get_task());
});
reaction->reactor.powerplant.add_idle_task(pool_descriptor, reaction);
}

/**
Expand Down
Loading

0 comments on commit a5d12d3

Please sign in to comment.