diff --git a/plugins/inmemoryscanner/src/lib/Scanner.cpp b/plugins/inmemoryscanner/src/lib/Scanner.cpp index 9c11a7a1..c424367a 100644 --- a/plugins/inmemoryscanner/src/lib/Scanner.cpp +++ b/plugins/inmemoryscanner/src/lib/Scanner.cpp @@ -132,9 +132,9 @@ namespace InMemoryScanner // The semaphore protects the yara rules from being accessed more than YR_MAX_THREADS (32 atm.) times in // parallel. - semaphore.wait(); + semaphore.acquire(); auto results = yaraInterface->scanMemory(memoryRegionDescriptor.base, mappedRegions); - semaphore.notify(); + semaphore.release(); logger->debug("End scanMemory"); diff --git a/plugins/inmemoryscanner/src/lib/Scanner.h b/plugins/inmemoryscanner/src/lib/Scanner.h index 57a6af70..b0932ff4 100644 --- a/plugins/inmemoryscanner/src/lib/Scanner.h +++ b/plugins/inmemoryscanner/src/lib/Scanner.h @@ -4,9 +4,8 @@ #include "Dumping.h" #include "IYaraInterface.h" #include "OutputXML.h" -#include "Semaphore.h" -#include #include +#include #include #include #include // NOLINT(modernize-deprecated-headers) @@ -37,8 +36,7 @@ namespace InMemoryScanner std::unique_ptr dumping; std::unique_ptr logger; std::unique_ptr inMemResultsLogger; - Semaphore semaphore = - Semaphore(YR_MAX_THREADS); + std::counting_semaphore<> semaphore{YR_MAX_THREADS}; [[nodiscard]] bool shouldRegionBeScanned(const VmiCore::MemoryRegion& memoryRegionDescriptor); diff --git a/plugins/inmemoryscanner/src/lib/Semaphore.h b/plugins/inmemoryscanner/src/lib/Semaphore.h deleted file mode 100644 index 751d8fbf..00000000 --- a/plugins/inmemoryscanner/src/lib/Semaphore.h +++ /dev/null @@ -1,58 +0,0 @@ -#include - -namespace InMemoryScanner -{ - template class Semaphore - { - public: - explicit Semaphore(size_t count); - - Semaphore(const Semaphore&) = delete; - - Semaphore(Semaphore&&) = delete; - - Semaphore& operator=(const Semaphore&) = delete; - - Semaphore& operator=(Semaphore&&) = delete; - - void notify(); - - void wait(); - - bool try_wait(); - - private: - Mutex mMutex; - CondVar mCv; - size_t mCount; - }; - - template Semaphore::Semaphore(size_t count) : mCount{count} {} - - template void Semaphore::notify() - { - std::lock_guard lock{mMutex}; - ++mCount; - mCv.notify_one(); - } - - template void Semaphore::wait() - { - std::unique_lock lock{mMutex}; - mCv.wait(lock, [&] { return mCount > 0; }); - --mCount; - } - - template bool Semaphore::try_wait() - { - std::lock_guard lock{mMutex}; - - if (mCount > 0) - { - --mCount; - return true; - } - - return false; - } -}