LRAUV  revA
ThreadHandler Class Reference

A ThreadHandler is a Handler which runs in its own thread. More...

#include <ThreadHandler.h>

Inheritance diagram for ThreadHandler:
Collaboration diagram for ThreadHandler:

Public Member Functions

 ThreadHandler (Component &inComponent, const Str &name, bool idleThread)
 Constructor. More...
 
virtual ~ThreadHandler ()
 Destructor. More...
 
virtual void run ()
 Overrides Handler::run(). More...
 
void cleanup (void)
 A member function for the cleanup callback (called from threadCleanup) More...
 
virtual void runInThread ()
 The new replacement for Handler::run() which runs in the thread., Run from the static threadEntryPoint. More...
 
virtual bool continueLoop (void)
 Evaluates additional thread-specific continue conditions. More...
 
virtual void shutdown (void)
 Cleanup the component (typically called from outside component) More...
 
void exitThread (void)
 Called by the thread to end itself (pthread_Exit) More...
 
void cancelThread (void)
 A "soft exit", requests the thread end on next loop. More...
 
void joinThread (void)
 Join the thread. More...
 
void killThread (void)
 End the thread immediately (uses pthread_cancel) More...
 
const pthread_t & getThreadId ()
 
- Public Member Functions inherited from Handler
 Handler (Component &inComponent, const Str &name, bool idleThread=false)
 Constructor. More...
 
virtual ~Handler ()
 Destructor. More...
 
virtual void runOnce (void)
 Virtual function run only on first iteration of handler. More...
 
virtual void runBetween (void)
 Virtual function run after the first iteration of the handler. More...
 
virtual bool isRunning (void)
 True if handler is running. More...
 
const StrgetName ()
 
void setAsLogHandler (bool logHandler)
 
void setAsControlLoopHandler (bool controlLoopHandler)
 

Static Public Member Functions

static int PthreadJoinTimeout (pthread_t wid, unsigned int msecs, Component &component)
 
static void * ThreadEntryPoint (void *me)
 The entry point for the new thread. A static function. More...
 
static void ThreadCleanup (void *me)
 A static entry point for a cleanup callback. More...
 
- Static Public Member Functions inherited from Handler
static bool IsFastSimAllowed ()
 

Protected Attributes

bool cancelled_
 Keeps track of the "cancelled" signal from cancelThread. More...
 
- Protected Attributes inherited from Handler
Componentcomponent_
 The "handled" Component. More...
 
Timestamp componentStartTime_
 Keep track of when the component is supposed to start next. More...
 
Logger logger_
 
PCallerpCaller_
 Used for making protected calls. More...
 
bool running_
 

Private Attributes

pthread_t threadID_
 The thread ID. More...
 
pthread_attr_t threadAttributes_
 the thread attributes More...
 

Additional Inherited Members

- Static Protected Attributes inherited from Handler
static const Timespan SMALL_INTERVAL = Timespan::FromMicros( 800 )
 

Detailed Description

A ThreadHandler is a Handler which runs in its own thread.

This is the primary abstraction for a "thread" in this software. When wrapped around a Component, this handler spins off a separate thread and executes the component in it.

Constructor & Destructor Documentation

ThreadHandler::ThreadHandler ( Component inComponent,
const Str name,
bool  idleThread 
)

Constructor.

Parameters
inComponentComponent being handled, passed to Handler
nameName for this (fed to its logger_)
idleThreadtrue if the thread wrapper for the component should be ile priority

References threadAttributes_.

ThreadHandler::~ThreadHandler ( )
virtual

Destructor.

Desctructor.

References joinThread(), killThread(), and threadAttributes_.

Member Function Documentation

void ThreadHandler::cancelThread ( void  )

A "soft exit", requests the thread end on next loop.

End this thread.

References cancelled_, Syslog::INFO, Handler::logger_, and Logger::syslog().

Referenced by runInThread(), and shutdown().

void ThreadHandler::cleanup ( void  )

A member function for the cleanup callback (called from threadCleanup)

References Handler::component_, and Component::uninitialize().

bool ThreadHandler::continueLoop ( void  )
virtual

Evaluates additional thread-specific continue conditions.

Supplemental end conditions.

Reimplemented from Handler.

References cancelled_, and Handler::continueLoop().

void ThreadHandler::exitThread ( void  )

Called by the thread to end itself (pthread_Exit)

Exit this thread.

References threadID_.

Referenced by shutdown().

const pthread_t& ThreadHandler::getThreadId ( )
inline

References threadID_.

void ThreadHandler::joinThread ( void  )

Join the thread.

References Handler::component_, PthreadJoinTimeout(), and threadID_.

Referenced by shutdown(), and ~ThreadHandler().

void ThreadHandler::killThread ( void  )

End the thread immediately (uses pthread_cancel)

Send a kill signal to thread.

exitThread waits for the component to yield before ending the thread. killThread interrupts the thread.

References threadID_.

Referenced by ~ThreadHandler().

int ThreadHandler::PthreadJoinTimeout ( pthread_t  wid,
unsigned int  msecs,
Component component 
)
static
void ThreadHandler::run ( void  )
virtual

Overrides Handler::run().

Launch thread, run component in that thread.

Contains the actual launch of the new thread, then returns.

Reimplemented from Handler.

References threadAttributes_, ThreadEntryPoint(), and threadID_.

void ThreadHandler::runInThread ( void  )
virtual

The new replacement for Handler::run() which runs in the thread., Run from the static threadEntryPoint.

This is essentially "run", but it happens in the thread.

This in turn runs Handler::run() with some thread-specific entry and exit code.

References cancelThread(), Syslog::INFO, Handler::logger_, Handler::run(), Logger::syslog(), and ThreadCleanup().

void ThreadHandler::shutdown ( void  )
virtual

Cleanup the component (typically called from outside component)

Generic call, inherited from Handler to stop the component.

In this case, means "stop and reap the thread"

Reimplemented from Handler.

References cancelThread(), exitThread(), joinThread(), Handler::shutdown(), and threadID_.

void ThreadHandler::ThreadCleanup ( void *  me)
static

A static entry point for a cleanup callback.

Referenced by runInThread().

void * ThreadHandler::ThreadEntryPoint ( void *  me)
static

The entry point for the new thread. A static function.

This is my static thread entry point.

Referenced by run().

Member Data Documentation

bool ThreadHandler::cancelled_
protected

Keeps track of the "cancelled" signal from cancelThread.

Referenced by cancelThread(), and continueLoop().

pthread_attr_t ThreadHandler::threadAttributes_
private

the thread attributes

Referenced by run(), ThreadHandler(), and ~ThreadHandler().

pthread_t ThreadHandler::threadID_
private

The thread ID.

Referenced by exitThread(), getThreadId(), joinThread(), killThread(), run(), and shutdown().


The documentation for this class was generated from the following files: