From 2243c77f4b08c8ad144c7f38eec0bb6b94074161 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Fri, 25 Nov 2011 01:01:30 +0100 Subject: [PATCH] Now using a QSemaphore to make the observer threads sleep. This allows waking up the threads immediately *without* having to poll the 'terminated' flag at short intervals. --- src/Config.h | 2 +- src/Thread_CPUObserver.cpp | 11 ++++------- src/Thread_CPUObserver.h | 5 +++-- src/Thread_DiskObserver.cpp | 11 ++++------- src/Thread_DiskObserver.h | 5 +++-- src/Thread_RAMObserver.cpp | 11 ++++------- src/Thread_RAMObserver.h | 5 +++-- 7 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/Config.h b/src/Config.h index 79b8482f..7d663cee 100644 --- a/src/Config.h +++ b/src/Config.h @@ -30,7 +30,7 @@ #define VER_LAMEXP_MINOR_LO 4 #define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_PATCH 3 -#define VER_LAMEXP_BUILD 786 +#define VER_LAMEXP_BUILD 787 /////////////////////////////////////////////////////////////////////////////// // Tool versions (minimum expected versions!) diff --git a/src/Thread_CPUObserver.cpp b/src/Thread_CPUObserver.cpp index 4bb95674..a2a5edb6 100644 --- a/src/Thread_CPUObserver.cpp +++ b/src/Thread_CPUObserver.cpp @@ -33,7 +33,6 @@ typedef BOOL (WINAPI *GetSystemTimesPtr)(LPFILETIME lpIdleTime, LPFILETIME lpKer CPUObserverThread::CPUObserverThread(void) { - m_terminated = false; } CPUObserverThread::~CPUObserverThread(void) @@ -47,7 +46,6 @@ CPUObserverThread::~CPUObserverThread(void) void CPUObserverThread::run(void) { qDebug("CPU observer started!"); - m_terminated = false; try { @@ -61,6 +59,8 @@ void CPUObserverThread::run(void) FatalAppExit(0, L"Unhandeled exception error, application will exit!"); TerminateProcess(GetCurrentProcess(), -1); } + + while(m_semaphore.available()) m_semaphore.tryAcquire(); } ULONGLONG CPUObserverThread::filetime2ulonglong(const void *ftime) @@ -94,7 +94,7 @@ void CPUObserverThread::observe(void) sys[i] = 0; usr[i] = 0; idl[i] = 0; } - while(!m_terminated) + forever { if(getSystemTimes(&idlTime, &sysTime, &usrTime)) { @@ -127,10 +127,7 @@ void CPUObserverThread::observe(void) } } } - for(int i = 0; i < 6; i++) - { - if(!m_terminated) msleep(333); - } + if(m_semaphore.tryAcquire(1, 2000)) break; } } else diff --git a/src/Thread_CPUObserver.h b/src/Thread_CPUObserver.h index dda416ee..e656b2bf 100644 --- a/src/Thread_CPUObserver.h +++ b/src/Thread_CPUObserver.h @@ -22,6 +22,7 @@ #pragma once #include +#include class CPUObserverThread: public QThread { @@ -31,7 +32,7 @@ public: CPUObserverThread(void); ~CPUObserverThread(void); - void stop(void) { m_terminated = true; } + void stop(void) { m_semaphore.release(); } protected: void run(void); @@ -41,6 +42,6 @@ signals: void currentUsageChanged(const double usage); private: - volatile bool m_terminated; + QSemaphore m_semaphore; static inline unsigned __int64 filetime2ulonglong(const void *ftime); }; diff --git a/src/Thread_DiskObserver.cpp b/src/Thread_DiskObserver.cpp index d511332b..266f666e 100644 --- a/src/Thread_DiskObserver.cpp +++ b/src/Thread_DiskObserver.cpp @@ -35,7 +35,6 @@ DiskObserverThread::DiskObserverThread(const QString &path) : m_path(makeRootDir(path)) { - m_terminated = false; } DiskObserverThread::~DiskObserverThread(void) @@ -49,7 +48,6 @@ DiskObserverThread::~DiskObserverThread(void) void DiskObserverThread::run(void) { qDebug("DiskSpace observer started!"); - m_terminated = false; try { @@ -63,6 +61,8 @@ void DiskObserverThread::run(void) FatalAppExit(0, L"Unhandeled exception error, application will exit!"); TerminateProcess(GetCurrentProcess(), -1); } + + while(m_semaphore.available()) m_semaphore.tryAcquire(); } void DiskObserverThread::observe(void) @@ -71,7 +71,7 @@ void DiskObserverThread::observe(void) unsigned __int64 freeSpace, previousSpace = 0ui64; bool ok = false; - while(!m_terminated) + forever { freeSpace = lamexp_free_diskspace(m_path, &ok); if(ok) @@ -88,10 +88,7 @@ void DiskObserverThread::observe(void) previousSpace = freeSpace; } } - for(int i = 0; i < 6; i++) - { - if(!m_terminated) msleep(333); - } + if(m_semaphore.tryAcquire(1, 2000)) break; } } diff --git a/src/Thread_DiskObserver.h b/src/Thread_DiskObserver.h index 1bf4a0c2..f7ffb015 100644 --- a/src/Thread_DiskObserver.h +++ b/src/Thread_DiskObserver.h @@ -22,6 +22,7 @@ #pragma once #include +#include class DiskObserverThread: public QThread { @@ -31,7 +32,7 @@ public: DiskObserverThread(const QString &path); ~DiskObserverThread(void); - void stop(void) { m_terminated = true; } + void stop(void) { m_semaphore.release(); } protected: void run(void); @@ -44,6 +45,6 @@ signals: void freeSpaceChanged(const quint64); private: - volatile bool m_terminated; + QSemaphore m_semaphore; const QString m_path; }; diff --git a/src/Thread_RAMObserver.cpp b/src/Thread_RAMObserver.cpp index 44ad40a4..b9fc4682 100644 --- a/src/Thread_RAMObserver.cpp +++ b/src/Thread_RAMObserver.cpp @@ -30,7 +30,6 @@ RAMObserverThread::RAMObserverThread(void) { - m_terminated = false; } RAMObserverThread::~RAMObserverThread(void) @@ -44,7 +43,6 @@ RAMObserverThread::~RAMObserverThread(void) void RAMObserverThread::run(void) { qDebug("RAM observer started!"); - m_terminated = false; try { @@ -58,6 +56,8 @@ void RAMObserverThread::run(void) FatalAppExit(0, L"Unhandeled exception error, application will exit!"); TerminateProcess(GetCurrentProcess(), -1); } + + while(m_semaphore.available()) m_semaphore.tryAcquire(); } void RAMObserverThread::observe(void) @@ -65,7 +65,7 @@ void RAMObserverThread::observe(void) MEMORYSTATUSEX memoryStatus; double previous = -1.0; - while(!m_terminated) + forever { memset(&memoryStatus, 0, sizeof(MEMORYSTATUSEX)); memoryStatus.dwLength = sizeof(MEMORYSTATUSEX); @@ -79,10 +79,7 @@ void RAMObserverThread::observe(void) previous = current; } } - for(int i = 0; i < 6; i++) - { - if(!m_terminated) msleep(333); - } + if(m_semaphore.tryAcquire(1, 2000)) break; } } diff --git a/src/Thread_RAMObserver.h b/src/Thread_RAMObserver.h index 06585f24..c4723971 100644 --- a/src/Thread_RAMObserver.h +++ b/src/Thread_RAMObserver.h @@ -22,6 +22,7 @@ #pragma once #include +#include class RAMObserverThread: public QThread { @@ -31,7 +32,7 @@ public: RAMObserverThread(void); ~RAMObserverThread(void); - void stop(void) { m_terminated = true; } + void stop(void) { m_semaphore.release(); } protected: void run(void); @@ -41,5 +42,5 @@ signals: void currentUsageChanged(const double usage); private: - volatile bool m_terminated; + QSemaphore m_semaphore; };