-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a DSL word that runs when a specific pool or all pools are idle (#…
…100) Adds a new DSL word Idle that can be used to execute a reaction when the pool does not have a current task. There are 4 main variants that this implements ```cpp on<Idle<>> ``` Will execute when all non always threads are idle ```cpp on<Idle<Pool<>> ``` Will execute when the default thread pool is idle ```cpp on<Idle<MainThread>> ``` Will execute when the main thread's pool is idle ```cpp on<Idle<SomePoolDescriptor>> ``` Will execute when that specific pool is idle - [x] Fix up the deadlock caused by the recursive mutex acquisition --------- Co-authored-by: Thomas O'Brien <[email protected]> Co-authored-by: Tom0Brien <[email protected]>
- Loading branch information
1 parent
f6704a1
commit 1599e91
Showing
17 changed files
with
547 additions
and
40 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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_DSL_WORD_IDLE_HPP | ||
#define NUCLEAR_DSL_WORD_IDLE_HPP | ||
|
||
#include <map> | ||
#include <mutex> | ||
#include <typeindex> | ||
|
||
#include "../../threading/ReactionTask.hpp" | ||
#include "../fusion/NoOp.hpp" | ||
#include "MainThread.hpp" | ||
#include "Pool.hpp" | ||
|
||
namespace NUClear { | ||
namespace dsl { | ||
namespace word { | ||
|
||
/** | ||
* @brief A base type to handle the common code for idling after turning the pool descriptor into an id. | ||
*/ | ||
inline void bind_idle(const std::shared_ptr<threading::Reaction>& reaction, | ||
const util::ThreadPoolDescriptor& pool_descriptor) { | ||
|
||
// 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); | ||
}); | ||
|
||
reaction->reactor.powerplant.add_idle_task(reaction->id, pool_descriptor, [reaction] { | ||
reaction->reactor.powerplant.submit(reaction->get_task()); | ||
}); | ||
} | ||
|
||
/** | ||
* @brief Execute a task when there is nothing currently running on the thread pool. | ||
* | ||
* @details | ||
* @code on<Idle<PoolType>>() @endcode | ||
* When the thread pool is idle, this task will be executed. This is use | ||
* | ||
* @par Implements | ||
* Bind | ||
* | ||
* @tparam PoolType the descriptor that was used to create the thread pool | ||
* void for the default pool | ||
* MainThread for the main thread pool | ||
*/ | ||
template <typename PoolType> | ||
struct Idle { | ||
template <typename DSL> | ||
static inline void bind(const std::shared_ptr<threading::Reaction>& reaction) { | ||
bind_idle(reaction, PoolType::template pool<DSL>(*reaction)); | ||
} | ||
}; | ||
|
||
template <> | ||
struct Idle<void> { | ||
template <typename DSL> | ||
static inline void bind(const std::shared_ptr<threading::Reaction>& reaction) { | ||
bind_idle(reaction, util::ThreadPoolDescriptor::AllPools()); | ||
} | ||
}; | ||
|
||
} // namespace word | ||
} // namespace dsl | ||
} // namespace NUClear | ||
|
||
#endif // NUCLEAR_DSL_WORD_IDLE_HPP |
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.