Worker.hpp
Go to the documentation of this file.
1 /**********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016, Philipp Leemann
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Robotic Systems Lab nor ETH Zurich
18  * nor the names of its contributors may be used to endorse or
19  * promote products derived from this software without specific
20  * prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
42 #pragma once
43 
44 #include <atomic>
45 #include <functional>
46 #include <string>
47 #include <thread>
48 
49 #include "bota_worker/Rate.hpp"
52 
53 namespace bota_worker
54 {
55 class Worker
56 {
57 public:
58  Worker() = delete;
59 
67  Worker(const std::string& name, const double timestep, const WorkerCallback& callback);
68  Worker(const std::string& name, const double timestep, const WorkerCallback& callback,
69  const WorkerCallbackFailureReaction& callbackFailureReaction);
70  explicit Worker(const WorkerOptions& options);
71  Worker(const Worker&) = delete; // atomics and threads are non-copyable
72  Worker(Worker&& /*other*/) noexcept; // declare custom move constructor to move atomics
73 
74  virtual ~Worker();
75 
76  bool start(const int priority = 0);
77  void stop(const bool wait = true);
78 
79  void setTimestep(const double timeStep);
80  void setEnforceRate(const bool enforceRate);
81 
82  const std::string& getName() const
83  {
84  return options_.name_;
85  }
86  const Rate& getRate() const
87  {
88  return rate_;
89  }
91  {
92  return rate_;
93  }
94 
95  bool isRunning() const
96  {
97  return running_;
98  }
99 
103  bool isDestructible() const
104  {
105  return done_.load() && options_.destructWhenDone_;
106  }
107 
108 private:
109  void run();
110 
111 private:
113 
114  std::atomic<bool> running_;
115  std::atomic<bool> done_;
116 
117  std::thread thread_;
119 };
120 
121 } // namespace bota_worker
const Rate & getRate() const
Definition: Worker.hpp:86
const std::string & getName() const
Definition: Worker.hpp:82
virtual ~Worker()
Definition: Worker.cpp:77
WorkerOptions options_
Definition: Worker.hpp:112
void stop(const bool wait=true)
Definition: Worker.cpp:125
Rate & getRate()
Definition: Worker.hpp:90
std::function< void(void)> WorkerCallbackFailureReaction
bool start(const int priority=0)
Definition: Worker.cpp:82
void setEnforceRate(const bool enforceRate)
Definition: Worker.cpp:149
std::atomic< bool > done_
Definition: Worker.hpp:115
std::function< bool(const WorkerEvent &)> WorkerCallback
std::atomic< bool > running_
Definition: Worker.hpp:114
bool isDestructible() const
Definition: Worker.hpp:103
void setTimestep(const double timeStep)
Definition: Worker.cpp:134
std::thread thread_
Definition: Worker.hpp:117
bool isRunning() const
Definition: Worker.hpp:95
std::string name_
Name for printing.
Definition: RateOptions.hpp:57


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