From: Marcus Brinkmann Date: Wed, 22 Dec 2010 16:02:33 +0000 (+0100) Subject: Avoid live lock in Windows (CE) under some situations due to unfair condition variables. X-Git-Tag: dbus-1.4.4~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93f98dc3d8ccab1e8ec3c051ebd875bf1e30d701;p=thirdparty%2Fdbus.git Avoid live lock in Windows (CE) under some situations due to unfair condition variables. --- diff --git a/dbus/dbus-sysdeps-thread-win.c b/dbus/dbus-sysdeps-thread-win.c index ab02950e8..e2972a318 100644 --- a/dbus/dbus-sysdeps-thread-win.c +++ b/dbus/dbus-sysdeps-thread-win.c @@ -219,8 +219,14 @@ _dbus_windows_condvar_wake_one (DBusCondVar *cond) EnterCriticalSection (&cond->lock); if (cond->list != NULL) - SetEvent (_dbus_list_pop_first (&cond->list)); - + { + SetEvent (_dbus_list_pop_first (&cond->list)); + /* Avoid live lock by pushing the waiter to the mutex lock + instruction, which is fair. If we don't do this, we could + acquire the condition variable again before the waiter has a + chance itself, leading to starvation. */ + Sleep (0); + } LeaveCriticalSection (&cond->lock); } @@ -231,7 +237,16 @@ _dbus_windows_condvar_wake_all (DBusCondVar *cond) while (cond->list != NULL) SetEvent (_dbus_list_pop_first (&cond->list)); - + + if (cond->list != NULL) + { + /* Avoid live lock by pushing the waiter to the mutex lock + instruction, which is fair. If we don't do this, we could + acquire the condition variable again before the waiter has a + chance itself, leading to starvation. */ + Sleep (0); + } + LeaveCriticalSection (&cond->lock); }