blocxx
Functions

BLOCXX_NAMESPACE::ThreadImpl Namespace Reference

The ThreadImpl namespace represents the functionality needed by the BloCxx Thread class (Thread). More...

Functions

void sleep (UInt32 milliSeconds)
 Suspend execution of the current thread until the given number of milliSeconds have elapsed.
void sleep (const Timeout &timeout)
 Suspend execution of the current thread until the given number of seconds have elapsed.
void yield ()
 Voluntarily yield to the processor giving the next thread in the chain the opportunity to run.
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 destroyThread (Thread_t &handle)
 Destroy any resources associated with a thread that was created with the createThread method.
bool sameThreads (const volatile Thread_t &handle1, const volatile Thread_t &handle2)
 Check two platform dependant thread types for equality.
BLOCXX_COMMON_API void exitThread (Thread_t &handle, Int32 rval)
 Exit thread method.
Thread_t currentThread ()
BLOCXX_COMMON_API UInt64 thread_t_ToUInt64 (Thread_t thr)
 Convert a Thread_t to an UInt64.
BLOCXX_COMMON_API int setThreadDetached (Thread_t &handle)
 Set a thread that was previously in the joinable state to a detached state.
BLOCXX_COMMON_API int joinThread (Thread_t &handle, Int32 &rval)
 Join a thread that has been previously set to joinable.
BLOCXX_COMMON_API void testCancel ()
 Test if this thread has been cancelled.
BLOCXX_COMMON_API void saveThreadInTLS (void *pTheThread)
BLOCXX_COMMON_API void sendSignalToThread (Thread_t threadID, int signo)
BLOCXX_COMMON_API void cancel (Thread_t threadID)

Detailed Description

The ThreadImpl namespace represents the functionality needed by the BloCxx Thread class (Thread).

It also contains other misellaneous functions which are useful for synchronization and threads. It is essentially an abstraction layer over a thread implementation.


Function Documentation

BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::cancel ( Thread_t  threadID)
BLOCXX_COMMON_API int BLOCXX_NAMESPACE::ThreadImpl::createThread ( Thread_t &  handle,
ThreadFunction  func,
void *  funcParm,
UInt32  threadFlags 
)

Starts a thread running the given function.

Parameters:
handleA platform specific thread handle
funcThe function that will run within the new thread.
funcParmThe parameter to func
flagsThe flags to use when creating the thread. Currently flags can contain the following: THREAD_JOINABLE - Thread will be created in the joinable state.
Returns:
0 on success. Otherwise -1 or a system error number (e.g. errno)

Referenced by BLOCXX_NAMESPACE::Thread::start().

Thread_t BLOCXX_NAMESPACE::ThreadImpl::currentThread ( ) [inline]
BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::destroyThread ( Thread_t &  handle)

Destroy any resources associated with a thread that was created with the createThread method.

Parameters:
handleA platform specific thread handle

Referenced by BLOCXX_NAMESPACE::Thread::~Thread().

BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::exitThread ( Thread_t &  handle,
Int32  rval 
)

Exit thread method.

This method is called everytime a thread exits. When the POSIX threads are being used, pthread_exit is called. This function does not return.

Parameters:
handleThe thread handle of the calling thread.
rvalThe thread's return value. This can get picked up by joinThread.

Referenced by BLOCXX_NAMESPACE::Thread::threadRunner().

BLOCXX_COMMON_API int BLOCXX_NAMESPACE::ThreadImpl::joinThread ( Thread_t &  handle,
Int32 &  rval 
)

Join a thread that has been previously set to joinable.

It is Assumed that if the thread has already terminated, this method will still succeed and return immediately.

Parameters:
handleA handle to the thread to join with.
rvalAn out parameter of the thread's return code.
Returns:
0 on success. Otherwise -1

Referenced by BLOCXX_NAMESPACE::Thread::join().

bool BLOCXX_NAMESPACE::ThreadImpl::sameThreads ( const volatile Thread_t &  handle1,
const volatile Thread_t &  handle2 
) [inline]

Check two platform dependant thread types for equality.

Parameters:
handle1The 1st thread type for the comparison.
handle2The 2nd thread type for the comparison.
Returns:
true if the thread types are equal. Otherwise false

Definition at line 97 of file ThreadImpl.hpp.

Referenced by BLOCXX_NAMESPACE::RWLocker::ThreadComparer::operator()(), and BLOCXX_NAMESPACE::WaitpidThreadFix::shouldUseWaitpidThreadFix().

BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::saveThreadInTLS ( void *  pTheThread)
BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::sendSignalToThread ( Thread_t  threadID,
int  signo 
)
BLOCXX_COMMON_API int BLOCXX_NAMESPACE::ThreadImpl::setThreadDetached ( Thread_t &  handle)

Set a thread that was previously in the joinable state to a detached state.

This will allow the threads resources to be released upon termination without being joined. A thread that is in the detached state can no longer be joined.

Parameters:
handleThe thread to set to the detached state.
Returns:
0 on success. Otherwise -1
BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::sleep ( const Timeout &  timeout)

Suspend execution of the current thread until the given number of seconds have elapsed.

Parameters:
secondsThe number of seconds to suspend execution for.

Definition at line 95 of file ThreadImpl.cpp.

References BLOCXX_NAMESPACE::TimeoutTimer::asAbsoluteTimeout(), BLOCXX_NAMESPACE::TimeoutTimer::expired(), BLOCXX_NAMESPACE::TimeoutTimer::loop(), and BLOCXX_NAMESPACE::Condition::timedWait().

BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::sleep ( UInt32  milliSeconds)

Suspend execution of the current thread until the given number of milliSeconds have elapsed.

Parameters:
milliSecondsThe number of milliseconds to suspend execution for.

Definition at line 89 of file ThreadImpl.cpp.

References BLOCXX_NAMESPACE::Timeout::relative().

Referenced by BLOCXX_NAMESPACE::Thread::sleep(), and yield().

BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::testCancel ( )

Test if this thread has been cancelled.

If so, a ThreadCancelledException will be thrown. DO NOT catch this exception. ThreadCancelledException is not derived from anything. Do not write code like this: try { //... } catch (...) { // swallow all exceptions }

Instead do this: try { //... } catch (ThreadCancelledException&) { throw; } catch (std::exception& e) { // handle the exception } The only place ThreadCancelledException should be caught is in Thread::threadRunner(). main() shouldn't need to catch it, as the main thread of an application should never be cancelled. The main thread shouldn't need to ever call testCancel. Note that this method is staic, and it will check the the current running thread has been cacelled. Thus, you can't call it on an object that doesn't represent the current running thread and expect it to work.

Referenced by BLOCXX_NAMESPACE::ServerSocketImpl::accept(), BLOCXX_NAMESPACE::SocketBaseImpl::connect(), BLOCXX_NAMESPACE::Select::selectRWEpoll(), BLOCXX_NAMESPACE::Select::selectRWPoll(), BLOCXX_NAMESPACE::Select::selectRWSelect(), and yield().

BLOCXX_COMMON_API UInt64 BLOCXX_NAMESPACE::ThreadImpl::thread_t_ToUInt64 ( Thread_t  thr)

Convert a Thread_t to an UInt64.

This hides platform dependencies like that fact that on some platforms (e.g linux) Thread_t is integral type, and can therefor be static_cast<>ed to unsigned long, but on other platforms, like FreeBSD, Thread_t is void*, or something else, and reinterpret_cast<> (hopefully that works ...) must be used.

BLOCXX_COMMON_API void BLOCXX_NAMESPACE::ThreadImpl::yield ( )

Voluntarily yield to the processor giving the next thread in the chain the opportunity to run.

Definition at line 114 of file ThreadImpl.cpp.

References sleep(), and testCancel().

Referenced by BLOCXX_NAMESPACE::Thread::yield().