source: flair-src/trunk/lib/FlairCore/src/Thread.h

Last change on this file was 330, checked in by Sanahuja Guillaume, 2 years ago

use less bandwidth in vprnlite

File size: 5.3 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  * \param stackSize stack size in bytes
49  */
50  Thread(const Object *parent, std::string name,
51         uint8_t priority,uint32_t stackSize=1024*128);
52
53  /*!
54  * \brief Destructor
55  *
56  * If thread is started, SafeStop() and Join() will
57  * be automatically called.
58  *
59  */
60  virtual ~Thread();
61
62  /*!
63  * \brief Start the thread
64  *
65  */
66  void Start(void);
67
68  /*!
69  * \brief Set a stop flag
70  *
71  * ToBeStopped() will return true after calling this method.
72  */
73  void SafeStop(void);
74
75  /*!
76  * \brief Set a stop flag
77  *
78  * Reimplemented Run() can poll this method to
79  * determine when to stop the thread.
80  *
81  * \return true if SafeStop() was called
82  */
83  bool ToBeStopped(void) const;
84
85  /*!
86  * \brief Join the thread
87  *
88  * This method will block untill Run() returns.
89  *
90  */
91  void Join(void);
92
93  /*!
94  * \brief Set the period in micro second
95  *
96  * After calling this method, IsPeriodSet will return true.
97  *
98  * \param period_us period in us
99  */
100  void SetPeriodUS(uint32_t period_us);
101
102  uint32_t GetPeriodUS() const;
103
104  /*!
105  * \brief Set the period in milli second
106  *
107  * After calling this method, IsPeriodSet will return true.
108  *
109  * \param period_ums period in ms
110  */
111  void SetPeriodMS(uint32_t period_ms);
112
113  uint32_t GetPeriodMS() const;
114
115  /*!
116  * \brief Returns if period was set
117  *
118  * \return true if a period was set using SetPeriodUS or SetPeriodMS
119  * false otherwise
120  */
121  bool IsPeriodSet(void);
122
123  /*!
124  * \brief Wait the period
125  *
126  * This method will block untill period is met. \n
127  * If no period was set (see SetPeriodUS, SetPeriodMS and IsPeriodSet), this
128  *method
129  * returns immediately.
130  *
131  */
132  void WaitPeriod(void) const;
133
134  /*!
135  * \brief Wait update of an IODevice
136  *
137  * This method will block untill IODevice::ProcessUpdate
138  * is called. \n
139  * This method is usefull to synchronize a thread with an IODevice.
140  *
141  * \param device IODevice to wait update from
142  * \return true if device was updated, false otherwise (timeout, etc)
143  */
144  bool WaitUpdate(const IODevice *device,Time timeout=TIME_INFINITE);
145
146  /*!
147  * \brief Suspend the thread
148  *
149  * This method will block untill Resume() is called.
150  *
151  */
152  void Suspend(void);
153
154  /*!
155  * \brief Suspend the thread with timeout
156  *
157  * This method will block until Resume() is called or the absolute date
158  *specified occurs
159  *
160  * \param date absolute date in ns
161  * \return true if thread is woken up by a call to Resume, false otherwise
162  */
163  bool SuspendUntil(Time date);
164
165  /*!
166  * \brief Resume the thread
167  *
168  * This method will unblock the call to Suspend().
169  *
170  */
171  void Resume(void);
172
173  /*!
174  * \brief Is the thread suspended?
175  *
176  * \return true if thread is suspended
177  *
178  */
179  bool IsSuspended(void) const;
180 
181  /*!
182  * \brief Is the thread running?
183  *
184  * \return true if thread is running (call to Start). A suspended thread is running.
185  *
186  */
187  bool IsRunning(void) const;
188
189  /*!
190  * \brief Sleep until absolute time
191  *
192  * This method will block untill time is reached.
193  *
194  * \param time absolute time
195  */
196  void SleepUntil(Time time) const;
197
198  /*!
199  * \brief Sleep for a certain time in micro second
200  *
201  * This method will block untill time is elapsed.
202  *
203  * \param time_us time to wait in micro second
204  */
205  void SleepUS(uint32_t time_us) const;
206
207  /*!
208  * \brief Sleep for a cartain time in milli second
209  *
210  * This method will block untill time is elapsed.
211  *
212  * \param time_ms time to wait in milli second
213  */
214  void SleepMS(uint32_t time_ms) const;
215
216  /*!
217  * \brief Warn if real time / non real time switches occur
218  *
219  * If enabled, a message with the call stack will be displayed
220  * in case of real time / non real time switches. \n
221  * This method can help to debug application and see if switches occur. \n
222  * Note that it as no effect if this method is called from the non real time
223  * Framework library.
224  *
225  * \param enable enable or disable warns
226  */
227  static void WarnUponSwitches(bool enable);
228
229private:
230  /*!
231  * \brief Run method
232  *
233  * This method is automatically called by Start(). \n
234  * This method must be reimplemented, in order to implement the thread.
235  *
236  */
237  virtual void Run(void) = 0;
238
239  class Thread_impl *pimpl_;
240};
241
242} // end namespace core
243} // end namespace flair
244
245#endif // THREAD_H
Note: See TracBrowser for help on using the repository browser.