diff --git a/src/PowerPlant.cpp b/src/PowerPlant.cpp index 13a66a2b6..61eb180b6 100644 --- a/src/PowerPlant.cpp +++ b/src/PowerPlant.cpp @@ -49,7 +49,7 @@ void PowerPlant::start() { scheduler.start(); } -void PowerPlant::submit(const uint64_t& id, +void PowerPlant::submit(const NUClear::id_t& id, const int& priority, const util::GroupDescriptor& group, const util::ThreadPoolDescriptor& pool, diff --git a/src/PowerPlant.hpp b/src/PowerPlant.hpp index 099f8091e..550d0869e 100644 --- a/src/PowerPlant.hpp +++ b/src/PowerPlant.hpp @@ -39,6 +39,7 @@ // Utilities #include "Configuration.hpp" #include "LogLevel.hpp" +#include "id.hpp" #include "message/LogMessage.hpp" #include "threading/ReactionTask.hpp" #include "threading/TaskScheduler.hpp" @@ -135,7 +136,7 @@ class PowerPlant { * @param immediate if this task should run immediately in the current thread * @param task the wrapped function to be executed */ - void submit(const uint64_t& id, + void submit(const NUClear::id_t& id, const int& priority, const util::GroupDescriptor& group, const util::ThreadPoolDescriptor& pool, diff --git a/src/dsl/operation/ChronoTask.hpp b/src/dsl/operation/ChronoTask.hpp index c88ce1c0e..f270fd262 100644 --- a/src/dsl/operation/ChronoTask.hpp +++ b/src/dsl/operation/ChronoTask.hpp @@ -24,6 +24,7 @@ #define NUCLEAR_DSL_OPERATION_CHRONOTASK_HPP #include "../../clock.hpp" +#include "../../id.hpp" namespace NUClear { namespace dsl { @@ -46,11 +47,11 @@ namespace dsl { * @param task the task to run, takes the time to execute as a reference so it can be updated for * future runs * @param time the time to execute this task - * @param id the unique identifer for this task + * @param id the unique identifier for this task */ ChronoTask(std::function&& task, - NUClear::clock::time_point time, - uint64_t id) + const NUClear::clock::time_point& time, + const NUClear::id_t& id) : task(std::move(task)), time(time), id(id) {} /** @@ -100,7 +101,7 @@ namespace dsl { /// The time this task should be executed NUClear::clock::time_point time; /// The unique identifier for this task so it can be unbound - uint64_t id{0}; + NUClear::id_t id{0}; }; } // namespace operation diff --git a/src/dsl/operation/Unbind.hpp b/src/dsl/operation/Unbind.hpp index 956e65686..23b60d5e3 100644 --- a/src/dsl/operation/Unbind.hpp +++ b/src/dsl/operation/Unbind.hpp @@ -23,6 +23,8 @@ #ifndef NUCLEAR_DSL_OPERATION_UNBIND_HPP #define NUCLEAR_DSL_OPERATION_UNBIND_HPP +#include "../../id.hpp" + namespace NUClear { namespace dsl { namespace operation { @@ -38,10 +40,10 @@ namespace dsl { */ template struct Unbind { - explicit Unbind(const uint64_t& id) : id(id){}; + explicit Unbind(const NUClear::id_t& id) : id(id){}; /// The id of the task to unbind - const uint64_t id{0}; + const NUClear::id_t id{0}; }; } // namespace operation diff --git a/src/dsl/word/Always.hpp b/src/dsl/word/Always.hpp index 30d3a4e3c..d23a06d47 100644 --- a/src/dsl/word/Always.hpp +++ b/src/dsl/word/Always.hpp @@ -27,6 +27,7 @@ #include #include +#include "../../id.hpp" #include "../../threading/ReactionTask.hpp" #include "../../util/ThreadPoolDescriptor.hpp" @@ -73,7 +74,7 @@ namespace dsl { template static inline util::ThreadPoolDescriptor pool(const threading::Reaction& reaction) { - static std::map pool_id; + static std::map pool_id; static std::mutex mutex; const std::lock_guard lock(mutex); @@ -90,7 +91,7 @@ namespace dsl { * the always reaction and one for the idle reaction that we generate in this function * The main purpose of this map is to ensure that the always reaction pointer doesn't get destroyed */ - static std::map, std::shared_ptr>> reaction_store = {}; diff --git a/src/dsl/word/IO.hpp b/src/dsl/word/IO.hpp index e5d45a79f..14a890cc2 100644 --- a/src/dsl/word/IO.hpp +++ b/src/dsl/word/IO.hpp @@ -23,6 +23,7 @@ #ifndef NUCLEAR_DSL_WORD_IO_HPP #define NUCLEAR_DSL_WORD_IO_HPP +#include "../../id.hpp" #include "../../threading/Reaction.hpp" #include "../../util/platform.hpp" #include "../operation/Unbind.hpp" @@ -59,9 +60,9 @@ namespace dsl { * @brief This is emitted when an IO operation has finished. */ struct IOFinished { - explicit IOFinished(const uint64_t& id) : id(id) {} + explicit IOFinished(const NUClear::id_t& id) : id(id) {} /// @brief The id of the reaction that has finished - uint64_t id; + NUClear::id_t id; }; /** diff --git a/src/id.hpp b/src/id.hpp new file mode 100644 index 000000000..64a33759e --- /dev/null +++ b/src/id.hpp @@ -0,0 +1,37 @@ +/* + * MIT License + * + * Copyright (c) 2023 NUClear Contributors + * + * This file is part of the NUClear codebase. + * See https://github.com/Fastcode/NUClear for further info. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef NUCLEAR_UTIL_ID_HPP +#define NUCLEAR_UTIL_ID_HPP + +#include + +namespace NUClear { + +/** + * @brief A unique identifier for a thread pool + */ +using id_t = std::size_t; + +} // namespace NUClear + +#endif // NUCLEAR_UTIL_ID_HPP diff --git a/src/message/ReactionStatistics.hpp b/src/message/ReactionStatistics.hpp index dda4c78d1..b3e953cc4 100644 --- a/src/message/ReactionStatistics.hpp +++ b/src/message/ReactionStatistics.hpp @@ -26,6 +26,7 @@ #include #include #include +#include "../id.hpp" #include "../clock.hpp" #include "../threading/ReactionIdentifiers.hpp" @@ -39,10 +40,10 @@ namespace message { struct ReactionStatistics { ReactionStatistics(threading::ReactionIdentifiers identifiers, - uint64_t reaction_id, - uint64_t task_id, - uint64_t cause_reaction_id, - uint64_t cause_task_id, + const NUClear::id_t& reaction_id, + const NUClear::id_t& task_id, + const NUClear::id_t& cause_reaction_id, + const NUClear::id_t& cause_task_id, const clock::time_point& emitted, const clock::time_point& start, const clock::time_point& finish, @@ -60,13 +61,13 @@ namespace message { /// @brief A string containing the username/on arguments/and callback name of the reaction. threading::ReactionIdentifiers identifiers; /// @brief The id of this reaction. - uint64_t reaction_id{0}; + NUClear::id_t reaction_id{0}; /// @brief The task id of this reaction. - uint64_t task_id{0}; + NUClear::id_t task_id{0}; /// @brief The reaction id of the reaction that caused this one or 0 if there was not one - uint64_t cause_reaction_id{0}; + NUClear::id_t cause_reaction_id{0}; /// @brief The reaction id of the task that caused this task or 0 if there was not one - uint64_t cause_task_id{0}; + NUClear::id_t cause_task_id{0}; /// @brief The time that this reaction was emitted to the thread pool clock::time_point emitted{}; /// @brief The time that execution started on this reaction diff --git a/src/threading/Reaction.cpp b/src/threading/Reaction.cpp index b165f40a4..8b4a255cf 100644 --- a/src/threading/Reaction.cpp +++ b/src/threading/Reaction.cpp @@ -26,7 +26,7 @@ namespace NUClear { namespace threading { // Initialize our reaction source - std::atomic Reaction::reaction_id_source(0); // NOLINT + std::atomic Reaction::reaction_id_source(0); // NOLINT Reaction::Reaction(Reactor& reactor, ReactionIdentifiers&& identifiers, TaskGenerator&& generator) : reactor(reactor), identifiers(std::move(identifiers)), generator(std::move(generator)) {} diff --git a/src/threading/Reaction.hpp b/src/threading/Reaction.hpp index f5f651c0b..2d48b6c4d 100644 --- a/src/threading/Reaction.hpp +++ b/src/threading/Reaction.hpp @@ -27,6 +27,7 @@ #include #include #include +#include "../id.hpp" #include #include "../util/GeneratedCallback.hpp" @@ -107,7 +108,7 @@ namespace threading { ReactionIdentifiers identifiers; /// @brief the unique identifier for this Reaction object - const uint64_t id{++reaction_id_source}; + const NUClear::id_t id{++reaction_id_source}; /// @brief if this is false, we cannot emit ReactionStatistics from any reaction triggered by this one bool emit_stats{true}; @@ -128,7 +129,8 @@ namespace threading { private: /// @brief a source for reaction_ids, atomically creates longs - static std::atomic reaction_id_source; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + static std::atomic + reaction_id_source; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) /// @brief the callback generator function (creates databound callbacks) TaskGenerator generator; }; diff --git a/src/threading/ReactionTask.hpp b/src/threading/ReactionTask.hpp index 27b0c3f15..25260e6ad 100644 --- a/src/threading/ReactionTask.hpp +++ b/src/threading/ReactionTask.hpp @@ -28,7 +28,7 @@ #include #include #include - +#include "../id.hpp" #include "../message/ReactionStatistics.hpp" #include "../util/GroupDescriptor.hpp" #include "../util/ThreadPoolDescriptor.hpp" @@ -50,7 +50,7 @@ namespace threading { class Task { private: /// @brief a source for task ids, atomically creates longs - static std::atomic task_id_source; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) + static std::atomic task_id_source; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) /// @brief the current task that is being executed by this thread (or nullptr if none is) static ATTRIBUTE_TLS Task* current_task; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) @@ -121,14 +121,14 @@ namespace threading { * * @return a new unique task id */ - static inline uint64_t new_task_id() { + static inline NUClear::id_t new_task_id() { return ++task_id_source; } /// @brief the parent Reaction object which spawned this ReactionType& parent; /// @brief the task id of this task (the sequence number of this particular task) - uint64_t id{new_task_id()}; + NUClear::id_t id{new_task_id()}; /// @brief the priority to run this task at int priority; /// @brief the statistics object that persists after this for information and debugging @@ -151,7 +151,7 @@ namespace threading { // Initialize our id source template - std::atomic Task::task_id_source(0); // NOLINT + std::atomic Task::task_id_source(0); // NOLINT // Initialize our current task template diff --git a/src/threading/TaskScheduler.cpp b/src/threading/TaskScheduler.cpp index 8cf1d3dd2..96d8ac326 100644 --- a/src/threading/TaskScheduler.cpp +++ b/src/threading/TaskScheduler.cpp @@ -168,7 +168,7 @@ namespace threading { } } - void TaskScheduler::submit(const uint64_t& id, + void TaskScheduler::submit(const NUClear::id_t& id, const int& priority, const util::GroupDescriptor& group_descriptor, const util::ThreadPoolDescriptor& pool_descriptor, diff --git a/src/threading/TaskScheduler.hpp b/src/threading/TaskScheduler.hpp index e58e17197..3859e0af2 100644 --- a/src/threading/TaskScheduler.hpp +++ b/src/threading/TaskScheduler.hpp @@ -32,6 +32,7 @@ #include #include +#include "../id.hpp" #include "../util/GroupDescriptor.hpp" #include "../util/ThreadPoolDescriptor.hpp" #include "../util/platform.hpp" @@ -93,7 +94,7 @@ namespace threading { */ struct Task { /// @brief The id of this task used for ordering - uint64_t id; + NUClear::id_t id; /// @brief The priority of this task int priority; /// @brief The group descriptor for this task @@ -168,7 +169,7 @@ namespace threading { * as normal * @param func the function to execute */ - void submit(const uint64_t& id, + void submit(const NUClear::id_t& id, const int& priority, const util::GroupDescriptor& group, const util::ThreadPoolDescriptor& pool, @@ -242,12 +243,12 @@ namespace threading { std::atomic started{false}; /// @brief A map of group ids to the number of active tasks currently running in that group - std::map groups{}; + std::map groups{}; /// @brief mutex for the group map std::mutex group_mutex; /// @brief A map of pool descriptor ids to pool descriptors - std::map> pool_queues{}; + std::map> pool_queues{}; /// @brief a mutex for when we are modifying the pool_queues map std::mutex pool_mutex; /// @brief a pointer to the pool_queue for the current thread so it does not have to access via the map diff --git a/src/util/GroupDescriptor.hpp b/src/util/GroupDescriptor.hpp index fbcdba126..3561b94ea 100644 --- a/src/util/GroupDescriptor.hpp +++ b/src/util/GroupDescriptor.hpp @@ -28,6 +28,8 @@ #include #include +#include "../id.hpp" + namespace NUClear { namespace util { @@ -36,7 +38,7 @@ namespace util { */ struct GroupDescriptor { /// @brief a unique identifier for this pool - uint64_t group_id{0}; + NUClear::id_t group_id{0}; /// @brief the maximum number of threads that can run concurrently in this group size_t thread_count{std::numeric_limits::max()}; @@ -44,9 +46,9 @@ namespace util { /** * @brief Return the next unique ID for a new group */ - static uint64_t get_unique_group_id() noexcept { + static NUClear::id_t get_unique_group_id() noexcept { // Make group 0 the default group - static std::atomic source{1}; + static std::atomic source{1}; return source++; } }; diff --git a/src/util/ThreadPoolDescriptor.cpp b/src/util/ThreadPoolDescriptor.cpp index e91c36d44..b044aff44 100644 --- a/src/util/ThreadPoolDescriptor.cpp +++ b/src/util/ThreadPoolDescriptor.cpp @@ -25,8 +25,8 @@ namespace NUClear { namespace util { - const uint64_t ThreadPoolDescriptor::MAIN_THREAD_POOL_ID = 0; - const uint64_t ThreadPoolDescriptor::DEFAULT_THREAD_POOL_ID = 1; + const NUClear::id_t ThreadPoolDescriptor::MAIN_THREAD_POOL_ID = 0; + const NUClear::id_t ThreadPoolDescriptor::DEFAULT_THREAD_POOL_ID = 1; } // namespace util } // namespace NUClear diff --git a/src/util/ThreadPoolDescriptor.hpp b/src/util/ThreadPoolDescriptor.hpp index 9dd545f0a..f6de67408 100644 --- a/src/util/ThreadPoolDescriptor.hpp +++ b/src/util/ThreadPoolDescriptor.hpp @@ -27,6 +27,8 @@ #include #include +#include "../id.hpp" + namespace NUClear { namespace util { @@ -35,21 +37,21 @@ namespace util { */ struct ThreadPoolDescriptor { /// @brief a unique identifier for this pool - uint64_t pool_id{ThreadPoolDescriptor::DEFAULT_THREAD_POOL_ID}; + NUClear::id_t pool_id{ThreadPoolDescriptor::DEFAULT_THREAD_POOL_ID}; /// @brief the number of threads this thread pool will use size_t thread_count{0}; /// @brief the ID of the main thread pool (not to be confused with the ID of the main thread) - static const uint64_t MAIN_THREAD_POOL_ID; + static const NUClear::id_t MAIN_THREAD_POOL_ID; /// @brief the ID of the default thread pool - static const uint64_t DEFAULT_THREAD_POOL_ID; + static const NUClear::id_t DEFAULT_THREAD_POOL_ID; /** * @brief Return the next unique ID for a new thread pool */ - static uint64_t get_unique_pool_id() noexcept { - static std::atomic source{2}; + static NUClear::id_t get_unique_pool_id() noexcept { + static std::atomic source{2}; return source++; } };