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 |
|
---|
19 | class Thread_impl;
|
---|
20 |
|
---|
21 | namespace flair {
|
---|
22 | namespace core {
|
---|
23 |
|
---|
24 | class 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 | */
|
---|
38 | class Thread : public Object {
|
---|
39 | friend class ::Thread_impl;
|
---|
40 |
|
---|
41 | public:
|
---|
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 Is the thread running?
|
---|
181 | *
|
---|
182 | * \return true if thread is running (call to Start). A suspended thread is running.
|
---|
183 | *
|
---|
184 | */
|
---|
185 | bool IsRunning(void) const;
|
---|
186 |
|
---|
187 | /*!
|
---|
188 | * \brief Sleep until absolute time
|
---|
189 | *
|
---|
190 | * This method will block untill time is reached.
|
---|
191 | *
|
---|
192 | * \param time absolute time
|
---|
193 | */
|
---|
194 | void SleepUntil(Time time) const;
|
---|
195 |
|
---|
196 | /*!
|
---|
197 | * \brief Sleep for a certain time in micro second
|
---|
198 | *
|
---|
199 | * This method will block untill time is elapsed.
|
---|
200 | *
|
---|
201 | * \param time_us time to wait in micro second
|
---|
202 | */
|
---|
203 | void SleepUS(uint32_t time_us) const;
|
---|
204 |
|
---|
205 | /*!
|
---|
206 | * \brief Sleep for a cartain time in milli second
|
---|
207 | *
|
---|
208 | * This method will block untill time is elapsed.
|
---|
209 | *
|
---|
210 | * \param time_ms time to wait in milli second
|
---|
211 | */
|
---|
212 | void SleepMS(uint32_t time_ms) const;
|
---|
213 |
|
---|
214 | /*!
|
---|
215 | * \brief Warn if real time / non real time switches occur
|
---|
216 | *
|
---|
217 | * If enabled, a message with the call stack will be displayed
|
---|
218 | * in case of real time / non real time switches. \n
|
---|
219 | * This method can help to debug application and see if switches occur. \n
|
---|
220 | * Note that it as no effect if this method is called from the non real time
|
---|
221 | * Framework library.
|
---|
222 | *
|
---|
223 | * \param enable enable or disable warns
|
---|
224 | */
|
---|
225 | static void WarnUponSwitches(bool enable);
|
---|
226 |
|
---|
227 | private:
|
---|
228 | /*!
|
---|
229 | * \brief Run method
|
---|
230 | *
|
---|
231 | * This method is automatically called by Start(). \n
|
---|
232 | * This method must be reimplemented, in order to implement the thread.
|
---|
233 | *
|
---|
234 | */
|
---|
235 | virtual void Run(void) = 0;
|
---|
236 |
|
---|
237 | class Thread_impl *pimpl_;
|
---|
238 | };
|
---|
239 |
|
---|
240 | } // end namespace core
|
---|
241 | } // end namespace flair
|
---|
242 |
|
---|
243 | #endif // THREAD_H
|
---|