source: rrlib_thread/tLoopThread.h @ 4:46a4150b6fd3

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

tLoopThread now allows to set whether to use application or system time after construction.
Cleaned up thread class code.

File size: 6.3 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() const
93  {
94    assert(&CurrentThread() == 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() const
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() const
110  {
111    return last_cycle_time.Load();
112  }
113
114  /*!
115   * \return Is Thread currently paused?
116   */
117  inline bool IsPausing() const
118  {
119    return pause_signal;
120  }
121
122  /*!
123   * \return Is thread currently running? (and not paused)
124   */
125  inline bool IsRunning() const
126  {
127    return IsAlive() && !IsPausing();
128  }
129
130  /*!
131   * \return Is this thread using "application time" instead of system time (see rrlib/util/time.h)?
132   */
133  inline bool IsUsingApplicationTime() const
134  {
135    return use_application_time;
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   * \param use_application_time Use "application time" (see rrlib/util/time.h) instead of system time?
163   *
164   * This will not have any effect on Sleep() and Wait() operations this thread might be executing.
165   *
166   * (This method is preferably called from this thread - to avoid issues in case
167   *  thread is currently performing operations with another time base)
168   */
169  void SetUseApplicationTime(bool use_application_time);
170
171//----------------------------------------------------------------------
172// Private fields and methods
173//----------------------------------------------------------------------
174private:
175
176  // TODO: For optimization, we could put the following three atomic variables in a single 64-bit atomic
177
178  /*! Thread pauses if this flag is set */
179  std::atomic<bool> pause_signal;
180
181  /*! Cycle time with which callback function is called */
182  rrlib::time::tAtomicDuration cycle_time;
183
184  /*! Use "application time" (see rrlib/util/time.h) instead of system time? */
185  std::atomic<bool> use_application_time;
186
187  /*! Display warning, if cycle time is exceeded? */
188  const bool warn_on_cycle_time_exceed;
189
190  /*! Display warnings on console? */
191  static const bool cDISPLAYWARNINGS = false;
192
193  /*! Time spent in last call to MainLoopCallback() */
194  rrlib::time::tAtomicDuration last_cycle_time;
195
196  /*! Start time of last cycle */
197  rrlib::time::tTimestamp last_cycle_start;
198
199  /*! Time thread waited in last loop */
200  rrlib::time::tDuration last_wait;
201
202
203  /*!
204   * The main loop
205   */
206  void MainLoop();
207
208};
209
210//----------------------------------------------------------------------
211// End of namespace declaration
212//----------------------------------------------------------------------
213}
214}
215
216
217#endif
Note: See TracBrowser for help on using the repository browser.