From 4b0ec380b16f1e1d433a822a6345650059502ac6 Mon Sep 17 00:00:00 2001 From: localhorst Date: Wed, 10 Dec 2025 21:15:39 +0100 Subject: [PATCH] fix atomic --- include/drive.h | 20 +++++++++-- makefile | 2 +- src/drive.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ src/reHDD.cpp | 16 +++++---- src/shred.cpp | 4 ++- src/tui.cpp | 23 +++++++++---- 6 files changed, 134 insertions(+), 19 deletions(-) diff --git a/include/drive.h b/include/drive.h index b766f5c..ffae208 100644 --- a/include/drive.h +++ b/include/drive.h @@ -33,16 +33,18 @@ public: NVME }; - struct + struct ShredSpeed { time_t u32ShredTimeDelta; - std::chrono::time_point chronoShredTimestamp; + std::chrono::time_point + chronoShredTimestamp; unsigned long ulWrittenBytes; unsigned long ulSpeedMetricBytesWritten; - } sShredSpeed; + }; std::atomic state; std::atomic connectionType; + std::atomic sShredSpeed; bool bWasShredded = false; // all shred iterations done bool bWasShredStarted = false; // shred was atleast once started @@ -76,6 +78,18 @@ private: protected: public: + // Copy constructor + Drive(const Drive &other); + + // Copy assignment operator + Drive &operator=(const Drive &other); + + // Move constructor + Drive(Drive &&other) noexcept; + + // Move assignment operator + Drive &operator=(Drive &&other) noexcept; + Drive(std::string path) { this->sPath = path; diff --git a/makefile b/makefile index 8d1f7fd..6abaa7e 100644 --- a/makefile +++ b/makefile @@ -18,7 +18,7 @@ DCOMPILE_FLAGS = -D DEBUG # Add additional include paths INCLUDES = include # General linker settings -LINK_FLAGS = -Llib -lpthread -lncurses -ltfng +LINK_FLAGS = -Llib -lpthread -lncurses -ltfng -latomic # Doc DOCDIR = doc diff --git a/src/drive.cpp b/src/drive.cpp index 71af4da..c1683ce 100644 --- a/src/drive.cpp +++ b/src/drive.cpp @@ -8,6 +8,94 @@ #include "../include/reHDD.h" using namespace std; +// Copy constructor +Drive::Drive(const Drive &other) + : state(other.state.load()), + connectionType(other.connectionType.load()), + sShredSpeed(other.sShredSpeed.load()), + bWasShredded(other.bWasShredded), + bWasShredStarted(other.bWasShredStarted), + bWasChecked(other.bWasChecked), + bWasDeleted(other.bWasDeleted), + bIsOffline(other.bIsOffline), + u32DriveChecksumAfterShredding(other.u32DriveChecksumAfterShredding), + sPath(other.sPath), + u32Timestamp(other.u32Timestamp), + d32TaskPercentage(other.d32TaskPercentage), + u32TimestampTaskStart(other.u32TimestampTaskStart), + u32TaskDuration(other.u32TaskDuration), + sSmartData(other.sSmartData) +{ +} + +// Copy assignment operator +Drive &Drive::operator=(const Drive &other) +{ + if (this != &other) + { + state = other.state.load(); + connectionType = other.connectionType.load(); + sShredSpeed = other.sShredSpeed.load(); + bWasShredded = other.bWasShredded; + bWasShredStarted = other.bWasShredStarted; + bWasChecked = other.bWasChecked; + bWasDeleted = other.bWasDeleted; + bIsOffline = other.bIsOffline; + u32DriveChecksumAfterShredding = other.u32DriveChecksumAfterShredding; + sPath = other.sPath; + u32Timestamp = other.u32Timestamp; + d32TaskPercentage = other.d32TaskPercentage; + u32TimestampTaskStart = other.u32TimestampTaskStart; + u32TaskDuration = other.u32TaskDuration; + sSmartData = other.sSmartData; + } + return *this; +} + +// Move constructor +Drive::Drive(Drive &&other) noexcept + : state(other.state.load()), + connectionType(other.connectionType.load()), + sShredSpeed(other.sShredSpeed.load()), + bWasShredded(other.bWasShredded), + bWasShredStarted(other.bWasShredStarted), + bWasChecked(other.bWasChecked), + bWasDeleted(other.bWasDeleted), + bIsOffline(other.bIsOffline), + u32DriveChecksumAfterShredding(other.u32DriveChecksumAfterShredding), + sPath(std::move(other.sPath)), + u32Timestamp(other.u32Timestamp), + d32TaskPercentage(other.d32TaskPercentage), + u32TimestampTaskStart(other.u32TimestampTaskStart), + u32TaskDuration(other.u32TaskDuration), + sSmartData(std::move(other.sSmartData)) +{ +} + +// Move assignment operator +Drive &Drive::operator=(Drive &&other) noexcept +{ + if (this != &other) + { + state = other.state.load(); + connectionType = other.connectionType.load(); + sShredSpeed = other.sShredSpeed.load(); + bWasShredded = other.bWasShredded; + bWasShredStarted = other.bWasShredStarted; + bWasChecked = other.bWasChecked; + bWasDeleted = other.bWasDeleted; + bIsOffline = other.bIsOffline; + u32DriveChecksumAfterShredding = other.u32DriveChecksumAfterShredding; + sPath = std::move(other.sPath); + u32Timestamp = other.u32Timestamp; + d32TaskPercentage = other.d32TaskPercentage; + u32TimestampTaskStart = other.u32TimestampTaskStart; + u32TaskDuration = other.u32TaskDuration; + sSmartData = std::move(other.sSmartData); + } + return *this; +} + string Drive::getPath(void) { return sPath; diff --git a/src/reHDD.cpp b/src/reHDD.cpp index abd5ab1..57cd30a 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -359,8 +359,8 @@ void reHDD::searchDrives(std::list *plistDrives) "Drive found: " + tmpDrive.getPath() + " (type: " + (tmpDrive.connectionType == Drive::ConnectionType::USB ? "USB" : tmpDrive.connectionType == Drive::ConnectionType::SATA ? "SATA" - : tmpDrive.connectionType == Drive::ConnectionType::NVME ? "NVME" - : "UNKNOWN") + + : tmpDrive.connectionType == Drive::ConnectionType::NVME ? "NVME" + : "UNKNOWN") + ")"); } @@ -571,13 +571,15 @@ void reHDD::updateShredMetrics(list *plistDrives) Drive *pTmpDrive = iterator_to_pointer::iterator>(it); // set metrics for calculating shred speed std::chrono::time_point chronoCurrentTimestamp = std::chrono::system_clock::now(); - time_t u32ShredTimeDelta = (chronoCurrentTimestamp - pTmpDrive->sShredSpeed.chronoShredTimestamp).count(); + auto shredSpeed = pTmpDrive->sShredSpeed.load(); + time_t u32ShredTimeDelta = (chronoCurrentTimestamp - shredSpeed.chronoShredTimestamp).count(); if (u32ShredTimeDelta > METRIC_THRESHOLD) { - pTmpDrive->sShredSpeed.u32ShredTimeDelta = u32ShredTimeDelta; - pTmpDrive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now(); - pTmpDrive->sShredSpeed.ulWrittenBytes = pTmpDrive->sShredSpeed.ulSpeedMetricBytesWritten; - pTmpDrive->sShredSpeed.ulSpeedMetricBytesWritten = 0U; + shredSpeed.u32ShredTimeDelta = u32ShredTimeDelta; + shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now(); + shredSpeed.ulWrittenBytes = shredSpeed.ulSpeedMetricBytesWritten; + shredSpeed.ulSpeedMetricBytesWritten = 0U; + pTmpDrive->sShredSpeed.store(shredSpeed); } } } diff --git a/src/shred.cpp b/src/shred.cpp index 85d4184..99c3b36 100644 --- a/src/shred.cpp +++ b/src/shred.cpp @@ -286,7 +286,9 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig ulDriveByteCounter += iReadBytes; ulDriveByteOverallCount += iReadBytes; d32Percent = this->calcProgress(); - drive->sShredSpeed.ulSpeedMetricBytesWritten += iReadBytes; + auto shredSpeed = drive->sShredSpeed.load(); + shredSpeed.ulSpeedMetricBytesWritten += iReadBytes; + drive->sShredSpeed.store(shredSpeed); #ifdef LOG_LEVEL_HIGH Logger::logThis()->info("Shred-Task (Checksum): ByteCount: " + to_string(ulDriveByteCounter) + " - progress: " + to_string(d32Percent) + " - Drive: " + drive->getSerial()); diff --git a/src/tui.cpp b/src/tui.cpp index cebc317..c70469c 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -99,9 +99,9 @@ void TUI::updateTUI(list *plistDrives, uint8_t u8SelectedEntry) string sSpeed = " "; string sTime = " "; string sTemp = it->sTemperatureToText(); - string sConnection = (it->connectionType == Drive::USB ? "USB" : it->connectionType == Drive::SATA ? "SATA" - : it->connectionType == Drive::NVME ? "NVME" - : ""); + string sConnection = (it->connectionType == Drive::ConnectionType::USB ? "USB" : it->connectionType == Drive::ConnectionType::SATA ? "SATA" + : it->connectionType == Drive::ConnectionType::NVME ? "NVME" + : ""); bool bSelectedEntry = false; @@ -123,29 +123,38 @@ void TUI::updateTUI(list *plistDrives, uint8_t u8SelectedEntry) switch (it->state) { case Drive::TaskState::SHRED_ACTIVE: - + { stream << fixed << setprecision(3) << (it->getTaskPercentage()); sState = "Shredding: " + stream.str() + "%"; it->calculateTaskDuration(); sTime = this->formatTimeDuration(it->getTaskDuration()); - sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes); + auto shredSpeed = it->sShredSpeed.load(); + sSpeed = this->formatSpeed(shredSpeed.u32ShredTimeDelta, shredSpeed.ulWrittenBytes); break; + } case Drive::TaskState::CHECK_ACTIVE: + { stream << fixed << setprecision(3) << (it->getTaskPercentage()); sState = "Checking: " + stream.str() + "%"; it->calculateTaskDuration(); sTime = this->formatTimeDuration(it->getTaskDuration()); - sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes); + auto shredSpeed = it->sShredSpeed.load(); + sSpeed = this->formatSpeed(shredSpeed.u32ShredTimeDelta, shredSpeed.ulWrittenBytes); break; + } case Drive::TaskState::DELETE_ACTIVE: + { + sState = "Deleting ..."; it->calculateTaskDuration(); sTime = this->formatTimeDuration(it->getTaskDuration()); break; + } case Drive::TaskState::NONE: case Drive::TaskState::SHRED_SELECTED: case Drive::TaskState::DELETE_SELECTED: + { if (it->bWasDeleted) { sState = "DELETED"; // mark drive as deleted previously @@ -172,8 +181,8 @@ void TUI::updateTUI(list *plistDrives, uint8_t u8SelectedEntry) wrefresh(dialog); } #endif - break; + } case Drive::TaskState::FROZEN: stream << fixed << setprecision(3) << (it->getTaskPercentage()); #ifdef FROZEN_ALERT