source: rrlib_thread/tLoopThread.h @ 0:c2492b69e880

Last change on this file since 0:c2492b69e880 was 0:c2492b69e880, checked in by Max Reichardt <mreichardt@…>, 7 years ago

Initial commit. These are refactored thread classes that were moved out of finroc_core_utils.

File size: 5.7 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/thread/tLoopThread.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2012-07-05
27 *
28 * \brief   Contains tLoopThread
29 *
30 * \b tLoopThread
31 *
32 * A Thread that calls a callback function with a specified rate.
33 *
34 */
35//----------------------------------------------------------------------
36#ifndef __rrlib__thread__tLoopThread_h__
37#define __rrlib__thread__tLoopThread_h__
38
39//----------------------------------------------------------------------
40// External includes (system with <>, local with "")
41//----------------------------------------------------------------------
42#include "rrlib/time/tAtomicDuration.h"
43
44//----------------------------------------------------------------------
45// Internal includes with ""
46//----------------------------------------------------------------------
47#include "rrlib/thread/tThread.h"
48
49//----------------------------------------------------------------------
50// Namespace declaration
51//----------------------------------------------------------------------
52namespace rrlib
53{
54namespace thread
55{
56
57//----------------------------------------------------------------------
58// Forward declarations / typedefs / enums
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Class declaration
63//----------------------------------------------------------------------
64//! Loop Thread.
65/*!
66 * A Thread that calls a callback function with a specified rate.
67 */
68class tLoopThread : public tThread
69{
70
71//----------------------------------------------------------------------
72// Public methods and typedefs
73//----------------------------------------------------------------------
74public:
75
76  /*!
77   * \param default_cycle_time Cycle time with which callback function is called
78   * \param use_application_time Use "application time" (see rrlib/util/time.h) instead of system time?
79   * \param warn_on_cycle_time_exceed Display warning, if cycle time is exceeded?
80   * \param pause_on_startup Pause Signal set at startup of this thread?
81   */
82  explicit tLoopThread(rrlib::time::tDuration default_cycle_time, bool use_application_time = true, bool warn_on_cycle_time_exceed = false, bool pause_on_startup = false);
83
84  /*!
85   * Resume Thread;
86   */
87  void ContinueThread();
88
89  /*!
90   * \return Start time of current cycle (always smaller than rrlib::time::Now())
91   */
92  inline rrlib::time::tTimestamp GetCurrentCycleStartTime()
93  {
94    assert(CurrentThreadRaw() == this && "Please only call from this thread");
95    return last_cycle_start;
96  }
97
98  /*!
99   * \return Current Cycle time with which callback function is called
100   */
101  inline rrlib::time::tDuration GetCycleTime()
102  {
103    return cycle_time.Load();
104  }
105
106  /*!
107   * \return Time spent in last call to MainLoopCallback()
108   */
109  inline rrlib::time::tDuration GetLastCycleTime()
110  {
111    return last_cycle_time.Load();
112  }
113
114  /*!
115   * \return Is Thread currently paused?
116   */
117  inline bool IsPausing()
118  {
119    return pause_signal;
120  }
121
122  /*!
123   * \return Is thread currently running? (and not paused)
124   */
125  inline bool IsRunning()
126  {
127    return IsAlive() && !IsPausing();
128  }
129
130  /*!
131   * \return Is the stop signal set in order to stop the thread?
132   */
133  inline bool IsStopSignalSet()
134  {
135    return stop_signal;
136  }
137
138  /*!
139   * Callback function that is called with the specified rate
140   */
141  virtual void MainLoopCallback() = 0;
142
143  /*!
144   * Pause Thread.
145   */
146  inline void PauseThread()
147  {
148    pause_signal = true;
149  }
150
151  virtual void Run();
152
153  /*!
154   * \param cycle_time New Cycle time with which callback function is called
155   */
156  inline void SetCycleTime(const rrlib::time::tDuration& cycle_time)
157  {
158    this->cycle_time.Store(cycle_time);
159  }
160
161
162//----------------------------------------------------------------------
163// Private fields and methods
164//----------------------------------------------------------------------
165private:
166
167  /*! Thread pauses if this flag is set */
168  std::atomic<bool> pause_signal;
169
170  /*! Cycle time with which callback function is called */
171  rrlib::time::tAtomicDuration cycle_time;
172
173  /*! Use "application time" (see rrlib/util/time.h) instead of system time? */
174  const bool use_application_time;
175
176  /*! Display warning, if cycle time is exceeded? */
177  const bool warn_on_cycle_time_exceed;
178
179  /*! Display warnings on console? */
180  static const bool cDISPLAYWARNINGS = false;
181
182  /*! Time spent in last call to MainLoopCallback() */
183  rrlib::time::tAtomicDuration last_cycle_time;
184
185  /*! Start time of last cycle */
186  rrlib::time::tTimestamp last_cycle_start;
187
188  /*! Time thread waited in last loop */
189  rrlib::time::tDuration last_wait;
190
191
192  /*!
193   * The main loop
194   */
195  void MainLoop();
196
197};
198
199//----------------------------------------------------------------------
200// End of namespace declaration
201//----------------------------------------------------------------------
202}
203}
204
205
206#endif
Note: See TracBrowser for help on using the repository browser.