OSDN Git Service

Some improvements and simplifications to error handling functions.
[lamexp/LameXP.git] / src / Thread_RAMObserver.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Thread_RAMObserver.h"
24 #include "Global.h"
25
26 #include <QDir>
27
28 //Windows includes
29 #define NOMINMAX
30 #define WIN32_LEAN_AND_MEAN
31 #include <Windows.h>
32
33 ////////////////////////////////////////////////////////////
34 // Constructor & Destructor
35 ////////////////////////////////////////////////////////////
36
37 RAMObserverThread::RAMObserverThread(void)
38 {
39 }
40
41 RAMObserverThread::~RAMObserverThread(void)
42 {
43 }
44
45 ////////////////////////////////////////////////////////////
46 // Protected functions
47 ////////////////////////////////////////////////////////////
48
49 void RAMObserverThread::run(void)
50 {
51         qDebug("RAM observer started!");
52
53         try
54         {
55                 observe();
56         }
57         catch(const std::exception &error)
58         {
59                 PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
60                 lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
61         }
62         catch(...)
63         {
64                 PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
65                 lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
66         }
67
68         while(m_semaphore.available()) m_semaphore.tryAcquire();
69 }
70
71 void RAMObserverThread::observe(void)
72 {
73         MEMORYSTATUSEX memoryStatus;
74         double previous = -1.0;
75
76         forever
77         {
78                 memset(&memoryStatus, 0, sizeof(MEMORYSTATUSEX));
79                 memoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
80                 
81                 if(GlobalMemoryStatusEx(&memoryStatus))
82                 {
83                         double current = static_cast<double>(memoryStatus.dwMemoryLoad) / 100.0;
84                         if(current != previous)
85                         {
86                                 emit currentUsageChanged(current);
87                                 previous = current;
88                         }
89                 }
90                 if(m_semaphore.tryAcquire(1, 2000)) break;
91         }
92 }
93
94 ////////////////////////////////////////////////////////////
95 // SLOTS
96 ////////////////////////////////////////////////////////////
97
98 /*NONE*/
99
100 ////////////////////////////////////////////////////////////
101 // EVENTS
102 ////////////////////////////////////////////////////////////
103
104 /*NONE*/