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 );
179 friend void lunchbox::abort(
bool );
180 static void _dumpAll();
192 # define LB_CHECK_THREADSAFETY
196 #define LB_TS_VAR( NAME ) \
198 struct NAME ## Struct \
201 : extMutex( false ) \
203 mutable lunchbox::ThreadID id; \
204 mutable std::string name; \
206 mutable lunchbox::ThreadID inRegion; \
210 #ifdef LB_CHECK_THREADSAFETY
211 # define LB_TS_RESET( NAME ) NAME.id = lunchbox::ThreadID();
213 # define LB_TS_THREAD( NAME ) \
215 if( NAME.id == lunchbox::ThreadID( )) \
217 NAME.id = lunchbox::Thread::getSelfThreadID(); \
218 NAME.name = lunchbox::Log::instance().getThreadName(); \
219 LBVERB << "Functions for " << #NAME \
220 << " locked from" << lunchbox::backtrace << std::endl; \
222 if( !NAME.extMutex && NAME.id != lunchbox::Thread::getSelfThreadID( )) \
224 LBERROR << "Threadsafety check for " << #NAME \
225 << " failed on object of type " \
226 << lunchbox::className( this ) << ", thread " \
227 << lunchbox::Thread::getSelfThreadID() << " (" \
228 << lunchbox::Log::instance().getThreadName() << ") != " \
229 << NAME.id << " (" << NAME.name << ")" << std::endl; \
230 LBABORT( "Non-threadsafe code called from two threads" ); \
234 # define LB_TS_NOT_THREAD( NAME ) \
236 if( !NAME.extMutex && NAME.id != lunchbox::ThreadID( )) \
238 if( NAME.id == lunchbox::Thread::getSelfThreadID( )) \
240 LBERROR << "Threadsafety check for not " << #NAME \
241 << " failed on object of type " \
242 << lunchbox::className( this ) << std::endl; \
243 LBABORT( "Code called from wrong thread" ); \
249 template<
typename T >
class ScopedThreadCheck :
public boost::noncopyable
252 explicit ScopedThreadCheck(
const T& data )
257 "Another thread already in critical region" );
265 "Another thread entered critical region" );
273 # define LB_TS_SCOPED( NAME ) \
274 lunchbox::ScopedThreadCheck< NAME ## Struct > scoped ## NAME ## Check(NAME);
277 # define LB_TS_RESET( NAME ) {}
278 # define LB_TS_THREAD( NAME ) {}
279 # define LB_TS_NOT_THREAD( NAME ) {}
280 # define LB_TS_SCOPED( NAME ) {}
284 #endif //LUNCHBOX_THREAD_H
static ThreadID getSelfThreadID()
Bind to a specific CPU core.
Defines export visibility macros for library Lunchbox.
Basic type definitions not provided by the operating system.
Affinity
Enumeration values for thread affinity.
Utility class to execute code in a separate execution thread.
An utility class to wrap OS-specific thread identifiers.
bool isStopped() const
Return if the thread is stopped.
virtual ~Thread()
Destruct the thread.
Bind to all cores of a specific socket (CPU)
void cancel()
Cancel (stop) the child thread.
Abstraction layer and common utilities for multi-threaded programming.
virtual bool start()
Start the thread.
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Pretty-print all members of the array.
bool isRunning() const
Return if the thread is running.
bool join()
Wait for the exit of the child thread.
virtual bool init()
The init function for the child thread.
virtual void exit()
Exit the child thread immediately.
Thread()
Construct a new thread.
virtual void run()=0
The entry function for the child thread.