20 #ifndef LUNCHBOX_THREAD_H
21 #define LUNCHBOX_THREAD_H
24 #include <lunchbox/debug.h>
25 #include <lunchbox/threadID.h>
28 #include <boost/noncopyable.hpp>
33 namespace detail {
class Thread; }
70 LUNCHBOX_API
virtual bool start();
83 virtual bool init(){
return true; }
93 virtual void run() = 0;
103 LUNCHBOX_API
virtual void exit();
111 LUNCHBOX_API
void cancel();
119 LUNCHBOX_API
bool join();
154 LUNCHBOX_API
static void yield();
157 LUNCHBOX_API
static void setName(
const std::string& name );
169 LUNCHBOX_API
static void setAffinity(
const int32_t affinity );
172 detail::Thread*
const _impl;
176 static void* runChild(
void* arg );
189 # define LB_CHECK_THREADSAFETY
193 #define LB_TS_VAR( NAME ) \
195 struct NAME ## Struct \
198 : extMutex( false ) \
200 mutable lunchbox::ThreadID id; \
201 mutable std::string name; \
203 mutable lunchbox::ThreadID inRegion; \
207 #ifdef LB_CHECK_THREADSAFETY
208 # define LB_TS_RESET( NAME ) NAME.id = lunchbox::ThreadID();
210 # define LB_TS_THREAD( NAME ) \
212 if( NAME.id == lunchbox::ThreadID( )) \
214 NAME.id = lunchbox::Thread::getSelfThreadID(); \
215 NAME.name = lunchbox::Log::instance().getThreadName(); \
216 LBVERB << "Functions for " << #NAME \
217 << " locked from" << lunchbox::backtrace << std::endl; \
219 if( !NAME.extMutex && NAME.id != lunchbox::Thread::getSelfThreadID( )) \
221 LBERROR << "Threadsafety check for " << #NAME \
222 << " failed on object of type " \
223 << lunchbox::className( this ) << ", thread " \
224 << lunchbox::Thread::getSelfThreadID() << " (" \
225 << lunchbox::Log::instance().getThreadName() << ") != " \
226 << NAME.id << " (" << NAME.name << ")" << std::endl; \
227 LBABORT( "Non-threadsafe code called from two threads" ); \
231 # define LB_TS_NOT_THREAD( NAME ) \
233 if( !NAME.extMutex && NAME.id != lunchbox::ThreadID( )) \
235 if( NAME.id == lunchbox::Thread::getSelfThreadID( )) \
237 LBERROR << "Threadsafety check for not " << #NAME \
238 << " failed on object of type " \
239 << lunchbox::className( this ) << std::endl; \
240 LBABORT( "Code called from wrong thread" ); \
246 template<
typename T >
class ScopedThreadCheck :
public boost::noncopyable
249 explicit ScopedThreadCheck(
const T& data )
254 "Another thread already in critical region" );
262 "Another thread entered critical region" );
270 # define LB_TS_SCOPED( NAME ) \
271 lunchbox::ScopedThreadCheck< NAME ## Struct > scoped ## NAME ## Check(NAME);
274 # define LB_TS_RESET( NAME ) {}
275 # define LB_TS_THREAD( NAME ) {}
276 # define LB_TS_NOT_THREAD( NAME ) {}
277 # define LB_TS_SCOPED( NAME ) {}
281 #endif //LUNCHBOX_THREAD_H
Bind to a specific CPU core.
Defines export visibility macros for Lunchbox.
static LUNCHBOX_API ThreadID getSelfThreadID()
Basic type definitions not provided by the operating system.
LUNCHBOX_API Thread()
Construct a new thread.
Affinity
Enumeration values for thread affinity.
Utility class to execute code in a separate execution thread.
virtual LUNCHBOX_API bool start()
Start the thread.
virtual LUNCHBOX_API ~Thread()
Destruct the thread.
An utility class to wrap OS-specific thread identifiers.
Bind to all cores of a specific socket (CPU)
LUNCHBOX_API void cancel()
Cancel (stop) the child thread.
LUNCHBOX_API bool isStopped() const
Return if the thread is stopped.
LUNCHBOX_API bool isRunning() const
Return if the thread is running.
virtual LUNCHBOX_API void exit()
Exit the child thread immediately.
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Pretty-print all members of the array.
virtual bool init()
The init function for the child thread.
LUNCHBOX_API bool isCurrent() const
virtual void run()=0
The entry function for the child thread.
LUNCHBOX_API bool join()
Wait for the exit of the child thread.