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 | {
|
---|
23 | namespace core
|
---|
24 | {
|
---|
25 |
|
---|
26 | class IODevice;
|
---|
27 |
|
---|
28 | /*! \class Thread
|
---|
29 | *
|
---|
30 | * \brief Abstract class for a thread
|
---|
31 | *
|
---|
32 | * To implement a thread, Run() method must be reimplemented. \n
|
---|
33 | * When Start() is called, it will automatically call Run() reimplemented method.
|
---|
34 | * A thread can be periodic, in this case WaitPeriod() will block untill period is met.
|
---|
35 | * Thread can also e synnchronized with an IODevice, using WaitUpdate() method. \n
|
---|
36 | * Thread period is by default 100ms.
|
---|
37 | */
|
---|
38 | class Thread: public Object
|
---|
39 | {
|
---|
40 | friend class ::Thread_impl;
|
---|
41 |
|
---|
42 | public:
|
---|
43 | /*!
|
---|
44 | * \brief Constructor
|
---|
45 | *
|
---|
46 | * \param parent parent
|
---|
47 | * \param name name
|
---|
48 | * \param priority priority, should be >20 (<20 is reserved for internal use)
|
---|
49 | */
|
---|
50 | Thread(const Object* parent,std::string name,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 method
|
---|
127 | * returns immediately.
|
---|
128 | *
|
---|
129 | */
|
---|
130 | void WaitPeriod(void) const;
|
---|
131 |
|
---|
132 | /*!
|
---|
133 | * \brief Wait update of an IODevice
|
---|
134 | *
|
---|
135 | * This method will block untill IODevice::ProcessUpdate
|
---|
136 | * is called. \n
|
---|
137 | * This method is usefull to synchronize a thread with an IODevice.
|
---|
138 | *
|
---|
139 | * \param device IODevice to wait update from
|
---|
140 | */
|
---|
141 | int WaitUpdate(const IODevice* device);
|
---|
142 |
|
---|
143 | /*!
|
---|
144 | * \brief Suspend the thread
|
---|
145 | *
|
---|
146 | * This method will block untill Resume() is called.
|
---|
147 | *
|
---|
148 | */
|
---|
149 | void Suspend(void);
|
---|
150 |
|
---|
151 | /*!
|
---|
152 | * \brief Suspend the thread with timeout
|
---|
153 | *
|
---|
154 | * This method will block until Resume() is called or the absolute date specified occurs
|
---|
155 | *
|
---|
156 | * \param date absolute date in ns
|
---|
157 | * \return true if thread is woken up by a call to Resume, false otherwise
|
---|
158 | */
|
---|
159 | bool SuspendUntil(Time date);
|
---|
160 |
|
---|
161 | /*!
|
---|
162 | * \brief Resume the thread
|
---|
163 | *
|
---|
164 | * This method will unblock the call to Suspend().
|
---|
165 | *
|
---|
166 | */
|
---|
167 | void Resume(void);
|
---|
168 |
|
---|
169 | /*!
|
---|
170 | * \brief Is the thread suspended?
|
---|
171 | *
|
---|
172 | * \return true if thread is suspended
|
---|
173 | *
|
---|
174 | */
|
---|
175 | bool IsSuspended(void) const;
|
---|
176 |
|
---|
177 | /*!
|
---|
178 | * \brief Sleep until absolute time
|
---|
179 | *
|
---|
180 | * This method will block untill time is reached.
|
---|
181 | *
|
---|
182 | * \param time absolute time
|
---|
183 | */
|
---|
184 | void SleepUntil(Time time) const;
|
---|
185 |
|
---|
186 | /*!
|
---|
187 | * \brief Sleep for a certain time in micro second
|
---|
188 | *
|
---|
189 | * This method will block untill time is elapsed.
|
---|
190 | *
|
---|
191 | * \param time_us time to wait in micro second
|
---|
192 | */
|
---|
193 | void SleepUS(uint32_t time_us) const;
|
---|
194 |
|
---|
195 | /*!
|
---|
196 | * \brief Sleep for a cartain time in milli second
|
---|
197 | *
|
---|
198 | * This method will block untill time is elapsed.
|
---|
199 | *
|
---|
200 | * \param time_ms time to wait in milli second
|
---|
201 | */
|
---|
202 | void SleepMS(uint32_t time_ms) const;
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | * \brief Warn if real time / non real time switches occur
|
---|
206 | *
|
---|
207 | * If enabled, a message with the call stack will be displayed
|
---|
208 | * in case of real time / non real time switches. \n
|
---|
209 | * This method can help to debug application and see if switches occur. \n
|
---|
210 | * Note that it as no effect if this method is called from the non real time
|
---|
211 | * Framework library.
|
---|
212 | *
|
---|
213 | * \param enable enable or disable warns
|
---|
214 | */
|
---|
215 | static void WarnUponSwitches(bool enable);
|
---|
216 |
|
---|
217 | private:
|
---|
218 | /*!
|
---|
219 | * \brief Run method
|
---|
220 | *
|
---|
221 | * This method is automatically called by Start(). \n
|
---|
222 | * This method must be reimplemented, in order to implement the thread.
|
---|
223 | *
|
---|
224 | */
|
---|
225 | virtual void Run(void)=0;
|
---|
226 |
|
---|
227 | class Thread_impl* pimpl_;
|
---|
228 | };
|
---|
229 |
|
---|
230 | } // end namespace core
|
---|
231 | } // end namespace flair
|
---|
232 |
|
---|
233 | #endif // THREAD_H
|
---|