Avoid live lock in Windows (CE) under some situations due to unfair condition variables.
authorMarcus Brinkmann <marcus.brinkmann@ruhr-uni-bochum.de>
Wed, 22 Dec 2010 16:02:33 +0000 (17:02 +0100)
committerRalf Habacker <ralf.habacker@freenet.de>
Wed, 29 Dec 2010 21:44:27 +0000 (22:44 +0100)
dbus/dbus-sysdeps-thread-win.c

index ab02950..e2972a3 100644 (file)
@@ -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);
 }