Orocos Real-Time Toolkit  2.8.3
Timer.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: FMTC Tue Mar 11 21:49:25 CET 2008 Timer.hpp
3 
4  Timer.hpp - description
5  -------------------
6  begin : Tue March 11 2008
7  copyright : (C) 2008 FMTC
8  email : peter.soetens@fmtc.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef ORO_RTT_TIMER_HPP
40 #define ORO_RTT_TIMER_HPP
41 
42 #include "Time.hpp"
43 #include "TimeService.hpp"
44 #include "Mutex.hpp"
45 #include "Semaphore.hpp"
46 #include "Condition.hpp"
47 #include "ThreadInterface.hpp"
48 #include "../base/RunnableInterface.hpp"
49 #include <vector>
50 #include <utility>
51 
52 namespace RTT
53 { namespace os {
54 
67  class RTT_API Timer
69  {
70  public:
74  typedef int TimerId;
75  protected:
78  mutable Mutex mmutex;
80 
81  struct TimerInfo
82  {
83  TimerInfo() : expires(0), period(0) {}
84  TimerInfo(const TimerInfo& other) { *this = other; }
85  TimerInfo& operator=(const TimerInfo& other) { this->expires = other.expires; this->period = other.period; return *this; }
86  Time expires; // was .first
87  Time period; // was .second
89  };
90 
96  typedef std::vector<TimerInfo> TimerIds;
97  TimerIds mtimers;
98  bool mdo_quit;
99 
100  bool initialize();
101  void finalize();
102  void step();
103 
104  // The main 'event' loop.
105  void loop();
106 
107  bool breakLoop();
108 
109  public:
122  Timer(TimerId max_timers, int scheduler = -1, int priority = 0);
123 
124  ~Timer();
125 
131  virtual void timeout(TimerId timer_id);
132 
137  void setMaxTimers(TimerId max);
138 
147  bool startTimer(TimerId timer_id, Seconds period);
148 
156  bool arm(TimerId timer_id, Seconds wait_time);
157 
163  TimeService::Seconds timeRemaining(TimerId timer_id) const;
164 
169  bool isArmed(TimerId timer_id) const;
170 
175  bool killTimer(TimerId timer_id);
176 
182  bool waitFor(RTT::os::Timer::TimerId id);
183 
192  bool waitForUntil(RTT::os::Timer::TimerId id, nsecs abs_time);
193 
194  };
195 }}
196 
197 #endif
Condition mcond
Definition: Timer.hpp:77
TimerInfo(const TimerInfo &other)
Definition: Timer.hpp:84
double Seconds
Seconds are stored as a double precision float.
Definition: Time.hpp:53
std::vector< TimerInfo > TimerIds
Index in vector is the timer id.
Definition: Timer.hpp:96
TimerInfo & operator=(const TimerInfo &other)
Definition: Timer.hpp:85
base::ActivityInterface * mThread
Definition: Timer.hpp:76
A class for running a certain piece of code in a thread.
int TimerId
A positive numeric ID representing a timer.
Definition: Timer.hpp:74
#define RTT_API
Definition: rtt-config.h:97
An object oriented wrapper around a condition variable.
Definition: Condition.hpp:60
TimeService::nsecs Time
Definition: Timer.hpp:79
Interface to start/stop and query a Activity.
TimerIds mtimers
Definition: Timer.hpp:97
double Seconds
The type used to store SI unit time keeping.
Definition: TimeService.hpp:41
Mutex mmutex
Definition: Timer.hpp:78
This class allows to define a timer object which can be armed in single shot and periodic mode...
Definition: Timer.hpp:67
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:82
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
long long nsecs
nanoseconds as a signed long long.
Definition: Time.hpp:69
long long nsecs
An integer representation of nanoseconds.
Definition: TimeService.hpp:61
bool mdo_quit
Definition: Timer.hpp:98