[Stk] Deadlock in RtApiAlsa?

Carlos Pita carlosjosepita@gmail.com
Sun, 24 Sep 2006 14:11:14 -0300


Hi all!

I'm not completely sure but I think there is a bug that causes a
deadlock in RtApiAlsa class from stk-2.4.1. Below is a very schematic
pseudocode showing the problem. When the app decides to cancel the
callback it calls cancelStreamCallback, which locks the stream mutex, unsets
usingCallback and waits for finalization of the callback
thread, which is continuously calling the callback (via tickStream)
inside a loop supposed to abort as soon as usingCallback becomes false.
But the fact is that tickStream could lock the same mutex itself thus
creating a deadlock, as in that case the main thread would be waiting
for the callback thread to finish, which in turn would be waiting for
the release of the mutex previously locked by the main thread.

MainThread
----------

cancelStreamCallback() {
   MUTEX_LOCK(M)
   usingCallback = False
   pthread_join(CallbackThread)
   MUTEX_UNLOCK(M)
}

CallbackThread
--------------

while (usingCallback) tickStream();

tickStream() {
   callback();
   ....
   MUTEX_LOCK(M)
   ....
}

Best regards,
Carlos