From b3e4a47c3ace7b8daffc94c0b11f41f1d1f2f73a Mon Sep 17 00:00:00 2001 From: rachytski Date: Sat, 28 Jan 2012 01:17:10 +0400 Subject: [PATCH] more precise synchronization of Fence mechanism. --- base/fence_manager.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/base/fence_manager.cpp b/base/fence_manager.cpp index 938d959566..606e3f9542 100644 --- a/base/fence_manager.cpp +++ b/base/fence_manager.cpp @@ -47,7 +47,7 @@ int FenceManager::insertFence() void FenceManager::signalFence(int id) { - threads::MutexGuard g(m_mutex); + threads::MutexGuard mutexGuard(m_mutex); if (m_isCancelled) return; @@ -62,6 +62,7 @@ void FenceManager::signalFence(int id) threads::Condition * cond = it->second; + /// i suppose that this guard will be destroyed after mutexGuard threads::ConditionGuard fenceGuard(*cond); /// erasing fence from active fences @@ -92,11 +93,17 @@ void FenceManager::joinFence(int id) } cond = it->second; + + /// we should lock condition here, to prevent us from the situation + /// when the condition will be signaled before it's been waited for + cond->Lock(); } - threads::ConditionGuard fenceGuard(*cond); - if (m_activeFences.find(id) != m_activeFences.end()) - fenceGuard.Wait(); + /// to prevent from "spurious wakeups" + while (m_activeFences.find(id) != m_activeFences.end()) + cond->Wait(); + + cond->Unlock(); } void FenceManager::cancel()