add proper mutex locking to prevent thread clobbering during clocking (i.e. attempts to make modem and protocol clocking thread safe); split frame read nad write operations into their own threads;

pull/42/head
Bryan Biedenkapp 2 years ago
parent 629c2fe75e
commit 7a90f953fd

@ -55,6 +55,7 @@ using namespace lookups;
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <mutex>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -755,6 +756,7 @@ int Host::run()
} }
bool hasTxShutdown = false; bool hasTxShutdown = false;
std::mutex clockingMutex;
// Macro to start DMR duplex idle transmission (or beacon) // Macro to start DMR duplex idle transmission (or beacon)
#define START_DMR_DUPLEX_IDLE(x) \ #define START_DMR_DUPLEX_IDLE(x) \
@ -767,17 +769,23 @@ int Host::run()
// setup protocol processor threads // setup protocol processor threads
/** Digital Mobile Radio */ /** Digital Mobile Radio */
ThreadFunc dmrProcessThread([&, this]() { ThreadFunc dmrFrameReadThread([&, this]() {
#if defined(ENABLE_DMR) #if defined(ENABLE_DMR)
if (dmr != nullptr) { if (dmr != nullptr) {
LogDebug(LOG_HOST, "DMR, started frame processor"); LogDebug(LOG_HOST, "DMR, started frame processor (modem read)");
while (!g_killed) { while (!g_killed) {
clockingMutex.lock();
{
// ------------------------------------------------------ // ------------------------------------------------------
// -- Write to Modem Processing -- // -- Read from Modem Processing --
// ------------------------------------------------------ // ------------------------------------------------------
// write DMR slot 1 frames to modem // read DMR slot 1 frames from modem
writeFramesDMR1(dmr.get(), [&, this]() { readFramesDMR1(dmr.get(), [&, this]() {
if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get());
}
// if there is a P25 CC running; halt the CC // if there is a P25 CC running; halt the CC
if (p25 != nullptr) { if (p25 != nullptr) {
if (p25->getCCRunning() && !p25->getCCHalted()) { if (p25->getCCRunning() && !p25->getCCHalted()) {
@ -793,8 +801,12 @@ int Host::run()
} }
}); });
// write DMR slot 2 frames to modem // read DMR slot 2 frames from modem
writeFramesDMR2(dmr.get(), [&, this]() { readFramesDMR2(dmr.get(), [&, this]() {
if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get());
}
// if there is a P25 CC running; halt the CC // if there is a P25 CC running; halt the CC
if (p25 != nullptr) { if (p25 != nullptr) {
if (p25->getCCRunning() && !p25->getCCHalted()) { if (p25->getCCRunning() && !p25->getCCHalted()) {
@ -809,17 +821,32 @@ int Host::run()
} }
} }
}); });
}
clockingMutex.unlock();
// ------------------------------------------------------ if (m_state != STATE_IDLE)
// -- Read from Modem Processing -- Thread::sleep(m_activeTickDelay);
// ------------------------------------------------------ if (m_state == STATE_IDLE)
Thread::sleep(m_idleTickDelay);
}
}
#endif // defined(ENABLE_DMR)
});
dmrFrameReadThread.run();
// read DMR slot 1 frames from modem ThreadFunc dmrFrameWriteThread([&, this]() {
readFramesDMR1(dmr.get(), [&, this]() { #if defined(ENABLE_DMR)
if (dmr != nullptr) { if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get()); LogDebug(LOG_HOST, "DMR, started frame processor (modem write)");
} while (!g_killed) {
clockingMutex.lock();
{
// ------------------------------------------------------
// -- Write to Modem Processing --
// ------------------------------------------------------
// write DMR slot 1 frames to modem
writeFramesDMR1(dmr.get(), [&, this]() {
// if there is a P25 CC running; halt the CC // if there is a P25 CC running; halt the CC
if (p25 != nullptr) { if (p25 != nullptr) {
if (p25->getCCRunning() && !p25->getCCHalted()) { if (p25->getCCRunning() && !p25->getCCHalted()) {
@ -835,12 +862,8 @@ int Host::run()
} }
}); });
// read DMR slot 2 frames from modem // write DMR slot 2 frames to modem
readFramesDMR2(dmr.get(), [&, this]() { writeFramesDMR2(dmr.get(), [&, this]() {
if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get());
}
// if there is a P25 CC running; halt the CC // if there is a P25 CC running; halt the CC
if (p25 != nullptr) { if (p25 != nullptr) {
if (p25->getCCRunning() && !p25->getCCHalted()) { if (p25->getCCRunning() && !p25->getCCHalted()) {
@ -855,6 +878,8 @@ int Host::run()
} }
} }
}); });
}
clockingMutex.unlock();
if (m_state != STATE_IDLE) if (m_state != STATE_IDLE)
Thread::sleep(m_activeTickDelay); Thread::sleep(m_activeTickDelay);
@ -864,20 +889,22 @@ int Host::run()
} }
#endif // defined(ENABLE_DMR) #endif // defined(ENABLE_DMR)
}); });
dmrProcessThread.run(); dmrFrameWriteThread.run();
/** Project 25 */ /** Project 25 */
ThreadFunc p25ProcessThread([&, this]() { ThreadFunc p25FrameReadThread([&, this]() {
#if defined(ENABLE_P25) #if defined(ENABLE_P25)
if (p25 != nullptr) { if (p25 != nullptr) {
LogDebug(LOG_HOST, "P25, started frame processor"); LogDebug(LOG_HOST, "P25, started frame processor (modem read)");
while (!g_killed) { while (!g_killed) {
clockingMutex.lock();
{
// ------------------------------------------------------ // ------------------------------------------------------
// -- Write to Modem Processing -- // -- Read from Modem Processing --
// ------------------------------------------------------ // ------------------------------------------------------
// write P25 frames to modem // read P25 frames from modem
writeFramesP25(p25.get(), [&, this]() { readFramesP25(p25.get(), [&, this]() {
if (dmr != nullptr) { if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get()); this->interruptDMRBeacon(dmr.get());
} }
@ -889,13 +916,32 @@ int Host::run()
} }
} }
}); });
}
clockingMutex.unlock();
if (m_state != STATE_IDLE)
Thread::sleep(m_activeTickDelay);
if (m_state == STATE_IDLE)
Thread::sleep(m_idleTickDelay);
}
}
#endif // defined(ENABLE_P25)
});
p25FrameReadThread.run();
ThreadFunc p25FrameWriteThread([&, this]() {
#if defined(ENABLE_P25)
if (p25 != nullptr) {
LogDebug(LOG_HOST, "P25, started frame processor (modem write)");
while (!g_killed) {
clockingMutex.lock();
{
// ------------------------------------------------------ // ------------------------------------------------------
// -- Read from Modem Processing -- // -- Write to Modem Processing --
// ------------------------------------------------------ // ------------------------------------------------------
// read P25 frames from modem // write P25 frames to modem
readFramesP25(p25.get(), [&, this]() { writeFramesP25(p25.get(), [&, this]() {
if (dmr != nullptr) { if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get()); this->interruptDMRBeacon(dmr.get());
} }
@ -907,6 +953,8 @@ int Host::run()
} }
} }
}); });
}
clockingMutex.unlock();
if (m_state != STATE_IDLE) if (m_state != STATE_IDLE)
Thread::sleep(m_activeTickDelay); Thread::sleep(m_activeTickDelay);
@ -916,20 +964,22 @@ int Host::run()
} }
#endif // defined(ENABLE_P25) #endif // defined(ENABLE_P25)
}); });
p25ProcessThread.run(); p25FrameWriteThread.run();
/** Next Generation Digital Narrowband */ /** Next Generation Digital Narrowband */
ThreadFunc nxdnProcessThread([&, this]() { ThreadFunc nxdnFrameReadThread([&, this]() {
#if defined(ENABLE_NXDN) #if defined(ENABLE_NXDN)
if (nxdn != nullptr) { if (nxdn != nullptr) {
LogDebug(LOG_HOST, "NXDN, started frame processor"); LogDebug(LOG_HOST, "NXDN, started frame processor (modem read)");
while (!g_killed) { while (!g_killed) {
clockingMutex.lock();
{
// ------------------------------------------------------ // ------------------------------------------------------
// -- Write to Modem Processing -- // -- Read from Modem Processing --
// ------------------------------------------------------ // ------------------------------------------------------
// write NXDN frames to modem // read NXDN frames from modem
writeFramesNXDN(nxdn.get(), [&, this]() { readFramesNXDN(nxdn.get(), [&, this]() {
if (dmr != nullptr) { if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get()); this->interruptDMRBeacon(dmr.get());
} }
@ -941,13 +991,32 @@ int Host::run()
} }
} }
}); });
}
clockingMutex.unlock();
if (m_state != STATE_IDLE)
Thread::sleep(m_activeTickDelay);
if (m_state == STATE_IDLE)
Thread::sleep(m_idleTickDelay);
}
}
#endif // defined(ENABLE_NXDN)
});
nxdnFrameReadThread.run();
ThreadFunc nxdnFrameWriteThread([&, this]() {
#if defined(ENABLE_NXDN)
if (nxdn != nullptr) {
LogDebug(LOG_HOST, "NXDN, started frame processor (modem write)");
while (!g_killed) {
clockingMutex.lock();
{
// ------------------------------------------------------ // ------------------------------------------------------
// -- Read from Modem Processing -- // -- Write to Modem Processing --
// ------------------------------------------------------ // ------------------------------------------------------
// read NXDN frames from modem // write NXDN frames to modem
readFramesNXDN(nxdn.get(), [&, this]() { writeFramesNXDN(nxdn.get(), [&, this]() {
if (dmr != nullptr) { if (dmr != nullptr) {
this->interruptDMRBeacon(dmr.get()); this->interruptDMRBeacon(dmr.get());
} }
@ -959,6 +1028,8 @@ int Host::run()
} }
} }
}); });
}
clockingMutex.unlock();
if (m_state != STATE_IDLE) if (m_state != STATE_IDLE)
Thread::sleep(m_activeTickDelay); Thread::sleep(m_activeTickDelay);
@ -968,7 +1039,7 @@ int Host::run()
} }
#endif // defined(ENABLE_NXDN) #endif // defined(ENABLE_NXDN)
}); });
nxdnProcessThread.run(); nxdnFrameWriteThread.run();
// main execution loop // main execution loop
while (!killed) { while (!killed) {
@ -1007,6 +1078,8 @@ int Host::run()
} }
} }
clockingMutex.lock();
{
// ------------------------------------------------------ // ------------------------------------------------------
// -- Modem Clocking -- // -- Modem Clocking --
// ------------------------------------------------------ // ------------------------------------------------------
@ -1035,6 +1108,8 @@ int Host::run()
if (nxdn != nullptr) if (nxdn != nullptr)
nxdn->clock(ms); nxdn->clock(ms);
#endif // defined(ENABLE_NXDN) #endif // defined(ENABLE_NXDN)
}
clockingMutex.unlock();
// ------------------------------------------------------ // ------------------------------------------------------
// -- Timer Clocking -- // -- Timer Clocking --
@ -1292,9 +1367,12 @@ int Host::run()
if (g_killed) { if (g_killed) {
// shutdown reader threads // shutdown reader threads
dmrProcessThread.wait(); dmrFrameReadThread.wait();
p25ProcessThread.wait(); dmrFrameWriteThread.wait();
nxdnProcessThread.wait(); p25FrameReadThread.wait();
p25FrameWriteThread.wait();
nxdnFrameReadThread.wait();
nxdnFrameWriteThread.wait();
#if defined(ENABLE_DMR) #if defined(ENABLE_DMR)
if (dmr != nullptr) { if (dmr != nullptr) {

Loading…
Cancel
Save

Powered by TurnKey Linux.