[omniORB] signals and mutex's

jnye@micro-optics.com jnye@micro-optics.com
Fri, 2 Mar 2001 08:39:47 -0400


The following code is the proper way to do what you want:

#include <semaphore.h>

static sem_t sem;


void shutDownHandler(int signal)
{
    static bool signalled = false;
    if (!signalled)
    {
        sem_post(&sem);
        signalled = true;
    }
}


int main()
{
    sem_init(&sem, 0, 0);

    ...
    boa->impl_is_ready(0, 1);

    signal(SIGINT, shutDownHandler);

    sem.wait(&sem);    // Block here until interrupted.

    server->_dispose();
    boa->impl_shutdown();
    ...

    sem_destroy(&sem);
}


Not only are you using a mutex in the context of the signal handler
incorrectly, you are also using a condition which is also not signal-safe.
The only threading construct that is signal-safe is the semaphore. The code
ends up being nicer, too ;).

Cheers,
Jason.



-----Original Message-----
From: owner-omniorb-list@uk.research.att.com
[mailto:owner-omniorb-list@uk.research.att.com]On Behalf Of Matthew
Berry
Sent: Friday, March 02, 2001 4:30 AM
To: omniORB
Subject: [omniORB] signals and mutex's


I read somewhere that a pthread mutex should not be used in a signal handler
as it may result in deadlock.

The code I have implemented is listed below. Is this a problem?

....

void shutDownHandler(int signal)
{
    static bool signalled = false;
    static omni_mutex mutex;

    omni_mutex_lock lock(mutex)

    if (!signalled)
    {
        shut_down_condition.signal();

        signalled = true;
    }
}


....


int main()
{
    ...
        boa->impl_is_ready(0, 1);        // do not block

        signal(SIGINT, shutDownHandler);

        shut_down_condition.wait();
        shut_down_condition.unlock();

        server->_dispose();
        boa->impl_shutdown();
        ...
}