From 933b35311a6121b5e177c8300699b90b9ffbca8e Mon Sep 17 00:00:00 2001 From: Guobao SHEN Date: Fri, 30 Sep 2011 16:28:29 -0400 Subject: [PATCH] fix a mutex lock in methods: removeAndUnsetListOwnership and addAndSetListOwnership --- pvAccessApp/remote/channelSearchManager.cpp | 45 ++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/pvAccessApp/remote/channelSearchManager.cpp b/pvAccessApp/remote/channelSearchManager.cpp index 16bcdd0..479a5a0 100644 --- a/pvAccessApp/remote/channelSearchManager.cpp +++ b/pvAccessApp/remote/channelSearchManager.cpp @@ -30,9 +30,10 @@ void BaseSearchInstance::addAndSetListOwnership(SearchInstance::List* newOwner, { if(ownerMutex == NULL) THROW_BASE_EXCEPTION("Null owner mutex"); - _ownerMutex = ownerMutex; - Lock ownerGuard(*_ownerMutex); - Lock guard(_mutex); + Lock ownerGuard(*ownerMutex); + _ownerMutex = ownerMutex; + + Lock guard(_mutex); newOwner->push_back(this); //if (_owner == NULL) this->acquire(); // new owner _owner = newOwner; @@ -41,23 +42,29 @@ void BaseSearchInstance::addAndSetListOwnership(SearchInstance::List* newOwner, void BaseSearchInstance::removeAndUnsetListOwnership() { - if(_owner == NULL) return; + Mutex * localOm; + SearchInstance::List* localOwner; + BaseSearchInstance *_this; + { + Lock guard(_mutex); + if(_owner == NULL) return; - if(_ownerMutex == NULL) THROW_BASE_EXCEPTION("Null owner mutex"); - Lock ownerGuard(*_ownerMutex); - Lock guard(_mutex); - if(_owner != NULL) - { - //this->release(); - // TODO !!! - for (SearchInstance::List::iterator iter = _owner->begin(); iter != _owner->end(); iter++) - if (*iter == this) - { - _owner->erase(iter); - break; - } - _owner = NULL; - } + if(_ownerMutex == NULL) THROW_BASE_EXCEPTION("Null owner mutex"); + localOm=_ownerMutex; + localOwner=_owner; + _owner = NULL; + _ownerMutex = NULL; + _this=this; + } + + Lock ownerGuard(*localOm); + for (SearchInstance::List::iterator iter = localOwner->begin(); iter != localOwner->end(); iter++) { + if (*iter == _this) + { + localOwner->erase(iter); + break; + } + } } int32 BaseSearchInstance::getOwnerIndex()