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 | * \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 | */
|
---|
143 | int WaitUpdate(const IODevice *device);
|
---|
144 |
|
---|
145 | /*!
|
---|
146 | * \brief Suspend the thread
|
---|
147 | *
|
---|
148 | * This method will block untill Resume() is called.
|
---|
149 | *
|
---|
150 | */
|
---|
151 | void Suspend(void);
|
---|
152 |
|
---|
153 | /*!
|
---|
154 | * \brief Suspend the thread with timeout
|
---|
155 | *
|
---|
156 | * This method will block until Resume() is called or the absolute date
|
---|
157 | *specified occurs
|
---|
158 | *
|
---|
159 | * \param date absolute date in ns
|
---|
160 | * \return true if thread is woken up by a call to Resume, false otherwise
|
---|
161 | */
|
---|
162 | bool SuspendUntil(Time date);
|
---|
163 |
|
---|
164 | /*!
|
---|
165 | * \brief Resume the thread
|
---|
166 | *
|
---|
167 | * This method will unblock the call to Suspend().
|
---|
168 | *
|
---|
169 | */
|
---|
170 | void Resume(void);
|
---|
171 |
|
---|
172 | /*!
|
---|
173 | * \brief Is the thread suspended?
|
---|
174 | *
|
---|
175 | * \return true if thread is suspended
|
---|
176 | *
|
---|
177 | */
|
---|
178 | bool IsSuspended(void) const;
|
---|
179 |
|
---|
180 | /*!
|
---|
181 | * \brief Is the thread running?
|
---|
182 | *
|
---|
183 | * \return true if thread is running (call to Start). A suspended thread is running.
|
---|
184 | *
|
---|
185 | */
|
---|
186 | bool IsRunning(void) const;
|
---|
187 |
|
---|
188 | /*!
|
---|
189 | * \brief Sleep until absolute time
|
---|
190 | *
|
---|
191 | * This method will block untill time is reached.
|
---|
192 | *
|
---|
193 | * \param time absolute time
|
---|
194 | */
|
---|
195 | void SleepUntil(Time time) const;
|
---|
196 |
|
---|
197 | /*!
|
---|
198 | * \brief Sleep for a certain time in micro second
|
---|
199 | *
|
---|
200 | * This method will block untill time is elapsed.
|
---|
201 | *
|
---|
202 | * \param time_us time to wait in micro second
|
---|
203 | */
|
---|
204 | void SleepUS(uint32_t time_us) const;
|
---|
205 |
|
---|
206 | /*!
|
---|
207 | * \brief Sleep for a cartain time in milli second
|
---|
208 | *
|
---|
209 | * This method will block untill time is elapsed.
|
---|
210 | *
|
---|
211 | * \param time_ms time to wait in milli second
|
---|
212 | */
|
---|
213 | void SleepMS(uint32_t time_ms) const;
|
---|
214 |
|
---|
215 | /*!
|
---|
216 | * \brief Warn if real time / non real time switches occur
|
---|
217 | *
|
---|
218 | * If enabled, a message with the call stack will be displayed
|
---|
219 | * in case of real time / non real time switches. \n
|
---|
220 | * This method can help to debug application and see if switches occur. \n
|
---|
221 | * Note that it as no effect if this method is called from the non real time
|
---|
222 | * Framework library.
|
---|
223 | *
|
---|
224 | * \param enable enable or disable warns
|
---|
225 | */
|
---|
226 | static void WarnUponSwitches(bool enable);
|
---|
227 |
|
---|
228 | private:
|
---|
229 | /*!
|
---|
230 | * \brief Run method
|
---|
231 | *
|
---|
232 | * This method is automatically called by Start(). \n
|
---|
233 | * This method must be reimplemented, in order to implement the thread.
|
---|
234 | *
|
---|
235 | */
|
---|
236 | virtual void Run(void) = 0;
|
---|
237 |
|
---|
238 | class Thread_impl *pimpl_;
|
---|
239 | };
|
---|
240 |
|
---|
241 | } // end namespace core
|
---|
242 | } // end namespace flair
|
---|
243 |
|
---|
244 | #endif // THREAD_H
|
---|