[omniORB] omni_semaphore operation?

Stefan Seefeld seefelds@MAGELLAN.UMontreal.CA
Thu, 10 Aug 2000 20:39:57 -0400


Dietmar May wrote:
> 
> Hi all,
> 
> I must be missing something obvious (or maybe NOT so obvious).
> 
> The omni_semaphore code for 2.8.0 contains:
> 
> void
> omni_semaphore::wait(void)
> {
>     omni_mutex_lock l(m);
> 
>     while (value == 0)
>         c.wait();
> 
>     value--;
> }
> 
> void
> omni_semaphore::post(void)
> {
>     {
>         omni_mutex_lock l(m);
>         value++;
>     }
> 
>     c.signal();
> }
> 
> Now, if wait() is called in thread 1, the mutex 'm' gets locked by thread 1. Now when post() 
> is called by thread 2, it also attempts to lock 'm'. However, since 'm' is already locked by 
> thread 1, the threads would seem to deadlock, since thread 1 will not release 'm' until after 
> signal() is called by thread 2 -- but thread 2 is blocked waiting for 'm', and cannot reach 
> the call to signal().
> 
> Or so it seems at first glance.
> 
> What am I missing?

you are missing the details of a call to cond.wait(). While you wait on a conditional
variable, the associated mutex is *unlocked*. That happens by the pthread_cond_wait function,
so you can't see that in the code anywhere. The above is a simple trick to emulate real
posix semaphores in case they aren't available.

Hope that helps,	Stefan
_______________________________________________________              
              
Stefan Seefeld
Departement de Physique
Universite de Montreal
email: seefelds@magellan.umontreal.ca

_______________________________________________________

      ...ich hab' noch einen Koffer in Berlin...