WorkerManager.cpp
Go to the documentation of this file.
1 
8 #include <ros/ros.h>
9 namespace bota_worker
10 {
11 WorkerManager::WorkerManager() : workers_(), mutexWorkers_()
12 {
13 }
14 
16 {
17  cancelWorkers();
18 }
19 
20 bool WorkerManager::addWorker(const WorkerOptions& options, const bool autostart)
21 {
22  std::lock_guard<std::mutex> lock(mutexWorkers_);
23  auto inserted_element = workers_.emplace(options.name_, Worker(options));
24  if (!inserted_element.second)
25  {
26  ROS_ERROR("Failed to create worker [%s]", options.name_.c_str());
27  return false;
28  }
29  if (autostart)
30  {
31  return inserted_element.first->second.start();
32  }
33  return true;
34 }
35 
36 // bool WorkerManager::addWorker(Worker&& worker) {
37 // std::lock_guard<std::mutex> lock(mutexWorkers_);
38 // auto insertedElement = workers_.emplace( worker.getName(), std::move(worker) );
39 // if(!insertedElement.second) {
40 // ROS_ERROR("Failed to move worker [%s]", worker.getName().c_str());
41 // return false;
42 // }
43 // return true;
44 //}
45 
46 void WorkerManager::startWorker(const std::string& name, const int priority)
47 {
48  std::lock_guard<std::mutex> lock(mutexWorkers_);
49  auto worker = workers_.find(name);
50  if (worker == workers_.end())
51  {
52  ROS_ERROR("Cannot start worker [%s], worker not found", name.c_str());
53  return;
54  }
55  worker->second.start(priority);
56 }
57 
59 {
60  std::lock_guard<std::mutex> lock(mutexWorkers_);
61  for (auto& worker : workers_)
62  {
63  worker.second.start();
64  }
65 }
66 
67 void WorkerManager::stopWorker(const std::string& name, const bool wait)
68 {
69  std::lock_guard<std::mutex> lock(mutexWorkers_);
70  auto worker = workers_.find(name);
71  if (worker == workers_.end())
72  {
73  ROS_ERROR("Cannot stop worker [%s], worker not found", name.c_str());
74  return;
75  }
76  worker->second.stop(wait);
77 }
78 
79 void WorkerManager::stopWorkers(const bool wait)
80 {
81  std::lock_guard<std::mutex> lock(mutexWorkers_);
82  for (auto& worker : workers_)
83  {
84  worker.second.stop(wait);
85  }
86 }
87 
88 bool WorkerManager::hasWorker(const std::string& name)
89 {
90  std::lock_guard<std::mutex> lock(mutexWorkers_);
91  return (workers_.find(name) != workers_.end());
92 }
93 
94 void WorkerManager::cancelWorker(const std::string& name, const bool wait)
95 {
96  std::lock_guard<std::mutex> lock(mutexWorkers_);
97  auto worker = workers_.find(name);
98  if (worker == workers_.end())
99  {
100  ROS_ERROR("Cannot stop worker [%s], worker not found", name.c_str());
101  return;
102  }
103  worker->second.stop(wait);
104  workers_.erase(worker);
105 }
106 
107 void WorkerManager::cancelWorkers(const bool wait)
108 {
109  std::lock_guard<std::mutex> lock(mutexWorkers_);
110 
111  // signal all workers to stop
112  for (auto& worker : workers_)
113  {
114  worker.second.stop(wait);
115  }
116 
117  // call destructors of all workers, which will join the underlying thread
118  workers_.clear();
119 }
120 
121 void WorkerManager::setWorkerTimestep(const std::string& name, const double timeStep)
122 {
123  std::lock_guard<std::mutex> lock(mutexWorkers_);
124  auto worker = workers_.find(name);
125  if (worker == workers_.end())
126  {
127  ROS_ERROR("Cannot change timestep of worker [%s], worker not found", name.c_str());
128  return;
129  }
130  worker->second.setTimestep(timeStep);
131 }
132 
134 {
135  std::lock_guard<std::mutex> lock(mutexWorkers_);
136  for (auto it = workers_.begin(); it != workers_.end();)
137  {
138  if (it->second.isDestructible())
139  {
140  it = workers_.erase(it);
141  }
142  else
143  {
144  ++it;
145  }
146  }
147 }
148 
149 } // namespace bota_worker
bool hasWorker(const std::string &name)
void stopWorker(const std::string &name, const bool wait=true)
void setWorkerTimestep(const std::string &name, const double timeStep)
void stopWorkers(const bool wait=true)
std::unordered_map< std::string, Worker > workers_
void cancelWorkers(const bool wait=true)
void startWorker(const std::string &name, const int priority=0)
std::string name_
Name for printing.
Definition: RateOptions.hpp:57
#define ROS_ERROR(...)
bool addWorker(const std::string &name, const double timestep, bool(T::*fp)(const WorkerEvent &), T *obj, const int priority=0, const bool autostart=true)
void cancelWorker(const std::string &name, const bool wait=true)


bota_worker
Author(s):
autogenerated on Wed Mar 3 2021 03:09:10