RTAB-Map 0.25.2
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UThread.h
1/*
2Copyright (c) 2010, Mathieu Labbe
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holders nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef UTHREADNODE_H
29#define UTHREADNODE_H
30
31#include "rtabmap/utilite/utilite_export.h" // DLL export/import defines
32
33#include "rtabmap/utilite/UThreadC.h"
34
94class UTILITE_EXPORT UThread : public UThreadC<void>
95{
96public:
100 enum Priority{kPLow, kPBelowNormal, kPNormal, kPAboveNormal, kPRealTime};
101
102public:
103 //return caller thread id
104 static unsigned long currentThreadId() {return (unsigned long)UThreadC<void>::Self();}
105
106public:
112 UThread(Priority priority = kPNormal);
113
120 virtual ~UThread();
121
127 void start();
128
135 void kill();
136
143 void join(bool killFirst = false);
144
149 void setPriority(Priority priority);
150
157 void setAffinity(int cpu = 0);
158
162 bool isCreating() const;
163
167 bool isRunning() const;
168
172 bool isIdle() const;
173
177 bool isKilled() const;
178
179 Handle getThreadHandle() const {return handle_;}
180 unsigned long getThreadId() const {return threadId_;}
181
182protected:
183
184private:
191 virtual void mainLoopBegin() {}
192
202 virtual void mainLoop() = 0;
203
211 virtual void mainLoopKill() {}
212
219 virtual void mainLoopEnd() {}
220
221 /*
222 * Inherited method ThreadMain() from Thread.
223 * @see Thread<void>
224 */
225 void ThreadMain();
226
227 /*
228 * Apply thread priority. This is called when starting the thread.
229 * *@todo : Support pthread
230 */
231 void applyPriority();
232
233 /*
234 * Apply cpu affinity. This is called when starting the thread.
235 * *@todo : Support Windows
236 */
237 void applyAffinity();
238
239 /*
240 * Inherited method Create() from Thread.
241 * Here we force this function to be private so the
242 * inherited class can't have access to it.
243 * @see Thread<void>
244 */
245 int Create(
246 Handle * const & H = 0,
247 const bool & CreateDetached = false,
248 const unsigned int & StackSize = 0,
249 const bool & CancelEnable = false, // UNUSED
250 const bool & CancelAsync = false // UNUSED
251 ) const;
252
253 //Methods from UThread<void> class hided
254 static int Join( Handle H )
255 { return UThreadC<void>::Join(H); }
256#ifndef ANDROID
257 static int Kill( Handle H )
258 { return UThreadC<void>::Kill(H); }
259#endif
260 static int Detach( Handle H )
261 { return UThreadC<void>::Detach(H); }
262
263private:
264 void operator=(UThread &) {}
265 UThread( const UThread &) : state_(kSIdle) {}
266
267private:
268 enum State{kSIdle, kSCreating, kSRunning, kSKilled}; /* Enum of states. */
269 State state_; /* The thread state. */
270 Priority priority_; /* The thread priority. */
271 Handle handle_; /* The thread handle. */
272 unsigned long threadId_; /* The thread id. */
273 int cpuAffinity_; /* The cpu affinity. */
274 UMutex killSafelyMutex_; /* Mutex used to protect the kill() method. */
275 UMutex runningMutex_; /* Mutex used to notify the join method when the thread has finished. */
276};
277
278#endif // UTHREADNODE_H
void start()
UThread(Priority priority=kPNormal)
bool isIdle() const
bool isRunning() const
bool isKilled() const
virtual ~UThread()
void setPriority(Priority priority)
void kill()
void setAffinity(int cpu=0)
bool isCreating() const
void join(bool killFirst=false)