Signaler::Intr()

Coordinate execution of all functions registered by Signaler::OnIntr().

Virtual?

No

 

Class

Signaler

 

Arguments

None

 

Returns

void

 

Notes

Intr() is the Signaler class’s main handler for interrupt signals.

Most Helix Server client applications do not need to call Intr() directly, because it is called directly from the internal handler function that catches the interrupt signals.

This internal handler function also causes the process to exit, returning an exit status of -1 to the operating system. (For instance, signaler.Intr(); exit( -1 ))

If you require more flexible or complex interrupt handling, replace the default interrupt handler function with your own by using the ANSI C signal(2) function, and call Intr() to execute the registered functions.

Caveat

Intr() does not deregister functions after they have been called. When calling a registered function twice might cause a failure, immediately deregister it using DeleteOnIntr() after the function has been called.

See also

Signaler::OnIntr()

Example

#include <unistd.h>    // for sleep()
#include <signal.h>
#include <stdhdrs.h>
#include <strbuf.h>
#include <signaler.h>

class MyClass
{
    public:
    void         Set( StrPtr *d ) { data = *d; }
    const StrPtr *Get()           { return &data; }
    void         Identify()       { printf( "I'm %s\n", data.Text() ); }

    private:
    StrBuf       data;
};

static int intrCount = 0;
static const int maxIntr = 3;

// Replacement handler for SIGINT signals. Overrides Signaler class's
// default handler to avoid immediate exit.

static void trap_interrupt( int sig )
{
    intrCount++;
    printf( "Received SIGINT. Calling registered functions...\n" );
    signaler.Intr();
    printf( "All functions done\n\n" );
    if ( intrCount >= maxIntr )
    {
        printf( "Interrupt limit hit. Exiting...\n" );
        exit( 0 );
    }
}

static void InterruptHandler( void *p )
{
    MyClass      *m = ( MyClass * )p;
    m->Identify();

    // Don't identify this object again
    signaler.DeleteOnIntr( p );
}

int main( int argc, char **argv )
{
    signal( SIGINT, trap_interrupt );
    signaler.Catch();
    int objCount = 5;
    int nextId = 1;
    for ( ; ; )
    {
        int i;
        for ( i = nextId; i < nextId + objCount; i++ )
        {
            StrBuf data;

            data.Set( "Object" );
            data << i;

            MyClass *p = new MyClass;
            p->Set( &data );

            printf( "Registering %s\n", data.Text() );
            signaler.OnIntr( InterruptHandler, ( void * )p );
        }

        nextId = i;
        printf( "\n" );
        printf( "Hit ^C to fire the interrupt handler [%d to go]\n",
                maxIntr - intrCount );
        sleep( 10 );
    }

    exit( 0 );
}