From 4b37a196448bfa96a542175a9cee944d6fd9e7a1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 2 Mar 2023 15:21:45 +0100 Subject: [PATCH] try to use more locks with timeouts --- bsp_q7s/fs/SdCardManager.cpp | 24 ++++++++++++------------ bsp_q7s/fs/SdCardManager.h | 4 +++- fsfw | 2 +- mission/config/torquer.h | 2 ++ mission/controller/AcsController.cpp | 2 +- mission/devices/ImtqHandler.cpp | 2 +- mission/devices/PcduHandler.cpp | 16 +++++++++------- mission/devices/PcduHandler.h | 5 ++++- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/bsp_q7s/fs/SdCardManager.cpp b/bsp_q7s/fs/SdCardManager.cpp index f8da9bee..45c9f0d0 100644 --- a/bsp_q7s/fs/SdCardManager.cpp +++ b/bsp_q7s/fs/SdCardManager.cpp @@ -20,14 +20,14 @@ SdCardManager* SdCardManager::INSTANCE = nullptr; SdCardManager::SdCardManager() : SystemObject(objects::SDC_MANAGER), cmdExecutor(256) { - mutex = MutexFactory::instance()->createMutex(); - ReturnValue_t result = mutex->lockMutex(); + sdLock = MutexFactory::instance()->createMutex(); + ReturnValue_t result = sdLock->lockMutex(); if (result != returnvalue::OK) { sif::error << "SdCardManager::SdCardManager: Mutex lock failed" << std::endl; } uint8_t prefSdRaw = 0; result = scratch::readNumber(scratch::PREFERED_SDC_KEY, prefSdRaw); - if (mutex->unlockMutex() != returnvalue::OK) { + if (sdLock->unlockMutex() != returnvalue::OK) { sif::error << "SdCardManager::SdCardManager: Mutex unlock failed" << std::endl; } @@ -195,7 +195,7 @@ ReturnValue_t SdCardManager::setSdCardState(sd::SdCard sdCard, bool on) { ReturnValue_t SdCardManager::getSdCardsStatus(SdStatePair& active) { using namespace std; - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); if (not filesystem::exists(SD_STATE_FILE)) { return STATUS_FILE_NEXISTS; } @@ -378,7 +378,7 @@ void SdCardManager::processSdStatusLine(std::pair& act } std::optional SdCardManager::getPreferredSdCard() const { - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); auto res = mg.getLockResult(); if (res != returnvalue::OK) { sif::error << "SdCardManager::getPreferredSdCard: Lock error" << std::endl; @@ -387,7 +387,7 @@ std::optional SdCardManager::getPreferredSdCard() const { } ReturnValue_t SdCardManager::setPreferredSdCard(sd::SdCard sdCard) { - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); if (sdCard == sd::SdCard::BOTH) { return returnvalue::FAILED; } @@ -399,7 +399,7 @@ ReturnValue_t SdCardManager::updateSdCardStateFile() { if (cmdExecutor.getCurrentState() == CommandExecutor::States::PENDING) { return CommandExecutor::COMMAND_PENDING; } - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); // Use q7hw utility and pipe the command output into the state file std::string updateCmd = "q7hw sd info all > " + std::string(SD_STATE_FILE); cmdExecutor.load(updateCmd, blocking, printCmdOutput); @@ -411,7 +411,7 @@ ReturnValue_t SdCardManager::updateSdCardStateFile() { } const char* SdCardManager::getCurrentMountPrefix() const { - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); if (currentPrefix.has_value()) { return currentPrefix.value().c_str(); } @@ -464,7 +464,7 @@ void SdCardManager::setPrintCommandOutput(bool print) { this->printCmdOutput = p bool SdCardManager::isSdCardUsable(std::optional sdCard) { { - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); if (markedUnusable) { return false; } @@ -560,7 +560,7 @@ ReturnValue_t SdCardManager::performFsck(sd::SdCard sdcard, bool printOutput, in } void SdCardManager::setActiveSdCard(sd::SdCard sdCard) { - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); sdInfo.active = sdCard; if (sdInfo.active == sd::SdCard::SLOT_0) { currentPrefix = config::SD_0_MOUNT_POINT; @@ -570,7 +570,7 @@ void SdCardManager::setActiveSdCard(sd::SdCard sdCard) { } std::optional SdCardManager::getActiveSdCard() const { - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); if (markedUnusable) { return std::nullopt; } @@ -578,6 +578,6 @@ std::optional SdCardManager::getActiveSdCard() const { } void SdCardManager::markUnusable() { - MutexGuard mg(mutex); + MutexGuard mg(sdLock, lockType, lockTimeout); markedUnusable = true; } diff --git a/bsp_q7s/fs/SdCardManager.h b/bsp_q7s/fs/SdCardManager.h index 77055589..1b538dad 100644 --- a/bsp_q7s/fs/SdCardManager.h +++ b/bsp_q7s/fs/SdCardManager.h @@ -223,7 +223,9 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { bool sdCardActive = true; bool printCmdOutput = true; bool markedUnusable = false; - MutexIF* mutex = nullptr; + MutexIF* sdLock = nullptr; + MutexIF::TimeoutType lockType = MutexIF::TimeoutType::WAITING; + uint32_t lockTimeout = 40; SdCardManager(); diff --git a/fsfw b/fsfw index 511d07c0..f8409754 160000 --- a/fsfw +++ b/fsfw @@ -1 +1 @@ -Subproject commit 511d07c0c78de7b1850e341dfcf8be7589f3c523 +Subproject commit f84097543e59a3564eae4ac19b7118102728c8a9 diff --git a/mission/config/torquer.h b/mission/config/torquer.h index 10a27991..8241eb83 100644 --- a/mission/config/torquer.h +++ b/mission/config/torquer.h @@ -9,6 +9,8 @@ namespace torquer { // Additional buffer time to accont for time until I2C command arrives and ramp up / ramp down // time of the MGT static constexpr dur_millis_t TORQUE_BUFFER_TIME_MS = 20; +static constexpr MutexIF::TimeoutType LOCK_TYPE = MutexIF::TimeoutType::WAITING; +static constexpr uint32_t LOCK_TIMEOUT = 20; MutexIF* lazyLock(); extern bool TORQUEING; diff --git a/mission/controller/AcsController.cpp b/mission/controller/AcsController.cpp index c0853695..5aa12523 100644 --- a/mission/controller/AcsController.cpp +++ b/mission/controller/AcsController.cpp @@ -429,7 +429,7 @@ ReturnValue_t AcsController::commandActuators(int16_t xDipole, int16_t yDipole, uint16_t rampTime) { { PoolReadGuard pg(&dipoleSet); - MutexGuard mg(torquer::lazyLock()); + MutexGuard mg(torquer::lazyLock(), torquer::LOCK_TYPE, torquer::LOCK_TIMEOUT); torquer::NEW_ACTUATION_FLAG = true; dipoleSet.setDipoles(xDipole, yDipole, zDipole, dipoleTorqueDuration); } diff --git a/mission/devices/ImtqHandler.cpp b/mission/devices/ImtqHandler.cpp index 5c5bdb61..fadf5a63 100644 --- a/mission/devices/ImtqHandler.cpp +++ b/mission/devices/ImtqHandler.cpp @@ -214,7 +214,7 @@ ReturnValue_t ImtqHandler::buildCommandFromCommand(DeviceCommandId_t deviceComma << ", y = " << dipoleSet.yDipole.value << ", z = " << dipoleSet.zDipole.value << ", duration = " << dipoleSet.currentTorqueDurationMs.value << std::endl; } - MutexGuard mg(torquer::lazyLock()); + MutexGuard mg(torquer::lazyLock(), torquer::LOCK_TYPE, torquer::LOCK_TIMEOUT); torquer::TORQUEING = true; torquer::TORQUE_COUNTDOWN.setTimeout(dipoleSet.currentTorqueDurationMs.value); rawPacket = commandBuffer; diff --git a/mission/devices/PcduHandler.cpp b/mission/devices/PcduHandler.cpp index 46106796..5ed227cd 100644 --- a/mission/devices/PcduHandler.cpp +++ b/mission/devices/PcduHandler.cpp @@ -18,7 +18,7 @@ PCDUHandler::PCDUHandler(object_id_t setObjectId, size_t cmdQueueSize) auto mqArgs = MqArgs(setObjectId, static_cast(this)); commandQueue = QueueFactory::instance()->createMessageQueue( cmdQueueSize, MessageQueueMessage::MAX_MESSAGE_SIZE, &mqArgs); - pwrMutex = MutexFactory::instance()->createMutex(); + pwrLock = MutexFactory::instance()->createMutex(); } PCDUHandler::~PCDUHandler() {} @@ -41,7 +41,7 @@ ReturnValue_t PCDUHandler::performOperation(uint8_t counter) { if (pg.getReadResult() == returnvalue::OK) { if (switcherSet.p60Dock5VStack.value != switchState) { triggerEvent(power::SWITCH_HAS_CHANGED, switchState, pcdu::Switches::P60_DOCK_5V_STACK); - MutexGuard mg(pwrMutex); + MutexGuard mg(pwrLock); switchStates[pcdu::P60_DOCK_5V_STACK] = switchState; } switcherSet.p60Dock5VStack.setValid(true); @@ -179,7 +179,7 @@ void PCDUHandler::updatePdu2SwitchStates() { switcherSet.pdu2Switches[idx] = pdu2CoreHk.outputEnables[idx]; } switcherSet.pdu2Switches.setValid(true); - MutexGuard mg(pwrMutex); + MutexGuard mg(pwrLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX); checkAndUpdatePduSwitch(pdu, Switches::PDU2_CH0_Q7S, pdu2CoreHk.outputEnables[Channels::Q7S]); checkAndUpdatePduSwitch(pdu, Switches::PDU2_CH1_PL_PCDU_BATT_0_14V8, @@ -216,7 +216,7 @@ void PCDUHandler::updatePdu1SwitchStates() { switcherSet.pdu1Switches[idx] = pdu1CoreHk.outputEnables[idx]; } switcherSet.pdu1Switches.setValid(true); - MutexGuard mg(pwrMutex); + MutexGuard mg(pwrLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX); checkAndUpdatePduSwitch(pdu, Switches::PDU1_CH0_TCS_BOARD_3V3, pdu1CoreHk.outputEnables[Channels::TCS_BOARD_3V3]); checkAndUpdatePduSwitch(pdu, Switches::PDU1_CH1_SYRLINKS_12V, @@ -402,9 +402,11 @@ ReturnValue_t PCDUHandler::getSwitchState(uint8_t switchNr) const { sif::debug << "PCDUHandler::getSwitchState: Invalid switch number" << std::endl; return returnvalue::FAILED; } - pwrMutex->lockMutex(); - uint8_t currentState = switchStates[switchNr]; - pwrMutex->unlockMutex(); + uint8_t currentState = 0; + { + MutexGuard mg(pwrLock, LOCK_TYPE, LOCK_TIMEOUT, LOCK_CTX); + currentState = switchStates[switchNr]; + } if (currentState == 1) { return PowerSwitchIF::SWITCH_ON; } else { diff --git a/mission/devices/PcduHandler.h b/mission/devices/PcduHandler.h index d1f3996b..45bbd392 100644 --- a/mission/devices/PcduHandler.h +++ b/mission/devices/PcduHandler.h @@ -51,7 +51,10 @@ class PCDUHandler : public PowerSwitchIF, private: uint32_t pstIntervalMs = 0; - MutexIF* pwrMutex = nullptr; + MutexIF* pwrLock = nullptr; + static constexpr MutexIF::TimeoutType LOCK_TYPE = MutexIF::TimeoutType::WAITING; + static constexpr uint32_t LOCK_TIMEOUT = 20; + static constexpr char LOCK_CTX[] = "PcduHandler"; /** Housekeeping manager. Handles updates of local pool variables. */ LocalDataPoolManager poolManager;