From 14b880b7083a8eb03f1371aa50df1483f7a074a6 Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Tue, 13 Aug 2024 16:22:56 -0400 Subject: [PATCH] add some mutex locking for thread safety, ensure addFrame and getFrame cannot happen simultaneously; --- src/host/dmr/Slot.cpp | 5 +++++ src/host/dmr/Slot.h | 2 ++ src/host/nxdn/Control.cpp | 9 +++++++++ src/host/nxdn/Control.h | 2 ++ src/host/p25/Control.cpp | 10 ++++++++++ src/host/p25/Control.h | 2 ++ 6 files changed, 30 insertions(+) diff --git a/src/host/dmr/Slot.cpp b/src/host/dmr/Slot.cpp index ab74637a..15c22cd7 100644 --- a/src/host/dmr/Slot.cpp +++ b/src/host/dmr/Slot.cpp @@ -98,6 +98,7 @@ Slot::Slot(uint32_t slotNo, uint32_t timeout, uint32_t tgHang, uint32_t queueSiz m_slotNo(slotNo), m_txImmQueue(queueSize, "DMR Imm Slot Frame"), m_txQueue(queueSize, "DMR Slot Frame"), + m_queueLock(), m_rfState(RS_RF_LISTENING), m_rfLastDstId(0U), m_rfLastSrcId(0U), @@ -333,6 +334,8 @@ uint32_t Slot::getFrame(uint8_t* data) { assert(data != nullptr); + std::lock_guard lock(m_queueLock); + if (m_txQueue.isEmpty() && m_txImmQueue.isEmpty()) return 0U; @@ -987,6 +990,8 @@ void Slot::addFrame(const uint8_t *data, bool net, bool imm) { assert(data != nullptr); + std::lock_guard lock(m_queueLock); + if (!net) { if (m_netState != RS_NET_IDLE) return; diff --git a/src/host/dmr/Slot.h b/src/host/dmr/Slot.h index 004b329b..209c522f 100644 --- a/src/host/dmr/Slot.h +++ b/src/host/dmr/Slot.h @@ -35,6 +35,7 @@ #include "network/Network.h" #include +#include namespace dmr { @@ -307,6 +308,7 @@ namespace dmr RingBuffer m_txImmQueue; RingBuffer m_txQueue; + std::mutex m_queueLock; RPT_RF_STATE m_rfState; uint32_t m_rfLastDstId; diff --git a/src/host/nxdn/Control.cpp b/src/host/nxdn/Control.cpp index 6320cc42..20beb50c 100644 --- a/src/host/nxdn/Control.cpp +++ b/src/host/nxdn/Control.cpp @@ -42,6 +42,11 @@ const uint8_t SCRAMBLER[] = { 0x28U, 0x28U, 0x00U, 0x0AU, 0x02U, 0x82U, 0x20U, 0x28U, 0x82U, 0x2AU, 0xAAU, 0x20U, 0x22U, 0x80U, 0xA8U, 0x8AU, 0x08U, 0xA0U, 0xAAU, 0x02U }; +// --------------------------------------------------------------------------- +// Static Class Members +// --------------------------------------------------------------------------- + +std::mutex Control::m_queueLock; // --------------------------------------------------------------------------- // Public Class Members @@ -482,6 +487,8 @@ uint32_t Control::getFrame(uint8_t* data) { assert(data != nullptr); + std::lock_guard lock(m_queueLock); + if (m_txQueue.isEmpty() && m_txImmQueue.isEmpty()) return 0U; @@ -825,6 +832,8 @@ void Control::addFrame(const uint8_t *data, bool net, bool imm) { assert(data != nullptr); + std::lock_guard lock(m_queueLock); + if (!net) { if (m_rfTimeout.isRunning() && m_rfTimeout.hasExpired()) return; diff --git a/src/host/nxdn/Control.h b/src/host/nxdn/Control.h index 68e16af6..716413c6 100644 --- a/src/host/nxdn/Control.h +++ b/src/host/nxdn/Control.h @@ -43,6 +43,7 @@ #include #include +#include namespace nxdn { @@ -289,6 +290,7 @@ namespace nxdn RingBuffer m_txImmQueue; RingBuffer m_txQueue; + static std::mutex m_queueLock; RPT_RF_STATE m_rfState; uint32_t m_rfLastDstId; diff --git a/src/host/p25/Control.cpp b/src/host/p25/Control.cpp index f02431ca..38befffa 100644 --- a/src/host/p25/Control.cpp +++ b/src/host/p25/Control.cpp @@ -37,6 +37,12 @@ const uint8_t MAX_SYNC_BYTES_ERRS = 4U; const uint32_t TSBK_PCH_CCH_CNT = 6U; const uint32_t MAX_PREAMBLE_TDU_CNT = 64U; +// --------------------------------------------------------------------------- +// Static Class Members +// --------------------------------------------------------------------------- + +std::mutex Control::m_queueLock; + // --------------------------------------------------------------------------- // Public Class Members // --------------------------------------------------------------------------- @@ -695,6 +701,8 @@ uint32_t Control::getFrame(uint8_t* data) { assert(data != nullptr); + std::lock_guard lock(m_queueLock); + if (m_txQueue.isEmpty() && m_txImmQueue.isEmpty()) return 0U; @@ -1133,6 +1141,8 @@ void Control::addFrame(const uint8_t* data, uint32_t length, bool net, bool imm) { assert(data != nullptr); + std::lock_guard lock(m_queueLock); + if (!net) { if (m_rfTimeout.isRunning() && m_rfTimeout.hasExpired()) return; diff --git a/src/host/p25/Control.h b/src/host/p25/Control.h index c7cb817e..3c4a2f8e 100644 --- a/src/host/p25/Control.h +++ b/src/host/p25/Control.h @@ -47,6 +47,7 @@ #include #include #include +#include namespace p25 { @@ -312,6 +313,7 @@ namespace p25 RingBuffer m_txImmQueue; RingBuffer m_txQueue; + static std::mutex m_queueLock; RPT_RF_STATE m_rfState; uint32_t m_rfLastDstId;