CARMA C++
sza::util::SignalTask Class Reference

A generic base-class task for handling timers & signals. More...

#include <carma/szautil/SignalTask.h>

Inheritance diagram for sza::util::SignalTask:
sza::util::GenericTask< sza::util::SignalTaskMsg >

Classes

class  SignalHandler
 A class to manage signal handlers for all handled signals. More...
 

Public Member Functions

int getIoSig ()
 Query what signal this object uses for I/O events. More...
 
pthread_t getThreadId ()
 
virtual void run ()
 Run, handling signals. More...
 
void sendAddHandlerMsg (std::string name, SIGNALTASK_HANDLER_FN(*handler), bool add)
 Send a message to attach a handler to an existing timer. More...
 
void sendAddHandlerMsg (int sigNo, SIGNALTASK_HANDLER_FN(*handler), bool add)
 Send a message to attach a handler to a signal. More...
 
void sendEnableTimerMsg (std::string name, bool enable)
 Send a message to enable/disable a timer. More...
 
void sendInstallSignalMsg (int sigNo, SIGNALTASK_HANDLER_FN(*handler), void *arg=NULL)
 Send a message to install a signal. More...
 
void sendInstallTimerMsg (std::string name, int sigNo, unsigned long initSec, unsigned long initNsec, unsigned long intervalSec, unsigned long intervaNsec, SIGNALTASK_HANDLER_FN(*handler))
 Send a message to install a timer. More...
 
void serviceMsgQ ()
 Service our message queue. More...
 
 SignalTask (bool spawnThread=false)
 Constructor for SignalTask. More...
 
void stopTimers ()
 Stop all timers. More...
 
virtual ~SignalTask ()
 Making the destructor virtual ensures that the right destructor will be called for classes which inherit from SignalTask. More...
 
- Public Member Functions inherited from sza::util::GenericTask< sza::util::SignalTaskMsg >
void sendHeartBeatMsg ()
 Method to send a heartbeat message to this task via its message queue. More...
 
void sendRestartMsg ()
 Method to send a stop message to this task via its message queue. More...
 
void sendStopMsg ()
 Method to send a stop message to this task via its message queue. More...
 

Protected Attributes

sigset_t handledSignals_
 The set of signals handled by this task. More...
 
- Protected Attributes inherited from sza::util::GenericTask< sza::util::SignalTaskMsg >
sza::util::FdSet fdSet_
 A set of file descriptors associated with this task. More...
 
PipeQ< sza::util::SignalTaskMsg > msgq_
 A message queue, implemented as a pipe, by which we can communicate with this task. More...
 
Threadthread_
 If this GenericTask object was instantiated by another thread, keep a pointer to it here. More...
 
std::vector< Thread * > threads_
 A vector of Thread objects managed by this task. More...
 

Additional Inherited Members

- Protected Member Functions inherited from sza::util::GenericTask< sza::util::SignalTaskMsg >
virtual void addHandler (sza::util::SignalTaskMsg *msg)
 Respond to a message to add/remove a handler. More...
 
void cancelThread (unsigned order)
 Cancel the next thread with the specified order. More...
 
void cancelThreads ()
 A method to cancel threads managed by this task. More...
 
 GenericTask ()
 Protected constructor ensures that the base class cannot be instantiated. More...
 
 GenericTask (Thread *thread)
 Constructor which initializes thread_, below. More...
 
unsigned getMinCancelOrder ()
 Method to return the minimum cancel order for threads which are not yet running. More...
 
unsigned getMinStartOrder ()
 Method to return the minimum start order for threads which are not yet running. More...
 
ThreadgetThread (std::string name)
 A method to start all threads managed by this task running. More...
 
void pingThreads (void *arg)
 A method to ping all pingable threads managed by this task. More...
 
virtual void processMsg (sza::util::SignalTaskMsg *msg)
 This method should be defined by each inheriting task to process its own task-specific messages. More...
 
void raise (std::string name, int sigNo)
 Raise a signal to a named thread. More...
 
virtual void respondToHeartBeat ()
 Respond to a heartbeat message. More...
 
virtual void restart (void)
 Restart this thread. More...
 
void sendTaskMsg (sza::util::SignalTaskMsg *msg)
 Send a message to this task via its message queue. More...
 
void shutdownConnection (int fd)
 Shutdown a connection. More...
 
void startThread (void *arg, unsigned order)
 Start the next thread with the specified order. More...
 
void startThreads (void *arg)
 A method to start all threads. More...
 
bool threadsAreRunning ()
 A method to test if the threads managed by this task are running. More...
 
bool threadsNeedCancelling ()
 Return true if there are still uncancelled threads. More...
 
bool threadsNeedStarting ()
 Return true if there are still unstarted threads. More...
 
virtual ~GenericTask ()
 Making the destructor virtual ensures that the right destructor will be called for classes which inherit from GenericTask. More...
 

Detailed Description

A generic base-class task for handling timers & signals.

Context:

Under pthreads, there is no guarantee which thread a signal will be delivered to, so traditional use of signal handlers in multiple threads is unsafe.

The safe way of handling signals under pthreads is to block signals in all threads but one, which is dedicated to servicing all signals.

The default run() method of this class handles signals in normal code, by blocking in a loop on sigwait() until any catchable signal has arrived. On receipt of a signal, if a handler has been installed for that signal using either installTimer() or installSignal() methods below, that handler is called. Note that all signals are blocked between calls to sigwait(), so that we are guaranteed not to be interrupted by another signal while servicing one that has just arrived.

Each pass of the loop atomically unblocks a set of signals specified by a sigset_t* argument passed to sigwait(). This set is modified by calls to installTimer() and installSignal(), below.

SignalTask installs

Definition at line 55 of file SignalTask.h.

Constructor & Destructor Documentation

sza::util::SignalTask::SignalTask ( bool  spawnThread = false)

Constructor for SignalTask.

The thread id is set in this constructor to the id returned by pthread_self(). Calling our internal send*Msg() methods will therefore cause an I/O signal to be raised to the thread which instantiated this object, causing the message queue to be checked.

virtual sza::util::SignalTask::~SignalTask ( )
virtual

Making the destructor virtual ensures that the right destructor will be called for classes which inherit from SignalTask.

Member Function Documentation

int sza::util::SignalTask::getIoSig ( )

Query what signal this object uses for I/O events.

virtual void sza::util::SignalTask::run ( )
virtual

Run, handling signals.

Reimplemented from sza::util::GenericTask< sza::util::SignalTaskMsg >.

void sza::util::SignalTask::sendAddHandlerMsg ( std::string  name,
SIGNALTASK_HANDLER_FN *  handler,
bool  add 
)

Send a message to attach a handler to an existing timer.

void sza::util::SignalTask::sendAddHandlerMsg ( int  sigNo,
SIGNALTASK_HANDLER_FN *  handler,
bool  add 
)

Send a message to attach a handler to a signal.

void sza::util::SignalTask::sendEnableTimerMsg ( std::string  name,
bool  enable 
)

Send a message to enable/disable a timer.

void sza::util::SignalTask::sendInstallSignalMsg ( int  sigNo,
SIGNALTASK_HANDLER_FN *  handler,
void *  arg = NULL 
)

Send a message to install a signal.

void sza::util::SignalTask::sendInstallTimerMsg ( std::string  name,
int  sigNo,
unsigned long  initSec,
unsigned long  initNsec,
unsigned long  intervalSec,
unsigned long  intervaNsec,
SIGNALTASK_HANDLER_FN *  handler 
)

Send a message to install a timer.

void sza::util::SignalTask::serviceMsgQ ( )
virtual

Service our message queue.

Reimplemented from sza::util::GenericTask< sza::util::SignalTaskMsg >.

void sza::util::SignalTask::stopTimers ( )

Stop all timers.

Member Data Documentation

sigset_t sza::util::SignalTask::handledSignals_
protected

The set of signals handled by this task.

We make this protected so that inheritors of this class can redefine run(), which may require access to handledSignals_.

Definition at line 255 of file SignalTask.h.


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