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.
This commit is contained in:
parent
fb53e209b5
commit
2243c77f4b
@ -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!)
|
||||
|
@ -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
|
||||
|
@ -22,6 +22,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <QThread>
|
||||
#include <QSemaphore>
|
||||
|
||||
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);
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <QThread>
|
||||
#include <QSemaphore>
|
||||
|
||||
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;
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <QThread>
|
||||
#include <QSemaphore>
|
||||
|
||||
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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user