Changeset 13 in flair-dev for trunk/include/FlairCore/Thread.h
- Timestamp:
- Apr 8, 2016, 3:39:24 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/FlairCore/Thread.h
r2 r13 19 19 class Thread_impl; 20 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 }; 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 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 219 private: 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 }; 229 231 230 232 } // end namespace core
Note:
See TracChangeset
for help on using the changeset viewer.