Skip to content

Commit

Permalink
add some mutex locking for affiliation lookups;
Browse files Browse the repository at this point in the history
  • Loading branch information
gatekeep committed Nov 12, 2024
1 parent 482a480 commit 2979110
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
24 changes: 22 additions & 2 deletions src/common/lookups/AffiliationLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ using namespace lookups;

const uint32_t UNIT_REG_TIMEOUT = 43200U; // 12 hours

// ---------------------------------------------------------------------------
// Static Class Members
// ---------------------------------------------------------------------------

std::mutex AffiliationLookup::m_mutex;

// ---------------------------------------------------------------------------
// Public Class Members
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -297,6 +303,7 @@ bool AffiliationLookup::grantCh(uint32_t dstId, uint32_t srcId, uint32_t grantTi
return false;
}

std::lock_guard<std::mutex> lock(m_mutex);
uint32_t chNo = m_chLookup->getFirstRFChannel();
m_chLookup->removeRFCh(chNo);

Expand Down Expand Up @@ -326,19 +333,23 @@ void AffiliationLookup::touchGrant(uint32_t dstId)
return;
}

std::lock_guard<std::mutex> lock(m_mutex);
if (isGranted(dstId)) {
m_grantTimers[dstId].start();
}
}

/* Helper to release the channel grant for the destination ID. */

bool AffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll)
bool AffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll, bool noLock)
{
if (dstId == 0U && !releaseAll) {
return false;
}

if (!noLock)
m_mutex.lock();

// are we trying to release all grants?
if (dstId == 0U && releaseAll) {
LogWarning(LOG_HOST, "%s, force releasing all channel grants", m_name.c_str());
Expand All @@ -354,6 +365,8 @@ bool AffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll)
releaseGrant(dstId, false);
}

if (!noLock)
m_mutex.unlock();
return true;
}

Expand Down Expand Up @@ -383,9 +396,14 @@ bool AffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll)
}

m_grantTimers[dstId].stop();

if (!noLock)
m_mutex.unlock();
return true;
}

if (!noLock)
m_mutex.unlock();
return false;
}

Expand Down Expand Up @@ -527,6 +545,8 @@ uint32_t AffiliationLookup::getGrantedSrcId(uint32_t dstId)

void AffiliationLookup::clock(uint32_t ms)
{
std::lock_guard<std::mutex> lock(m_mutex);

// clock all the grant timers
std::vector<uint32_t> gntsToRel = std::vector<uint32_t>();
for (auto entry : m_grantChTable) {
Expand All @@ -540,7 +560,7 @@ void AffiliationLookup::clock(uint32_t ms)

// release grants that have timed out
for (uint32_t dstId : gntsToRel) {
releaseGrant(dstId, false);
releaseGrant(dstId, false, true);
}

if (!m_disableUnitRegTimeout) {
Expand Down
6 changes: 5 additions & 1 deletion src/common/lookups/AffiliationLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <algorithm>
#include <vector>
#include <functional>
#include <mutex>

namespace lookups
{
Expand Down Expand Up @@ -182,9 +183,10 @@ namespace lookups
* @brief Helper to release the channel grant for the destination ID.
* @param dstId Destination Address.
* @param releaseAll Flag indicating all channel grants should be released.
* @param noLock Flag indicating no mutex lock operation should be performed while releasing.
* @returns bool True, if channel grant was released, otherwise false.
*/
virtual bool releaseGrant(uint32_t dstId, bool releaseAll);
virtual bool releaseGrant(uint32_t dstId, bool releaseAll, bool noLock = false);
/**
* @brief Helper to determine if the channel number is busy.
* @param chNo Channel Number.
Expand Down Expand Up @@ -298,6 +300,8 @@ namespace lookups
bool m_disableUnitRegTimeout;

bool m_verbose;

static std::mutex m_mutex;
};
} // namespace lookups

Expand Down
12 changes: 11 additions & 1 deletion src/host/dmr/lookups/DMRAffiliationLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ bool DMRAffiliationLookup::grantChSlot(uint32_t dstId, uint32_t srcId, uint8_t s

/* Helper to release the channel grant for the destination ID. */

bool DMRAffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll)
bool DMRAffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll, bool noLock)
{
if (dstId == 0U && !releaseAll) {
return false;
}

if (!noLock)
m_mutex.lock();

// are we trying to release all grants?
if (dstId == 0U && releaseAll) {
LogWarning(LOG_HOST, "%s, force releasing all channel grants", m_name.c_str());
Expand All @@ -107,6 +110,8 @@ bool DMRAffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll)
releaseGrant(dstId, false);
}

if (!noLock)
m_mutex.unlock();
return true;
}

Expand Down Expand Up @@ -139,9 +144,14 @@ bool DMRAffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll)
}

m_grantTimers[dstId].stop();

if (!noLock)
m_mutex.unlock();
return true;
}

if (!noLock)
m_mutex.unlock();
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/host/dmr/lookups/DMRAffiliationLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ namespace dmr
* @brief Helper to release the channel grant for the destination ID.
* @param dstId Destination Address.
* @param releaseAll Flag indicating all channel grants should be released.
* @param noLock Flag indicating no mutex lock operation should be performed while releasing.
* @returns bool True, if channel grant was released, otherwise false.
*/
bool releaseGrant(uint32_t dstId, bool releaseAll) override;
bool releaseGrant(uint32_t dstId, bool releaseAll, bool noLock = false) override;
/**
* @brief Helper to determine if the channel number is busy.
* @param chNo Channel Number.
Expand Down
4 changes: 2 additions & 2 deletions src/host/p25/lookups/P25AffiliationLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ std::vector<uint32_t> P25AffiliationLookup::clearGroupAff(uint32_t dstId, bool r

/* Helper to release the channel grant for the destination ID. */

bool P25AffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll)
bool P25AffiliationLookup::releaseGrant(uint32_t dstId, bool releaseAll, bool noLock)
{
bool ret = ::lookups::AffiliationLookup::releaseGrant(dstId, releaseAll);
bool ret = ::lookups::AffiliationLookup::releaseGrant(dstId, releaseAll, noLock);
if (ret) {
if (m_rfGrantChCnt > 0U) {
m_p25->m_siteData.setChCnt(m_chLookup->rfChSize() + m_rfGrantChCnt);
Expand Down
3 changes: 2 additions & 1 deletion src/host/p25/lookups/P25AffiliationLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ namespace p25
* @brief Helper to release the channel grant for the destination ID.
* @param dstId Destination Address.
* @param releaseAll Flag indicating all channel grants should be released.
* @param noLock Flag indicating no mutex lock operation should be performed while releasing.
* @returns bool True, if channel grant was released, otherwise false.
*/
bool releaseGrant(uint32_t dstId, bool releaseAll) override;
bool releaseGrant(uint32_t dstId, bool releaseAll, bool noLock = false) override;
/** @} */

protected:
Expand Down

0 comments on commit 2979110

Please sign in to comment.