[PATCH] Avoid live lock in Windows (CE) under some situations due to unfair condition variables.

Marcus Brinkmann marcus.brinkmann at ruhr-uni-bochum.de
Wed Dec 22 08:02:33 PST 2010


---
 dbus/dbus-sysdeps-thread-win.c |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/dbus/dbus-sysdeps-thread-win.c b/dbus/dbus-sysdeps-thread-win.c
index ab02950..e2972a3 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);
 }
 
-- 
1.7.1


--------------070207080108060701090909--


More information about the dbus mailing list