Emesary : change to use scoped lock

- scoped locks using lock_guard are better
- because of this the exception handler can also be removed;
  although this was originally intended to manage the locks
  it never did work properly with C++ exceptions.
This commit is contained in:
Richard Harrison
2020-12-17 12:57:53 +00:00
committed by James Turner
parent 4251b28e88
commit 985897f8ba
+47 -61
View File
@@ -45,14 +45,6 @@ namespace simgear
std::atomic<int> receiveDepth; std::atomic<int> receiveDepth;
std::atomic<int> sentMessageCount; std::atomic<int> sentMessageCount;
void UnlockList()
{
_lock.unlock();
}
void LockList()
{
_lock.lock();
}
public: public:
Transmitter() : receiveDepth(0), sentMessageCount(0) Transmitter() : receiveDepth(0), sentMessageCount(0)
{ {
@@ -69,20 +61,17 @@ namespace simgear
// most recently registered recipients should process the messages/events first. // most recently registered recipients should process the messages/events first.
virtual void Register(IReceiver& r) virtual void Register(IReceiver& r)
{ {
LockList(); std::lock_guard<std::mutex> scopeLock(_lock);
recipient_list.push_back(&r); recipient_list.push_back(&r);
r.OnRegisteredAtTransmitter(this); r.OnRegisteredAtTransmitter(this);
if (std::find(deleted_recipients.begin(), deleted_recipients.end(), &r) != deleted_recipients.end()) if (std::find(deleted_recipients.begin(), deleted_recipients.end(), &r) != deleted_recipients.end())
deleted_recipients.remove(&r); deleted_recipients.remove(&r);
UnlockList();
} }
// Removes an object from receving message from this transmitter // Removes an object from receving message from this transmitter
virtual void DeRegister(IReceiver& R) virtual void DeRegister(IReceiver& R)
{ {
LockList(); std::lock_guard<std::mutex> scopeLock(_lock);
//printf("Remove %x\n", &R);
if (recipient_list.size()) if (recipient_list.size())
{ {
if (std::find(recipient_list.begin(), recipient_list.end(), &R) != recipient_list.end()) if (std::find(recipient_list.begin(), recipient_list.end(), &R) != recipient_list.end())
@@ -93,7 +82,6 @@ namespace simgear
deleted_recipients.push_back(&R); deleted_recipients.push_back(&R);
} }
} }
UnlockList();
} }
// Notify all registered recipients. Stop when receipt status of abort or finished are received. // Notify all registered recipients. Stop when receipt status of abort or finished are received.
@@ -107,69 +95,68 @@ namespace simgear
ReceiptStatus return_status = ReceiptStatusNotProcessed; ReceiptStatus return_status = ReceiptStatusNotProcessed;
sentMessageCount++; sentMessageCount++;
try
std::vector<IReceiver*> temp;
{ {
LockList(); std::lock_guard<std::mutex> scopeLock(_lock);
if (receiveDepth == 0) if (receiveDepth == 0)
deleted_recipients.clear(); deleted_recipients.clear();
receiveDepth++; receiveDepth++;
std::vector<IReceiver*> temp(recipient_list.size());
int idx = 0; int idx = 0;
for (RecipientList::iterator i = recipient_list.begin(); i != recipient_list.end(); i++) for (RecipientList::iterator i = recipient_list.begin(); i != recipient_list.end(); i++)
{ {
temp[idx++] = *i; temp.push_back(*i);
} }
UnlockList(); }
int tempSize = temp.size(); int tempSize = temp.size();
for (int index = 0; index < tempSize; index++) for (int index = 0; index < tempSize; index++)
{
IReceiver* R = temp[index];
{ {
IReceiver* R = temp[index]; std::lock_guard<std::mutex> scopeLock(_lock);
LockList();
if (deleted_recipients.size()) if (deleted_recipients.size())
{ {
if (std::find(deleted_recipients.begin(), deleted_recipients.end(), R) != deleted_recipients.end()) if (std::find(deleted_recipients.begin(), deleted_recipients.end(), R) != deleted_recipients.end())
{ {
UnlockList();
continue; continue;
} }
} }
UnlockList();
if (R)
{
ReceiptStatus rstat = R->Receive(M);
switch (rstat)
{
case ReceiptStatusFail:
return_status = ReceiptStatusFail;
break;
case ReceiptStatusPending:
return_status = ReceiptStatusPending;
break;
case ReceiptStatusPendingFinished:
return rstat;
case ReceiptStatusNotProcessed:
break;
case ReceiptStatusOK:
if (return_status == ReceiptStatusNotProcessed)
return_status = rstat;
break;
case ReceiptStatusAbort:
return ReceiptStatusAbort;
case ReceiptStatusFinished:
return ReceiptStatusOK;
}
}
} }
if (R)
{
ReceiptStatus rstat = R->Receive(M);
switch (rstat)
{
case ReceiptStatusFail:
return_status = ReceiptStatusFail;
break;
case ReceiptStatusPending:
return_status = ReceiptStatusPending;
break;
case ReceiptStatusPendingFinished:
return rstat;
case ReceiptStatusNotProcessed:
break;
case ReceiptStatusOK:
if (return_status == ReceiptStatusNotProcessed)
return_status = rstat;
break;
case ReceiptStatusAbort:
return ReceiptStatusAbort;
case ReceiptStatusFinished:
return ReceiptStatusOK;
}
}
} }
catch (...)
{
throw;
// return_status = ReceiptStatusAbort;
}
receiveDepth--; receiveDepth--;
return return_status; return return_status;
} }
@@ -177,9 +164,8 @@ namespace simgear
// number of currently registered recipients // number of currently registered recipients
virtual int Count() virtual int Count()
{ {
LockList(); std::lock_guard<std::mutex> scopeLock(_lock);
return recipient_list.size(); return recipient_list.size();
UnlockList();
} }
// number of sent messages. // number of sent messages.