39#include "blocxx/BLOCXX_config.h"
58#ifdef BLOCXX_HAVE_OPENSSL
59#include <openssl/err.h>
93 ::memset(&zthr, 0,
sizeof(zthr));
97static Thread_t NULLTHREAD = zeroThread();
100sameId(
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;
203 rval = pTheThread->
run();
212 std::clog <<
"!!! Exception: " << ex.
type() <<
" caught in Thread class\n";
213 std::clog << ex << std::endl;
215 Logger logger(COMPONENT_NAME);
222 catch (std::exception& ex)
225 std::clog <<
"!!! std::exception: " << ex.
what() <<
" caught in Thread class" << std::endl;
227 Logger logger(COMPONENT_NAME);
237 std::clog <<
"!!! Unknown Exception caught in Thread class" << std::endl;
239 Logger logger(COMPONENT_NAME);
254 std::clog <<
"!!! Exception: " << ex.
type() <<
" caught in Thread class\n";
255 std::clog << ex << std::endl;
257 Logger logger(COMPONENT_NAME);
262 catch (std::exception& ex)
265 std::clog <<
"!!! std::exception: " << ex.
what() <<
" caught in Thread class" << std::endl;
267 Logger logger(COMPONENT_NAME);
275 std::clog <<
"!!! Unknown Exception caught in Thread class" << std::endl;
277 Logger logger(COMPONENT_NAME);
299 cb->notifyThreadDone(
this);
301#ifdef BLOCXX_HAVE_OPENSSL
333#if !defined(BLOCXX_WIN32)
366#if !defined(BLOCXX_WIN32)
380 Logger logger(COMPONENT_NAME);
385 BLOCXX_LOG_DEBUG3(logger,
"Thread::definitiveCancel waiting for thread to exit.");
393 BLOCXX_LOG_ERROR(logger,
"Thread::definitiveCancel cancelling thread because it did not exit!");
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
#define BLOCXX_THROW_ERRNO_MSG1(exType, msg, errnum)
Throw an exception using FILE, LINE, errnum and strerror(errnum)
#define BLOCXX_GLOBAL_STRING_INIT(str)
#define BLOCXX_LOG_DEBUG3(logger, message)
Log message to logger with the Debug3 level.
#define BLOCXX_LOG_ERROR(logger, message)
Log message to logger with the Error level.
ThreadBarrier thread_barrier
#define BLOCXX_THREAD_FLG_JOINABLE
void notifyAll()
Signal all threads that are currently waiting on the Condition object.
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 ...
This class is the base of all exceptions thrown by BloCxx code.
virtual const char * what() const
Returns getMessage()
virtual const char * type() const
Returns a string representing the concrete type.
This class can be used to store a global variable that is lazily initialized in a thread safe manner.
Note that descriptions of what exceptions may be thrown assumes that object is used correctly,...
Note that descriptions of what exceptions may be thrown assumes that object is used correctly,...
The ThreadBarrier class is used to synchronize threads.
Descriptions of exceptions thrown assume that the object is used correctly, i.e., method precondition...
Atomic_t m_cancelRequested
static void testCancel()
Test if this thread has been cancelled.
Thread()
Create a new Thread object.
virtual void start(const ThreadDoneCallbackRef &cb=ThreadDoneCallbackRef(0))
Start this Thread's execution.
Int32 join()
Join with this Thread's execution.
virtual ~Thread()
Destroy this Thread object.
bool timedWait(const Timeout &timeout)
Wait for the thread to finish.
void cancel()
Definitively cancel this Threads execution.
NonRecursiveMutex m_stateGuard
void doneRunning(const ThreadDoneCallbackRef &cb)
virtual void doDefinitiveCancel()
See the documentation for doCooperativeCancel().
void shutdown()
Call the thread's doShutdown(), which may be used by the thread to safely stop.
virtual void doCooperativeCancel()
This function is available for subclasses of Thread to override if they wish to be notified when a co...
virtual void doShutdown()
This function is available for subclasses of Thread to override if they wish to be notified when shut...
static Int32 threadRunner(void *paramPtr)
virtual Int32 run()=0
The method that will be run when the start method is called.
void cancel_internal(bool is_locked)
bool definitiveCancel(const Timeout &timeout=Timeout::relative(60))
Attempt to cooperatively and then definitively cancel this Thread's execution.
void cooperativeCancel()
Attempt to cooperatively cancel this Threads execution.
A timeout can be absolute, which means that it will happen at the specified DateTime.
static Timeout relative(float seconds)
A TimeoutTimer is used by an algorithm to determine when a timeout has expired.
Timeout asAbsoluteTimeout() const
Converts the timer to an absolute timeout.
BLOCXX_COMMON_API void testCancel()
Test if this thread has been cancelled.
BLOCXX_COMMON_API void destroyThread(Thread_t &handle)
Destroy any resources associated with a thread that was created with the createThread method.
BLOCXX_COMMON_API int joinThread(Thread_t &handle, Int32 &rval)
Join a thread that has been previously set to joinable.
bool sameThreads(const volatile Thread_t &handle1, const volatile Thread_t &handle2)
Check two platform dependant thread types for equality.
BLOCXX_COMMON_API int createThread(Thread_t &handle, ThreadFunction func, void *funcParm, UInt32 threadFlags)
Starts a thread running the given function.
BLOCXX_COMMON_API void cancel(Thread_t threadID)
BLOCXX_COMMON_API void exitThread(Thread_t &handle, Int32 rval)
Exit thread method.
BLOCXX_COMMON_API void saveThreadInTLS(void *pTheThread)
BLOCXX_COMMON_API void sendSignalToThread(Thread_t threadID, int signo)
IntrusiveReference< ThreadDoneCallback > ThreadDoneCallbackRef
In the event a thread has been cancelled, a ThreadCancelledException will be thrown.