Go to the documentation of this file.
39 #include "blocxx/BLOCXX_config.h"
58 #ifdef BLOCXX_HAVE_OPENSSL
59 #include <openssl/err.h>
93 ::memset(&zthr, 0,
sizeof(zthr));
97 static Thread_t NULLTHREAD = zeroThread();
100 sameId(
const Thread_t& t1,
const Thread_t& t2)
112 , m_cancelRequested(0)
122 if (!sameId(
m_id, NULLTHREAD))
145 "Thread::start - thread is already running");
147 if (!sameId(
m_id, NULLTHREAD))
150 "Thread::start - cannot start previously run thread");
173 Format(
"Thread::join - ThreadImpl::joinThread: %1(%2)",
174 errno, strerror(errno)).c_str());
185 Thread_t theThreadID;
191 ThreadParam* pParam =
static_cast<ThreadParam*
>(paramPtr);
192 Thread* pTheThread = pParam->thread;
194 theThreadID = pTheThread->m_id;
198 pTheThread->m_isRunning =
true;
203 rval = pTheThread->run();
209 catch (Exception& ex)
212 std::clog <<
"!!! Exception: " << ex.type() <<
" caught in Thread class\n";
213 std::clog << ex << std::endl;
217 pTheThread->doneRunning(
cb);
222 catch (std::exception& ex)
225 std::clog <<
"!!! std::exception: " << ex.what() <<
" caught in Thread class" << std::endl;
229 pTheThread->doneRunning(
cb);
237 std::clog <<
"!!! Unknown Exception caught in Thread class" << std::endl;
242 pTheThread->doneRunning(
cb);
248 pTheThread->doneRunning(
cb);
251 catch (Exception& ex)
254 std::clog <<
"!!! Exception: " << ex.type() <<
" caught in Thread class\n";
255 std::clog << ex << std::endl;
258 BLOCXX_LOG_ERROR(logger, Format(
"!!! Exception caught in Thread class: %1", ex));
262 catch (std::exception& ex)
265 std::clog <<
"!!! std::exception: " << ex.what() <<
" caught in Thread class" << std::endl;
268 BLOCXX_LOG_ERROR(logger, Format(
"!!! std::exception caught in Thread class: %1", ex.what()));
275 std::clog <<
"!!! Unknown Exception caught in Thread class" << std::endl;
299 cb->notifyThreadDone(
this);
301 #ifdef BLOCXX_HAVE_OPENSSL
333 #if !defined(BLOCXX_WIN32)
366 #if !defined(BLOCXX_WIN32)
375 catch (ThreadException&)
393 BLOCXX_LOG_ERROR(logger,
"Thread::definitiveCancel cancelling thread because it did not exit!");
419 catch (ThreadException&)
423 NonRecursiveMutex mtx;
424 NonRecursiveMutexLock l(is_locked ? mtx :
m_stateGuard);
457 TimeoutTimer tt(timeout);
NonRecursiveMutex m_stateGuard
bool sameThreads(const volatile Thread_t &handle1, const volatile Thread_t &handle2)
Check two platform dependant thread types for equality.
virtual ~Thread()
Destroy this Thread object.
BLOCXX_COMMON_API void destroyThread(Thread_t &handle)
Destroy any resources associated with a thread that was created with the createThread method.
The ThreadBarrier class is used to synchronize threads.
IntrusiveReference< ThreadDoneCallback > ThreadDoneCallbackRef
virtual void doDefinitiveCancel()
See the documentation for doCooperativeCancel().
#define BLOCXX_THROW_ERRNO_MSG1(exType, msg, errnum)
Throw an exception using FILE, LINE, errnum and strerror(errnum)
BLOCXX_COMMON_API void cancel(Thread_t threadID)
static void testCancel()
Test if this thread has been cancelled.
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
virtual void doShutdown()
This function is available for subclasses of Thread to override if they wish to be notified when shut...
bool timedWait(NonRecursiveMutexLock &lock, const Timeout &timeout)
Atomically unlock a given mutex and wait for a given amount of time for this Condition object to get ...
void cancel_internal(bool is_locked)
BLOCXX_COMMON_API void testCancel()
Test if this thread has been cancelled.
Int32 join()
Join with this Thread's execution.
#define BLOCXX_THREAD_FLG_JOINABLE
Note that descriptions of what exceptions may be thrown assumes that object is used correctly,...
BLOCXX_COMMON_API int joinThread(Thread_t &handle, Int32 &rval)
Join a thread that has been previously set to joinable.
BLOCXX_COMMON_API int createThread(Thread_t &handle, ThreadFunction func, void *funcParm, UInt32 threadFlags)
Starts a thread running the given function.
void cooperativeCancel()
Attempt to cooperatively cancel this Threads execution.
virtual void doCooperativeCancel()
This function is available for subclasses of Thread to override if they wish to be notified when a co...
static Timeout relative(float seconds)
class BLOCXX_COMMON_API Logger
static Int32 threadRunner(void *paramPtr)
LazyGlobal< String, char const *const > GlobalString
bool timedWait(const Timeout &timeout)
Wait for the thread to finish.
Atomic_t m_cancelRequested
void cancel()
Definitively cancel this Threads execution.
#define BLOCXX_LOG_ERROR(logger, message)
Log message to logger with the Error level.
#define BLOCXX_GLOBAL_STRING_INIT(str)
void doneRunning(const ThreadDoneCallbackRef &cb)
bool definitiveCancel(const Timeout &timeout=Timeout::relative(60))
Attempt to cooperatively and then definitively cancel this Thread's execution.
#define BLOCXX_LOG_DEBUG3(logger, message)
Log message to logger with the Debug3 level.
BLOCXX_COMMON_API void saveThreadInTLS(void *pTheThread)
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
BLOCXX_COMMON_API void sendSignalToThread(Thread_t threadID, int signo)
A TimeoutTimer is used by an algorithm to determine when a timeout has expired.
virtual void start(const ThreadDoneCallbackRef &cb=ThreadDoneCallbackRef(0))
Start this Thread's execution.
BLOCXX_COMMON_API void exitThread(Thread_t &handle, Int32 rval)
Exit thread method.
Thread()
Create a new Thread object.
ThreadBarrier thread_barrier
GlobalString COMPONENT_NAME
A timeout can be absolute, which means that it will happen at the specified DateTime.
void shutdown()
Call the thread's doShutdown(), which may be used by the thread to safely stop.
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
void notifyAll()
Signal all threads that are currently waiting on the Condition object.
In the event a thread has been cancelled, a ThreadCancelledException will be thrown.