Main Page   Class Hierarchy   Alphabetical List   Compound List   Compound Members  

util::SignalHandler Class Reference

#include <SignalHandler.h>

Inheritance diagram for util::SignalHandler:

util::Object List of all members.

Detailed Description

SignalHandler is a class for easily assigning handlers for different types of signals.

SignalHandler works by creating a thread that waits for the signals for which a handler has been set. The signals that SignalHandler is waiting for must be blocked by all other threads. This is done automatically when adding new handlers. When a signal is caught, the corresponding handler is invoked.

Example

 class MyHandler : public SignalHandler
 {
 public:
   void handleSignal(int signal)
   {
     switch (signal)
       {
       case SIGINT: break;
       case SIGUSR1: break;
       }
   }
 };

 ...

 MyHandler myHandler;
 SignalHandler::addHandler(SIGINT, &myHandler);  //myHandler handles SIGINT
 SignalHandler::addHandler(SIGUSR1, &myHandler); //... and SIGUSR1
 SignalHandler::start();
 

It is advisable that you create the needed signal handlers before any threads are created in you process to ensure the signal masks are set correctly.


Public Methods

 SignalHandler ()
virtual ~SignalHandler ()
virtual void handleSignal (int signal)=0
 This function is invoked by the SignalHandler when a signal is caught on which a handler is registered.


Static Public Methods

void setHandler (int signal, SignalHandler *handler) throw (InvalidArgumentException&)
 Set a handler for signal.

void removeHandler (int signal) throw (InvalidArgumentException&)
 Remove a signal handler.

void blockSignals ()
 Block all signals from the calling process.

void unblockSignals ()
 Unblock all signals from the calling process.

void blockSignal (int sig) throw (InvalidArgumentException&)
 Block a signal from the calling process.

void unblockSignal (int sig) throw (InvalidArgumentException&)
 Unblock a signal from the calling process.

void start ()
 Start the signal handling.

void stop ()
 Halt the signal handler.


Friends

class HandlerThread


Member Function Documentation

void util::SignalHandler::blockSignal int    sig throw (InvalidArgumentException&) [static]
 

Block a signal from the calling process.

The calling process and all threads created by it after this call will not see the specified signal.

Exceptions:
InvalidArgumentException  & if sig is not a valid signal number

void util::SignalHandler::blockSignals   [static]
 

Block all signals from the calling process.

The calling process and all threads created by it after this call will not see any signals.

virtual void util::SignalHandler::handleSignal int    signal [pure virtual]
 

This function is invoked by the SignalHandler when a signal is caught on which a handler is registered.

Parameters:
signal  the number of the signal that was caught

void util::SignalHandler::removeHandler int    signal throw (InvalidArgumentException&) [static]
 

Remove a signal handler.

If a handler is removed after the start() method has been called, the signal is still delivered to this SignalHandler. In this case, SignalHandler resends the signal to the owner process.

Exceptions:
InvalidArgumentException  & if signal is not a valid signal number

void util::SignalHandler::setHandler int    signal,
SignalHandler *    handler
throw (InvalidArgumentException&) [static]
 

Set a handler for signal.

The signal numbers are specified in signal.h (see the Unix manual). Typical examples include SIGINT (Ctrl-C), SIGUSR1 (user signal 1) and the like. If a handler is added after the start() method has been called, the newly added signal will be caught only after some other, previously added signal has been caught. The handler of a signal can, however, be changed with no problems.

Exceptions:
InvalidArgumentException  & if signal is not a valid signal number

void util::SignalHandler::start   [static]
 

Start the signal handling.

This method starts a thread that waits for the signals specified by setHandler() calls. Only one such thread can be active, and the signals must be blocked by other threads.

void util::SignalHandler::stop   [static]
 

Halt the signal handler.

Actually, the signal handler exits only after one of the signals it has been waiting for is issued. Thus, calling stop() and start() again is not a good idea, unless you explicitly send a signal that causes the handler to exit.

void util::SignalHandler::unblockSignal int    sig throw (InvalidArgumentException&) [static]
 

Unblock a signal from the calling process.

The calling process and all threads created by it after this call will see the specified signal in addition to other unblocked signals.

Exceptions:
InvalidArgumentException  & if sig is not a valid signal number

void util::SignalHandler::unblockSignals   [static]
 

Unblock all signals from the calling process.

The calling process and all threads created by it after this call will see all signals.


The documentation for this class was generated from the following files:
Documentation generated on 11.09.2003 with Doxygen.
The documentation is copyrighted material.
Copyright © Topi Mäenpää 2003. All rights reserved.