[omniORB] signals and mutex's

Matthew Berry mberry@mweb.co.za
Fri, 02 Mar 2001 16:00:36 +0200


Jason,

Thanks for the help.

What happens (in the very unlikely scenario) if a context switch takes place
just after sem_post but before setting signalled = true. This could result
in another thread calling sem_post a second time. I presume that since we
are using a semaphore this actually has no side-effect. In this case, we
could remove the signalled flag.

BTW, could I use (/ should I not be using) the omni_semaphore abstraction?



> -----Original Message-----
> From: jnye@micro-optics.com [mailto:jnye@micro-optics.com]
> Sent: 02 March 2001 14:40
> To: mberry@mweb.co.za; omniorb-list@uk.research.att.com
> Subject: RE: [omniORB] signals and mutex's
>
>
> 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();
>         ...
> }
>