Capabilities2 0.3.0
Loading...
Searching...
No Matches
threadtrigger_runner.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <map>
5#include <atomic>
6#include <mutex>
7#include <thread>
8
11#include <capabilities2_msgs/msg/capability_event_code.hpp>
12
14{
15
21{
22public:
30 static const std::string bond_from_thread_id(const std::string& thread_id)
31 {
32 // extract bond_id from thread_id (format: "bond_id/trigger_id")
33 size_t slash_pos = thread_id.find('/');
34 std::string bond_id = (slash_pos != std::string::npos) ? thread_id.substr(0, slash_pos) : thread_id;
35 return bond_id;
36 }
37
44 static const std::string instance_from_thread_id(const std::string& thread_id)
45 {
46 // extract instance_id from thread_id (format: "bond_id/instance_id")
47 size_t slash_pos = thread_id.find('/');
48 std::string instance_id = (slash_pos != std::string::npos) ? thread_id.substr(slash_pos + 1) : "";
49 return instance_id;
50 }
51
52public:
56
58 {
59 // stop any running threads on destruction
60 stop_execution(std::chrono::milliseconds(500));
61 }
62
73 virtual void trigger(capabilities2_events::EventParameters& parameters, const std::string& bond_id,
74 const std::string& instance_id) override
75 {
76 // namespace the thread id with bond id for later
77 // could list all threads related to a bond if needed
78 std::string thread_id = bond_id + "/" + instance_id;
79
80 // start execution thread
81 {
82 std::scoped_lock lock(mutex_);
83 // TODO: consider emitting on start event here
84 // emit_event(bond_id, capabilities2_msgs::msg::CapabilityEventCode::ON_STARTED, updated_on_started(parameters));
85 execution_thread_pool_[thread_id] = std::thread(&ThreadTriggerRunner::execution, this, parameters, thread_id);
86 }
87
88 // emit trigger event
89 emit_event(bond_id, instance_id, capabilities2_msgs::msg::CapabilityEventCode::TRIGGERED);
90
91 // BUG: thread management?
92
93 // TODO: consider emitting on stop event here
94 // emit_event(bond_id, capabilities2_msgs::msg::CapabilityEventCode::ON_STOPPED, updated_on_stopped(parameters));
95
96 RCLCPP_DEBUG(node_->get_logger(), "started execution thread for thread id: %s", thread_id.c_str());
97 }
98
99protected:
110 virtual void execution(capabilities2_events::EventParameters parameters, const std::string& thread_id) = 0;
111
112private:
114 void stop_execution(std::chrono::milliseconds timeout = std::chrono::milliseconds(500))
115 {
116 // signal listening threads to stop
118
119 // try join in a non-blocking way and log if threads are not cleaned up properly
120 {
121 std::scoped_lock lock(mutex_);
122 for (auto& [thread_id, exec_thread] : execution_thread_pool_)
123 {
124 if (exec_thread.joinable())
125 {
126 // TODO: FIX THIS FUTURE MICHAEL
127 exec_thread.join();
128 }
129 }
130
131 // clear the thread pool
133 }
134 }
135
136protected:
140 std::map<std::string, std::thread> execution_thread_pool_;
141
145 std::mutex mutex_;
146
150 std::atomic<bool> execution_should_stop_;
151};
152
153} // namespace capabilities2_runner
void emit_event(const std::string &bond_id, const std::string &instance_id, const uint8_t &event_type, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit an event from this event node to all matching connections
Definition event_node.hpp:64
base class for all runners
Definition runner_base.hpp:74
rclcpp::Node::SharedPtr node_
shared pointer to the capabilities node Allows to use ros node related functionalities
Definition runner_base.hpp:469
add threaded trigger execution to the runner
Definition threadtrigger_runner.hpp:21
static const std::string bond_from_thread_id(const std::string &thread_id)
helper function to extract bond_id from thread_id
Definition threadtrigger_runner.hpp:30
void stop_execution(std::chrono::milliseconds timeout=std::chrono::milliseconds(500))
Definition threadtrigger_runner.hpp:114
ThreadTriggerRunner()
Definition threadtrigger_runner.hpp:53
~ThreadTriggerRunner()
Definition threadtrigger_runner.hpp:57
virtual void execution(capabilities2_events::EventParameters parameters, const std::string &thread_id)=0
Trigger process to be executed.
virtual void trigger(capabilities2_events::EventParameters &parameters, const std::string &bond_id, const std::string &instance_id) override
Trigger the runner.
Definition threadtrigger_runner.hpp:73
std::atomic< bool > execution_should_stop_
flag to signal execution threads to stop.
Definition threadtrigger_runner.hpp:150
std::map< std::string, std::thread > execution_thread_pool_
dictionary of threads that executes the execute function
Definition threadtrigger_runner.hpp:140
std::mutex mutex_
mutex for threadpool synchronisation.
Definition threadtrigger_runner.hpp:145
static const std::string instance_from_thread_id(const std::string &thread_id)
helper function to extract instance_id from thread_id
Definition threadtrigger_runner.hpp:44
Definition action_runner.hpp:19
capability options for a capability runner given by interface and provider
Definition event_parameters.hpp:167