Hang in pthread_cond_wait

Sebastien Decugis <[email protected]> Thu, 29 Apr 2004 14:36:44 +0200
Newsgroups gmane.comp.lib.phil
Message-ID <[email protected]>
Hi,

I think the futex_requeue feature used in pthread_cond_broadcast can
lead to a hang. Please consider the following sequence:

Thread A:
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
/* please note that this use of pthread_cond_broadcast is legal
according to POSIX */


Thread B and C:
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);


------------------------
  C: locks the mutex
  C: enters cond_wait
  C: locks cond->__lock
  C: releases the mutex
  C: cond->total_seq =3D 1
  C: val=3Dseq=3D0
  C: unlocks cond->__lock
  C: futex_wait (@=3Dcond->wake_up)
A  : locks the mutex
A  : unlocks the mutex
A  : enters pthread_cond_broadcast
A  : locks cond->__lock
A  : cond->wake_up=3Dcond_total_seq ( =3D=3D 1)
A  : unlocks cond->__lock
 B : locks the mutex
 B : enters cond_wait
 B : locks cond->__lock
 B : releases the mutex
 B : cond->total_seq =3D 2
 B : val=3Dseq=3D1
 B : unlocks cond->__lock
 B : futex_wait (@=3Dcond->wake_up)
A  : futex_requeue =3D> thread B is awaken, thread C is requeued on mutex=
.
A  : will try to lock the mutex on next loop
 B : locks cond->__lock
 B : as seq =3D=3D cond->wake_up, we loop inside the function
 B : unlocks cond->__lock
 B : futex_wait (@=3Dcond->wake_up)

Both 3 threads are now waiting.

The only workaround I can think of is to remove the FUTEX_REQUEUE call
from the broadcast function and always do a FUTEX_WAKE (ALL) instead.
This might be bad for performances but will avoid such hangs.

Please let me know if this sequence is incorrect (and why), as I don't
know the internals of futexes.

Thanks,

Sebastien.

--=20
S=E9bastien DECUGIS
Bull S.A.
NPTL Tests & Trace project
http://nptl.bullopensource.org/phpBB/


--=20
Phil-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/phil-list