vpr::ThreadNSPR Class Reference

Threads implementation using the NSPR API. More...

#include <vpr/Thread/Thread.h>

Inheritance diagram for vpr::ThreadNSPR:

Inheritance graph
[legend]
Collaboration diagram for vpr::ThreadNSPR:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ThreadNSPR (VPRThreadPriority priority=VPR_PRIORITY_NORMAL, VPRThreadScope scope=VPR_GLOBAL_THREAD, VPRThreadState state=VPR_JOINABLE_THREAD, PRUint32 stack_size=0)
 Non-spawning constructor.
 ThreadNSPR (thread_func_t func, void *arg=NULL, VPRThreadPriority priority=VPR_PRIORITY_NORMAL, VPRThreadScope scope=VPR_GLOBAL_THREAD, VPRThreadState state=VPR_JOINABLE_THREAD, PRUint32 stack_size=0)
 Spawning constructor.
 ThreadNSPR (BaseThreadFunctor *functor_ptr, VPRThreadPriority priority=VPR_PRIORITY_NORMAL, VPRThreadScope scope=VPR_GLOBAL_THREAD, VPRThreadState state=VPR_JOINABLE_THREAD, PRUint32 stack_size=0)
 Spawning constructor (functor version).
virtual ~ThreadNSPR ()
 Destructor.
virtual void setFunctor (BaseThreadFunctor *functorPtr)
 Sets the functor that this thread will execute.
virtual vpr::ReturnStatus start ()
 Creates a new thread that will execute this thread's functor.
virtual int join (void **status=NULL)
 Makes the calling thread wait for the termination of this thread.
virtual int resume ()
 Resumes the execution of a thread that was previously suspended using suspend().
virtual int suspend ()
 Suspends the execution of this thread.
virtual int getPrio (VPRThreadPriority *prio)
 Gets this thread's priority.
virtual int setPrio (VPRThreadPriority prio)
 Sets this thread's priority.
virtual int kill (int signum)
 Sends the specified signal to this thread (not necessarily SIGKILL).
virtual void kill ()
 Kills (cancels) this thread.
virtual std::ostream & outStream (std::ostream &out)
 Provides a way of printing the process ID neatly.

Static Public Member Functions

static Threadself ()
 Gets a pointer to the thread we are in.
static void yield ()
 Yields execution of the calling thread to allow a different blocked thread to execute.

Classes

struct  staticWrapper

Detailed Description

Threads implementation using the NSPR API.

This works by recieving a function in the constructor that is the function to call when the new thread is created. The function is stored internally to the class, then the class is "boot-strapped" by spawning a call to the startThread() function with in turn will call the previously set thread function.

This is typedef'd to vpr::Thread.

Definition at line 83 of file ThreadNSPR.h.


Constructor & Destructor Documentation

vpr::ThreadNSPR::ThreadNSPR ( VPRThreadPriority  priority = VPR_PRIORITY_NORMAL,
VPRThreadScope  scope = VPR_GLOBAL_THREAD,
VPRThreadState  state = VPR_JOINABLE_THREAD,
PRUint32  stack_size = 0 
)

Non-spawning constructor.

This will not start a thread.

Definition at line 60 of file ThreadNSPR.cpp.

00062    : mThread(NULL)
00063    , mUserThreadFunctor(NULL)
00064    , mDeleteFunctor(false)
00065    , mStartFunctor(NULL)
00066    , mPriority(priority)
00067    , mScope(scope)
00068    , mState(state)
00069    , mStackSize(stackSize)
00070 {
00071 }

vpr::ThreadNSPR::ThreadNSPR ( thread_func_t  func,
void *  arg = NULL,
VPRThreadPriority  priority = VPR_PRIORITY_NORMAL,
VPRThreadScope  scope = VPR_GLOBAL_THREAD,
VPRThreadState  state = VPR_JOINABLE_THREAD,
PRUint32  stack_size = 0 
)

Spawning constructor.

This will start a new thread that will execute the specified function.

Definition at line 75 of file ThreadNSPR.cpp.

References setFunctor(), and start().

00078    : mThread(NULL)
00079    , mUserThreadFunctor(NULL)
00080    , mDeleteFunctor(false)
00081    , mStartFunctor(NULL)
00082    , mPriority(priority)
00083    , mScope(scope)
00084    , mState(state)
00085    , mStackSize(stackSize)
00086 {
00087    mDeleteFunctor = true;
00088    setFunctor(new ThreadNonMemberFunctor(func, arg));
00089    start();
00090 }

vpr::ThreadNSPR::ThreadNSPR ( BaseThreadFunctor functor_ptr,
VPRThreadPriority  priority = VPR_PRIORITY_NORMAL,
VPRThreadScope  scope = VPR_GLOBAL_THREAD,
VPRThreadState  state = VPR_JOINABLE_THREAD,
PRUint32  stack_size = 0 
)

Spawning constructor (functor version).

This will start a new thread that will execute the specified functor.

Definition at line 94 of file ThreadNSPR.cpp.

References setFunctor(), and start().

00097    : mThread(NULL)
00098    , mUserThreadFunctor(NULL)
00099    , mDeleteFunctor(false)
00100    , mStartFunctor(NULL)
00101    , mPriority(priority)
00102    , mScope(scope)
00103    , mState(state)
00104    , mStackSize(stackSize)
00105 {
00106    setFunctor(functorPtr);
00107    start();
00108 }

vpr::ThreadNSPR::~ThreadNSPR (  )  [virtual]

Destructor.

Postcondition:
This thread is removed from the thread table and from the local thread hash.

Definition at line 111 of file ThreadNSPR.cpp.

References vpr::BaseThread::unregisterThread().

00112 {
00113    ThreadManager::instance()->lock();
00114    {
00115       unregisterThread();
00116    }
00117    ThreadManager::instance()->unlock();
00118 
00119    if ( NULL != mStartFunctor )
00120    {
00121       delete mStartFunctor;
00122       mStartFunctor = NULL;
00123    }
00124 
00125    if ( mDeleteFunctor )
00126    {
00127       delete mUserThreadFunctor;
00128       mUserThreadFunctor = NULL;
00129    }
00130 }


Member Function Documentation

void vpr::ThreadNSPR::setFunctor ( BaseThreadFunctor functorPtr  )  [virtual]

Sets the functor that this thread will execute.

Precondition:
The thread is not already running. The functor is valid.

Implements vpr::BaseThread.

Definition at line 132 of file ThreadNSPR.cpp.

References vpr::BaseThreadFunctor::isValid(), and vprASSERT.

Referenced by ThreadNSPR().

00133 {
00134    vprASSERT(mThread == NULL && "Thread already running");
00135    vprASSERT(functorPtr->isValid());
00136 
00137    mUserThreadFunctor = functorPtr;
00138 }

vpr::ReturnStatus vpr::ThreadNSPR::start (  )  [virtual]

Creates a new thread that will execute this thread's functor.

Precondition:
The functor to execute has been set. The thread is not already running.
Postcondition:
A thread (with any specified attributes) is created that begins executing our functor. Depending on the scheduler, it may begin execution immediately, or it may block for a short time before beginning execution.
Returns:
A vpr::ReturnStatus obj is returned to indicate the result of the thread creation.

Implements vpr::BaseThread.

Definition at line 141 of file ThreadNSPR.cpp.

References vpr::CondVarNSPR::acquire(), vpr::ReturnStatus::Fail, vpr::BaseThread::getTID(), vpr::BaseThreadFunctor::isValid(), vpr::NSPR_PrintError(), vpr::BaseThread::registerThread(), vpr::CondVarNSPR::release(), vpr::ReturnStatus::setCode(), vpr::ReturnStatus::Succeed, vprASSERT, vpr::vprThreadFunctorFunction(), and vpr::CondVarNSPR::wait().

Referenced by ThreadNSPR().

00142 {
00143    vpr::ReturnStatus status;
00144 
00145    if ( NULL != mThread )
00146    {
00147       vprASSERT(false && "Thread already running");
00148       status.setCode(vpr::ReturnStatus::Fail);
00149    }
00150    else if ( NULL == mUserThreadFunctor )
00151    {
00152       vprASSERT(false && "No functor set");
00153       status.setCode(vpr::ReturnStatus::Fail);
00154    }
00155    else
00156    {
00157       PRThreadPriority nspr_prio;
00158       PRThreadScope nspr_scope;
00159       PRThreadState nspr_state;
00160 
00161       nspr_prio  = vprThreadPriorityToNSPR(mPriority);
00162       nspr_scope = vprThreadScopeToNSPR(mScope);
00163       nspr_state = vprThreadStateToNSPR(mState);
00164 
00165       vprASSERT(mUserThreadFunctor->isValid());
00166 
00167       // Store the member functor and create the functor for spawning to our
00168       // start routine.
00169       mStartFunctor = 
00170          new ThreadMemberFunctor<ThreadNSPR>(this, &ThreadNSPR::startThread,
00171                                              NULL);
00172 
00173       // Finally create the thread.
00174       // - On success --> The start method registers the actual thread info
00175       mThreadStartCompleted = false;      // Initialize registration flag (uses cond var for this)
00176       PRThread* ret_thread =
00177          PR_CreateThread(PR_USER_THREAD, vprThreadFunctorFunction,
00178                          (void*) mStartFunctor, nspr_prio, nspr_scope,
00179                          nspr_state, (PRUint32) mStackSize);
00180 
00181       // Inform the caller if the thread was not created successfully.
00182       if ( NULL == ret_thread )
00183       {
00184          ThreadManager::instance()->lock();
00185          {
00186             registerThread(false);
00187          }
00188          ThreadManager::instance()->unlock();
00189 
00190          NSPR_PrintError("vpr::ThreadNSPR::spawn() - Cannot create thread");
00191          status.setCode(vpr::ReturnStatus::Fail);
00192       }
00193       else
00194       {
00195          // start thread will register the thread, so let's wait for it
00196          // -- Wait for registration to complete
00197          mThreadStartCondVar.acquire();
00198          {
00199             // While not desired state (ie. register completed)
00200             while ( !mThreadStartCompleted )
00201             {
00202                mThreadStartCondVar.wait();
00203             }
00204          }
00205          mThreadStartCondVar.release();
00206          // ASSERT: Thread has completed registration
00207          vprASSERT(NULL != mThread && "Thread registration failed");
00208          vprASSERT(-1 != getTID() && "Thread id is invalid for successful thread");
00209 
00210          // Set the return code to success
00211          status.setCode(vpr::ReturnStatus::Succeed);
00212       }
00213    }
00214 
00215    return status;
00216 }

int vpr::ThreadNSPR::join ( void **  status = NULL  )  [virtual]

Makes the calling thread wait for the termination of this thread.

Precondition:
None.
Postcondition:
The caller blocks until this thread finishes its execution (i.e., finishes its root function). This routine may return immediately if this thread has already exited.
Returns:
0 is returned upon successful completion. -1 is returned if an error occurred.

Reimplemented from vpr::BaseThread.

Definition at line 218 of file ThreadNSPR.cpp.

00219 {
00220    boost::ignore_unused_variable_warning(status);
00221    return PR_JoinThread(mThread);
00222 }

virtual int vpr::ThreadNSPR::resume (  )  [inline, virtual]

Resumes the execution of a thread that was previously suspended using suspend().

Precondition:
This thread was previously suspended using the suspend() member function.
Postcondition:
This thread is sent the SIGCONT signal and is allowed to begin executing again.
Returns:
0 is returned upon successful completion. -1 is returned if an error occurred.

Reimplemented from vpr::BaseThread.

Definition at line 167 of file ThreadNSPR.h.

00168    {
00169 //      return kill(SIGCONT);
00170       return -1;
00171    }

virtual int vpr::ThreadNSPR::suspend ( void   )  [inline, virtual]

Suspends the execution of this thread.

Postcondition:
This thread is sent the SIGSTOP signal and is thus suspended from execution until the member function resume() is called.
Returns:
0 is returned upon successful completion. -1 is returned if an error occurred.

Reimplemented from vpr::BaseThread.

Definition at line 182 of file ThreadNSPR.h.

00183    {
00184 //      return kill(SIGSTOP);
00185       return -1;
00186    }

virtual int vpr::ThreadNSPR::getPrio ( VPRThreadPriority prio  )  [inline, virtual]

Gets this thread's priority.

Precondition:
This is a valid thread.
Postcondition:
The priority of this thread is returned in the integer pointer variable.
Parameters:
prio Pointer to an int variable that will have the thread's priority stored in it.
Returns:
0 is returned upon successful completion. -1 is returned if an error occurred.

Definition at line 201 of file ThreadNSPR.h.

00202    {
00203       *prio = nsprThreadPriorityToVPR(PR_GetThreadPriority(mThread));
00204 
00205       return 0;
00206    }

int vpr::ThreadNSPR::setPrio ( VPRThreadPriority  prio  )  [virtual]

Sets this thread's priority.

Postcondition:
This thread has its priority set to the specified value.
Parameters:
prio The new priority for this thread.
Returns:
0 is returned upon successful completion. -1 is returned if an error occurred.
Note:
The priority must correspond to a value in the PRThreadPriority enumerated type.

Definition at line 288 of file ThreadNSPR.cpp.

00289 {
00290    int retval(0);
00291 
00292    if ( prio > 3 )
00293    {
00294       retval = -1;
00295    }
00296    else
00297    {
00298       PR_SetThreadPriority(mThread, vprThreadPriorityToNSPR(prio));
00299    }
00300 
00301    return retval;
00302 }

virtual int vpr::ThreadNSPR::kill ( int  signum  )  [inline, virtual]

Sends the specified signal to this thread (not necessarily SIGKILL).

Postcondition:
This thread receives the specified signal.
Parameters:
signum The signal to send to the specified thread.
Returns:
0 is returned upon successful completion. -1 is returned if an error occurred.

Reimplemented from vpr::BaseThread.

Definition at line 233 of file ThreadNSPR.h.

00234    {
00235       boost::ignore_unused_variable_warning(signum);
00236       return -1;
00237    }

virtual void vpr::ThreadNSPR::kill (  )  [inline, virtual]

Kills (cancels) this thread.

Postcondition:
This thread is cancelled. Depending on the cancellation attributes of the specified thread, it may terminate immediately, it may wait until a pre-defined cancel point to stop or it may ignore the cancel altogether. Thus, immediate cancellation is not guaranteed.
Note:
For the sake of clarity, it is probably better to use the cancel() routine instead of kill() because a two-argument version of kill() is also used for sending signals to threads. This kill() and cancel() do exactly the same thing.

Reimplemented from vpr::BaseThread.

Definition at line 253 of file ThreadNSPR.h.

00254    {
00255    }

Thread * vpr::ThreadNSPR::self (  )  [static]

Gets a pointer to the thread we are in.

Returns:
NULL is returned if this thread is not in global table.

A non-NULL value is the pointer to the thread that we are running within.

Definition at line 224 of file ThreadNSPR.cpp.

References vpr::ThreadKeyNSPR::getspecific(), and vprASSERT.

00225 {
00226    vprASSERT((statics.mStaticsInitialized==1221) && "Trying to call vpr::ThreadNSPR::self before statics are initialized. Don't do that");
00227 
00228    Thread* my_thread;
00229    threadIdKey().getspecific((void**)&my_thread);
00230 
00231    return my_thread;
00232 }

static void vpr::ThreadNSPR::yield (  )  [inline, static]

Yields execution of the calling thread to allow a different blocked thread to execute.

Postcondition:
The caller yields its execution control to another thread or process.

Definition at line 273 of file ThreadNSPR.h.

00274    {
00275       PR_Sleep(PR_INTERVAL_NO_WAIT);
00276    }

virtual std::ostream& vpr::ThreadNSPR::outStream ( std::ostream &  out  )  [virtual]

Provides a way of printing the process ID neatly.

Reimplemented from vpr::BaseThread.


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:55:30 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1