source: flair-dev/trunk/include/FlairCore/Thread.h @ 13

Last change on this file since 13 was 13, checked in by Bayard Gildas, 5 years ago

formatting script + include reformatted

File size: 5.0 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5/*!
6 * \file Thread.h
7 * \brief Abstract class for a thread
8 * \author Guillaume Sanahuja, Copyright Heudiasyc UMR UTC/CNRS 7253
9 * \date 2012/10/04
10 * \version 4.0
11 */
12
13#ifndef THREAD_H
14#define THREAD_H
15
16#include <Object.h>
17#include <stdint.h>
18
19class Thread_impl;
20
21namespace flair {
22namespace core {
23
24class IODevice;
25
26/*! \class Thread
27*
28* \brief Abstract class for a thread
29*
30* To implement a thread, Run() method must be reimplemented. \n
31* When Start() is called, it will automatically call Run() reimplemented method.
32* A thread can be periodic, in this case WaitPeriod() will block untill period
33*is met.
34* Thread can also e synnchronized with an IODevice, using WaitUpdate() method.
35*\n
36* Thread period is by default 100ms.
37*/
38class Thread : public Object {
39  friend class ::Thread_impl;
40
41public:
42  /*!
43  * \brief Constructor
44  *
45  * \param parent parent
46  * \param name name
47  * \param priority priority, should be >20 (<20 is reserved for internal use)
48  */
49  Thread(const Object *parent, std::string name,
50         uint8_t priority); // priority>20, for real time only
51
52  /*!
53  * \brief Destructor
54  *
55  * If thread is started, SafeStop() and Join() will
56  * be automatically called.
57  *
58  */
59  virtual ~Thread();
60
61  /*!
62  * \brief Start the thread
63  *
64  */
65  void Start(void);
66
67  /*!
68  * \brief Set a stop flag
69  *
70  * ToBeStopped() will return true after calling this method.
71  */
72  void SafeStop(void);
73
74  /*!
75  * \brief Set a stop flag
76  *
77  * Reimplemented Run() can poll this method to
78  * determine when to stop the thread.
79  *
80  * \return true if SafeStop() was called
81  */
82  bool ToBeStopped(void) const;
83
84  /*!
85  * \brief Join the thread
86  *
87  * This method will block untill Run() returns.
88  *
89  */
90  void Join(void);
91
92  /*!
93  * \brief Set the period in micro second
94  *
95  * After calling this method, IsPeriodSet will return true.
96  *
97  * \param period_us period in us
98  */
99  void SetPeriodUS(uint32_t period_us);
100
101  uint32_t GetPeriodUS() const;
102
103  /*!
104  * \brief Set the period in milli second
105  *
106  * After calling this method, IsPeriodSet will return true.
107  *
108  * \param period_ums period in ms
109  */
110  void SetPeriodMS(uint32_t period_ms);
111
112  uint32_t GetPeriodMS() const;
113
114  /*!
115  * \brief Returns if period was set
116  *
117  * \return true if a period was set using SetPeriodUS or SetPeriodMS
118  * false otherwise
119  */
120  bool IsPeriodSet(void);
121
122  /*!
123  * \brief Wait the period
124  *
125  * This method will block untill period is met. \n
126  * If no period was set (see SetPeriodUS, SetPeriodMS and IsPeriodSet), this
127  *method
128  * returns immediately.
129  *
130  */
131  void WaitPeriod(void) const;
132
133  /*!
134  * \brief Wait update of an IODevice
135  *
136  * This method will block untill IODevice::ProcessUpdate
137  * is called. \n
138  * This method is usefull to synchronize a thread with an IODevice.
139  *
140  * \param device IODevice to wait update from
141  */
142  int WaitUpdate(const IODevice *device);
143
144  /*!
145  * \brief Suspend the thread
146  *
147  * This method will block untill Resume() is called.
148  *
149  */
150  void Suspend(void);
151
152  /*!
153  * \brief Suspend the thread with timeout
154  *
155  * This method will block until Resume() is called or the absolute date
156  *specified occurs
157  *
158  * \param date absolute date in ns
159  * \return true if thread is woken up by a call to Resume, false otherwise
160  */
161  bool SuspendUntil(Time date);
162
163  /*!
164  * \brief Resume the thread
165  *
166  * This method will unblock the call to Suspend().
167  *
168  */
169  void Resume(void);
170
171  /*!
172  * \brief Is the thread suspended?
173  *
174  * \return true if thread is suspended
175  *
176  */
177  bool IsSuspended(void) const;
178
179  /*!
180  * \brief Sleep until absolute time
181  *
182  * This method will block untill time is reached.
183  *
184  * \param time absolute time
185  */
186  void SleepUntil(Time time) const;
187
188  /*!
189  * \brief Sleep for a certain time in micro second
190  *
191  * This method will block untill time is elapsed.
192  *
193  * \param time_us time to wait in micro second
194  */
195  void SleepUS(uint32_t time_us) const;
196
197  /*!
198  * \brief Sleep for a cartain time in milli second
199  *
200  * This method will block untill time is elapsed.
201  *
202  * \param time_ms time to wait in milli second
203  */
204  void SleepMS(uint32_t time_ms) const;
205
206  /*!
207  * \brief Warn if real time / non real time switches occur
208  *
209  * If enabled, a message with the call stack will be displayed
210  * in case of real time / non real time switches. \n
211  * This method can help to debug application and see if switches occur. \n
212  * Note that it as no effect if this method is called from the non real time
213  * Framework library.
214  *
215  * \param enable enable or disable warns
216  */
217  static void WarnUponSwitches(bool enable);
218
219private:
220  /*!
221  * \brief Run method
222  *
223  * This method is automatically called by Start(). \n
224  * This method must be reimplemented, in order to implement the thread.
225  *
226  */
227  virtual void Run(void) = 0;
228
229  class Thread_impl *pimpl_;
230};
231
232} // end namespace core
233} // end namespace flair
234
235#endif // THREAD_H
Note: See TracBrowser for help on using the repository browser.