Use epicsGuard and epicsGuardRelease

These objects provide exception-safe locking and unlocking.
This commit is contained in:
Andrew Johnson
2016-01-22 12:05:23 -06:00
parent 61c41ceb7e
commit 6c08a05b81
4 changed files with 202 additions and 326 deletions

View File

@ -8,6 +8,9 @@
* @author mrk
* @date 2012.11.21
*/
#include <epicsGuard.h>
#define epicsExportSharedSymbols
#include <pv/pvDatabase.h>
@ -40,25 +43,18 @@ PVDatabase::~PVDatabase()
void PVDatabase::destroy()
{
lock();
try {
if(isDestroyed) {
unlock();
return;
}
isDestroyed = true;
PVRecordMap::iterator iter;
while(true) {
iter = recordMap.begin();
if(iter==recordMap.end()) break;
PVRecordPtr pvRecord = (*iter).second;
recordMap.erase(iter);
if(pvRecord.get()!=NULL) pvRecord->destroy();
}
unlock();
} catch (...) {
unlock();
throw;
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return;
}
isDestroyed = true;
PVRecordMap::iterator iter;
while(true) {
iter = recordMap.begin();
if(iter==recordMap.end()) break;
PVRecordPtr pvRecord = (*iter).second;
recordMap.erase(iter);
if(pvRecord.get()!=NULL) pvRecord->destroy();
}
}
@ -72,101 +68,70 @@ void PVDatabase::unlock() {
PVRecordPtr PVDatabase::findRecord(string const& recordName)
{
lock();
try {
PVRecordPtr xxx;
if(isDestroyed) {
unlock();
return xxx;
}
PVRecordMap::iterator iter = recordMap.find(recordName);
if(iter!=recordMap.end()) {
unlock();
return (*iter).second;
}
unlock();
epicsGuard<epics::pvData::Mutex> guard(mutex);
PVRecordPtr xxx;
if(isDestroyed) {
return xxx;
} catch(...) {
unlock();
throw;
}
PVRecordMap::iterator iter = recordMap.find(recordName);
if(iter!=recordMap.end()) {
return (*iter).second;
}
return xxx;
}
PVStringArrayPtr PVDatabase::getRecordNames()
{
lock();
try {
PVStringArrayPtr xxx;
if(isDestroyed) {
unlock();
return xxx;
}
PVStringArrayPtr pvStringArray = static_pointer_cast<PVStringArray>
(getPVDataCreate()->createPVScalarArray(pvString));
size_t len = recordMap.size();
shared_vector<string> names(len);
PVRecordMap::iterator iter;
size_t i = 0;
for(iter = recordMap.begin(); iter!=recordMap.end(); ++iter) {
names[i++] = (*iter).first;
}
shared_vector<const string> temp(freeze(names));
pvStringArray->replace(temp);
unlock();
return pvStringArray;
} catch(...) {
unlock();
throw;
epicsGuard<epics::pvData::Mutex> guard(mutex);
PVStringArrayPtr xxx;
if(isDestroyed) {
return xxx;
}
PVStringArrayPtr pvStringArray = static_pointer_cast<PVStringArray>
(getPVDataCreate()->createPVScalarArray(pvString));
size_t len = recordMap.size();
shared_vector<string> names(len);
PVRecordMap::iterator iter;
size_t i = 0;
for(iter = recordMap.begin(); iter!=recordMap.end(); ++iter) {
names[i++] = (*iter).first;
}
shared_vector<const string> temp(freeze(names));
pvStringArray->replace(temp);
return pvStringArray;
}
bool PVDatabase::addRecord(PVRecordPtr const & record)
{
lock();
try {
if(isDestroyed) {
unlock();
return false;
}
string recordName = record->getRecordName();
PVRecordMap::iterator iter = recordMap.find(recordName);
if(iter!=recordMap.end()) {
unlock();
return false;
}
record->start();
recordMap.insert(PVRecordMap::value_type(recordName,record));
unlock();
return true;
} catch(...) {
unlock();
throw;
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return false;
}
string recordName = record->getRecordName();
PVRecordMap::iterator iter = recordMap.find(recordName);
if(iter!=recordMap.end()) {
return false;
}
record->start();
recordMap.insert(PVRecordMap::value_type(recordName,record));
return true;
}
bool PVDatabase::removeRecord(PVRecordPtr const & record)
{
lock();
try {
if(isDestroyed) {
unlock();
return false;
}
string recordName = record->getRecordName();
PVRecordMap::iterator iter = recordMap.find(recordName);
if(iter!=recordMap.end()) {
PVRecordPtr pvRecord = (*iter).second;
recordMap.erase(iter);
if(pvRecord.get()!=NULL) pvRecord->destroy();
unlock();
return true;
}
unlock();
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return false;
} catch(...) {
unlock();
throw;
}
string recordName = record->getRecordName();
PVRecordMap::iterator iter = recordMap.find(recordName);
if(iter!=recordMap.end()) {
PVRecordPtr pvRecord = (*iter).second;
recordMap.erase(iter);
if(pvRecord.get()!=NULL) pvRecord->destroy();
return true;
}
return false;
}
}}

View File

@ -8,6 +8,7 @@
* @author mrk
* @date 2012.11.21
*/
#include <epicsGuard.h>
#include <epicsThread.h>
#define epicsExportSharedSymbols
@ -68,48 +69,38 @@ void PVRecord::destroy()
if(traceLevel>1) {
cout << "PVRecord::destroy() " << recordName << endl;
}
lock();
try {
if(isDestroyed) {
unlock();
return;
}
isDestroyed = true;
pvTimeStamp.detach();
std::list<PVRecordClientWPtr>::iterator clientIter;
while(true) {
clientIter = pvRecordClientList.begin();
if(clientIter==pvRecordClientList.end()) break;
PVRecordClientPtr client = clientIter->lock();
if(client.get()) {
unlock();
client->detach(getPtrSelf());
lock();
}
pvRecordClientList.erase(clientIter);
}
std::list<PVListenerWPtr>::iterator listenerIter;
while(true) {
listenerIter = pvListenerList.begin();
if(listenerIter==pvListenerList.end()) break;
PVListenerPtr listener = listenerIter->lock();
if(listener.get()) {
unlock();
listener->unlisten(getPtrSelf());
lock();
}
pvListenerList.erase(listenerIter);
}
pvRecordStructure->destroy();
pvRecordStructure.reset();
pvStructure.reset();
unlock();
} catch(...) {
unlock();
throw;
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return;
}
isDestroyed = true;
pvTimeStamp.detach();
std::list<PVRecordClientWPtr>::iterator clientIter;
while(true) {
clientIter = pvRecordClientList.begin();
if(clientIter==pvRecordClientList.end()) break;
PVRecordClientPtr client = clientIter->lock();
if(client.get()) {
epicsGuardRelease<epics::pvData::Mutex> unguard(guard);
client->detach(getPtrSelf());
}
pvRecordClientList.erase(clientIter);
}
std::list<PVListenerWPtr>::iterator listenerIter;
while(true) {
listenerIter = pvListenerList.begin();
if(listenerIter==pvListenerList.end()) break;
PVListenerPtr listener = listenerIter->lock();
if(listener.get()) {
epicsGuardRelease<epics::pvData::Mutex> unguard(guard);
listener->unlisten(getPtrSelf());
}
pvListenerList.erase(listenerIter);
}
pvRecordStructure->destroy();
pvRecordStructure.reset();
pvStructure.reset();
}
void PVRecord::process()
@ -200,31 +191,23 @@ bool PVRecord::addPVRecordClient(PVRecordClientPtr const & pvRecordClient)
if(traceLevel>2) {
cout << "PVRecord::addPVRecordClient() " << recordName << endl;
}
lock();
try {
if(isDestroyed) {
unlock();
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return false;
}
std::list<PVRecordClientWPtr>::iterator iter;
for (iter = pvRecordClientList.begin();
iter!=pvRecordClientList.end();
iter++ )
{
PVRecordClientPtr client = iter->lock();
if(!client.get()) continue;
if(client.get()==pvRecordClient.get()) {
return false;
}
std::list<PVRecordClientWPtr>::iterator iter;
for (iter = pvRecordClientList.begin();
iter!=pvRecordClientList.end();
iter++ )
{
PVRecordClientPtr client = iter->lock();
if(!client.get()) continue;
if(client.get()==pvRecordClient.get()) {
unlock();
return false;
}
}
pvRecordClientList.push_back(pvRecordClient);
unlock();
return true;
} catch (...) {
unlock();
throw;
}
pvRecordClientList.push_back(pvRecordClient);
return true;
}
bool PVRecord::removePVRecordClient(PVRecordClientPtr const & pvRecordClient)
@ -232,31 +215,23 @@ bool PVRecord::removePVRecordClient(PVRecordClientPtr const & pvRecordClient)
if(traceLevel>2) {
cout << "PVRecord::removePVRecordClient() " << recordName << endl;
}
lock();
try {
if(isDestroyed) {
unlock();
return false;
}
std::list<PVRecordClientWPtr>::iterator iter;
for (iter = pvRecordClientList.begin();
iter!=pvRecordClientList.end();
iter++ )
{
PVRecordClientPtr client = iter->lock();
if(!client.get()) continue;
if(client.get()==pvRecordClient.get()) {
pvRecordClientList.erase(iter);
unlock();
return true;
}
}
unlock();
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return false;
} catch (...) {
unlock();
throw;
}
std::list<PVRecordClientWPtr>::iterator iter;
for (iter = pvRecordClientList.begin();
iter!=pvRecordClientList.end();
iter++ )
{
PVRecordClientPtr client = iter->lock();
if(!client.get()) continue;
if(client.get()==pvRecordClient.get()) {
pvRecordClientList.erase(iter);
return true;
}
}
return false;
}
void PVRecord::detachClients()
@ -264,29 +239,21 @@ void PVRecord::detachClients()
if(traceLevel>1) {
cout << "PVRecord::removePVRecordClient() " << recordName << endl;
}
lock();
try {
if(isDestroyed) {
unlock();
return;
}
std::list<PVRecordClientWPtr>::iterator iter;
for (iter = pvRecordClientList.begin();
iter!=pvRecordClientList.end();
iter++ )
{
PVRecordClientPtr client = iter->lock();
if(!client.get()) continue;
unlock();
client->detach(getPtrSelf());
lock();
}
pvRecordClientList.clear();
unlock();
} catch(...) {
unlock();
throw;
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return;
}
std::list<PVRecordClientWPtr>::iterator iter;
for (iter = pvRecordClientList.begin();
iter!=pvRecordClientList.end();
iter++ )
{
PVRecordClientPtr client = iter->lock();
if(!client.get()) continue;
epicsGuardRelease <epics::pvData::Mutex> unguard(guard);
client->detach(getPtrSelf());
}
pvRecordClientList.clear();
}
bool PVRecord::addListener(
@ -296,33 +263,25 @@ bool PVRecord::addListener(
if(traceLevel>1) {
cout << "PVRecord::addListener() " << recordName << endl;
}
lock();
try {
if(isDestroyed) {
unlock();
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return false;
}
std::list<PVListenerWPtr>::iterator iter;
for (iter = pvListenerList.begin(); iter!=pvListenerList.end(); iter++ )
{
PVListenerPtr listener = iter->lock();
if(!listener.get()) continue;
if(listener.get()==pvListener.get()) {
return false;
}
std::list<PVListenerWPtr>::iterator iter;
for (iter = pvListenerList.begin(); iter!=pvListenerList.end(); iter++ )
{
PVListenerPtr listener = iter->lock();
if(!listener.get()) continue;
if(listener.get()==pvListener.get()) {
unlock();
return false;
}
}
pvListenerList.push_back(pvListener);
this->pvListener = pvListener;
isAddListener = true;
pvCopy->traverseMaster(getPtrSelf());
this->pvListener = PVListenerPtr();;
unlock();
return true;
} catch(...) {
unlock();
throw;
}
pvListenerList.push_back(pvListener);
this->pvListener = pvListener;
isAddListener = true;
pvCopy->traverseMaster(getPtrSelf());
this->pvListener = PVListenerPtr();;
return true;
}
void PVRecord::nextMasterPVField(PVFieldPtr const & pvField)
@ -344,33 +303,25 @@ bool PVRecord::removeListener(
if(traceLevel>1) {
cout << "PVRecord::removeListener() " << recordName << endl;
}
lock();
try {
if(isDestroyed) {
unlock();
return false;
}
std::list<PVListenerWPtr>::iterator iter;
for (iter = pvListenerList.begin(); iter!=pvListenerList.end(); iter++ )
{
PVListenerPtr listener = iter->lock();
if(!listener.get()) continue;
if(listener.get()==pvListener.get()) {
pvListenerList.erase(iter);
this->pvListener = pvListener;
isAddListener = false;
pvCopy->traverseMaster(getPtrSelf());
this->pvListener = PVListenerPtr();
unlock();
return true;
}
}
unlock();
epicsGuard<epics::pvData::Mutex> guard(mutex);
if(isDestroyed) {
return false;
} catch(...) {
unlock();
throw;
}
std::list<PVListenerWPtr>::iterator iter;
for (iter = pvListenerList.begin(); iter!=pvListenerList.end(); iter++ )
{
PVListenerPtr listener = iter->lock();
if(!listener.get()) continue;
if(listener.get()==pvListener.get()) {
pvListenerList.erase(iter);
this->pvListener = pvListener;
isAddListener = false;
pvCopy->traverseMaster(getPtrSelf());
this->pvListener = PVListenerPtr();
return true;
}
}
return false;
}
void PVRecord::beginGroupPut()

View File

@ -11,6 +11,7 @@
#include <sstream>
#include <epicsGuard.h>
#include <epicsThread.h>
#include <pv/timeStamp.h>
#include <pv/pvSubArrayCopy.h>
@ -204,16 +205,10 @@ void ChannelProcessLocal::process()
cout << " nProcess " << nProcess << endl;
}
for(int i=0; i< nProcess; i++) {
pvRecord->lock();
try {
pvRecord->beginGroupPut();
pvRecord->process();
pvRecord->endGroupPut();
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
epicsGuard <PVRecord> guard(*pvRecord);
pvRecord->beginGroupPut();
pvRecord->process();
pvRecord->endGroupPut();
}
requester->processDone(Status::Ok,getPtrSelf());
}
@ -349,19 +344,15 @@ void ChannelGetLocal::get()
return;
}
bitSet->clear();
pvRecord->lock();
try {
{
epicsGuard <PVRecord> guard(*pvRecord);
if(callProcess) {
pvRecord->beginGroupPut();
pvRecord->process();
pvRecord->endGroupPut();
}
pvCopy->updateCopySetBitSet(pvStructure, bitSet);
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
if(firstTime) {
bitSet->clear();
bitSet->set(0);
@ -502,14 +493,10 @@ void ChannelPutLocal::get()
BitSetPtr bitSet(new BitSet(pvStructure->getNumberFields()));
bitSet->clear();
bitSet->set(0);
pvRecord->lock();
try {
{
epicsGuard <PVRecord> guard(*pvRecord);
pvCopy->updateCopyFromBitSet(pvStructure, bitSet);
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
requester->getDone(
Status::Ok,getPtrSelf(),pvStructure,bitSet);
if(pvRecord->getTraceLevel()>1)
@ -527,19 +514,15 @@ void ChannelPutLocal::put(
requester->putDone(channelDestroyedStatus,getPtrSelf());
return;
}
pvRecord->lock();
try {
{
epicsGuard <PVRecord> guard(*pvRecord);
pvRecord->beginGroupPut();
pvCopy->updateMaster(pvStructure, bitSet);
if(callProcess) {
pvRecord->process();
}
pvRecord->endGroupPut();
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
requester->putDone(Status::Ok,getPtrSelf());
if(pvRecord->getTraceLevel()>1)
{
@ -690,19 +673,15 @@ void ChannelPutGetLocal::putGet(
channelDestroyedStatus,getPtrSelf(),nullPVStructure,nullBitSet);
return;
}
pvRecord->lock();
try {
{
epicsGuard <PVRecord> guard(*pvRecord);
pvRecord->beginGroupPut();
pvPutCopy->updateMaster(pvPutStructure, putBitSet);
if(callProcess) pvRecord->process();
getBitSet->clear();
pvGetCopy->updateCopySetBitSet(pvGetStructure, getBitSet);
pvRecord->endGroupPut();
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
requester->putGetDone(
Status::Ok,getPtrSelf(),pvGetStructure,getBitSet);
if(pvRecord->getTraceLevel()>1)
@ -722,14 +701,10 @@ void ChannelPutGetLocal::getPut()
}
PVStructurePtr pvPutStructure = pvPutCopy->createPVStructure();
BitSetPtr putBitSet(new BitSet(pvPutStructure->getNumberFields()));
pvRecord->lock();
try {
{
epicsGuard <PVRecord> guard(*pvRecord);
pvPutCopy->initCopy(pvPutStructure, putBitSet);
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
requester->getPutDone(
Status::Ok,getPtrSelf(),pvPutStructure,putBitSet);
if(pvRecord->getTraceLevel()>1)
@ -748,14 +723,10 @@ void ChannelPutGetLocal::getGet()
return;
}
getBitSet->clear();
pvRecord->lock();
try {
{
epicsGuard <PVRecord> guard(*pvRecord);
pvGetCopy->updateCopySetBitSet(pvGetStructure, getBitSet);
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
requester->getGetDone(
Status::Ok,getPtrSelf(),pvGetStructure,getBitSet);
if(pvRecord->getTraceLevel()>1)
@ -1175,9 +1146,9 @@ void ChannelArrayLocal::getArray(size_t offset, size_t count, size_t stride)
return;
}
const char *exceptionMessage = NULL;
pvRecord->lock();
try {
bool ok = false;
epicsGuard <PVRecord> guard(*pvRecord);
while(true) {
size_t length = pvArray->getLength();
if(length<=0) break;
@ -1198,7 +1169,6 @@ void ChannelArrayLocal::getArray(size_t offset, size_t count, size_t stride)
} catch(std::exception e) {
exceptionMessage = e.what();
}
pvRecord->unlock();
Status status = Status::Ok;
if(exceptionMessage!=NULL) {
status = Status(Status::STATUSTYPE_ERROR,exceptionMessage);
@ -1234,13 +1204,12 @@ void ChannelArrayLocal::putArray(
size_t newLength = offset + count*stride;
if(newLength<pvArray->getLength()) pvArray->setLength(newLength);
const char *exceptionMessage = NULL;
pvRecord->lock();
try {
epicsGuard <PVRecord> guard(*pvRecord);
copy(pvArray,0,1,this->pvArray,offset,stride,count);
} catch(std::exception e) {
exceptionMessage = e.what();
}
pvRecord->unlock();
Status status = Status::Ok;
if(exceptionMessage!=NULL) {
status = Status(Status::STATUSTYPE_ERROR,exceptionMessage);
@ -1254,13 +1223,12 @@ void ChannelArrayLocal::getLength()
if(!requester.get()) return;
size_t length = 0;
const char *exceptionMessage = NULL;
pvRecord->lock();
try {
epicsGuard <PVRecord> guard(*pvRecord);
length = pvArray->getLength();
} catch(std::exception e) {
exceptionMessage = e.what();
}
pvRecord->unlock();
Status status = Status::Ok;
if(exceptionMessage!=NULL) {
status = Status(Status::STATUSTYPE_ERROR,exceptionMessage);
@ -1280,16 +1248,12 @@ void ChannelArrayLocal::setLength(size_t length)
{
cout << "ChannelArrayLocal::setLength" << endl;
}
pvRecord->lock();
try {
{
epicsGuard <PVRecord> guard(*pvRecord);
if(length>=0) {
if(pvArray->getLength()!=length) pvArray->setLength(length);
}
} catch(...) {
pvRecord->unlock();
throw;
}
pvRecord->unlock();
requester->setLengthDone(Status::Ok,getPtrSelf());
}

View File

@ -11,6 +11,7 @@
#include <sstream>
#include <epicsGuard.h>
#include <pv/thread.h>
#include <pv/bitSetUtil.h>
#include <pv/queue.h>
@ -144,21 +145,16 @@ Status MonitorLocal::start()
if(state==active) return alreadyStartedStatus;
}
pvRecord->addListener(getPtrSelf(),pvCopy);
pvRecord->lock();
try {
Lock xx(mutex);
state = active;
queue->clear();
isGroupPut = false;
activeElement = queue->getFree();
activeElement->changedBitSet->clear();
activeElement->overrunBitSet->clear();
activeElement->changedBitSet->set(0);
releaseActiveElement();
pvRecord->unlock();
} catch(...) {
pvRecord->unlock();
}
epicsGuard <PVRecord> guard(*pvRecord);
Lock xx(mutex);
state = active;
queue->clear();
isGroupPut = false;
activeElement = queue->getFree();
activeElement->changedBitSet->clear();
activeElement->overrunBitSet->clear();
activeElement->changedBitSet->set(0);
releaseActiveElement();
return Status::Ok;
}