source: nscp/include/pdh/thread_safe_impl.hpp @ f7a074d

0.4.00.4.10.4.2
Last change on this file since f7a074d was f0e6036, checked in by Michael Medin <michael@…>, 3 years ago

Reintegrated stable branch and removed some project files (no longer needed) (part 1 of many)

  • Property mode set to 100644
File size: 9.0 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
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     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#pragma once
22
23#include <pdh/core.hpp>
24#include <error.hpp>
25#include <pdh/basic_impl.hpp>
26#include <Mutex.h>
27
28namespace PDH {
29
30        class ThreadedSafePDH : public PDH::NativeExternalPDH {
31                MutexHandler mutex_;
32                typedef std::list<PDHImplSubscriber*> subscriber_list;
33                subscriber_list subscribers_;
34        private:
35
36        public:
37                ThreadedSafePDH() : NativeExternalPDH() {
38                }
39
40                bool reload() {
41                        MutexLock lock(mutex_);
42                        if (!lock.hasMutex())
43                                throw PDHException(_T("Failed to get mutex for reload"));
44                        return reload_unsafe();
45                }
46
47                bool reload_unsafe() {
48                        for(subscriber_list::const_iterator cit = subscribers_.begin(); cit != subscribers_.end(); ++cit)
49                                (*cit)->on_unload();
50                        unload_procs();
51                        load_procs();
52                        for(subscriber_list::const_iterator cit = subscribers_.begin(); cit != subscribers_.end(); ++cit)
53                                (*cit)->on_reload();
54                        return true;
55                }
56        public:
57
58                virtual void add_listener(PDHImplSubscriber* sub) {
59                        MutexLock lock(mutex_);
60                        if (!lock.hasMutex())
61                                throw PDHException(_T("Failed to get mutex for reload"));
62                        subscribers_.push_back(sub);
63                }
64                virtual void remove_listener(PDHImplSubscriber* sub) {
65                        MutexLock lock(mutex_);
66                        if (!lock.hasMutex())
67                                throw PDHException(_T("Failed to get mutex for reload"));
68                        for(subscriber_list::iterator it = subscribers_.begin(); it != subscribers_.end(); ++it) {
69                                if ( (*it) == sub)
70                                        it = subscribers_.erase(it);
71                                if (it == subscribers_.end())
72                                        break;
73                        }
74                }
75
76                virtual PDHError PdhLookupPerfIndexByName(LPCTSTR szMachineName,LPCTSTR szName,DWORD *dwIndex) {
77                        MutexLock lock(mutex_);
78                        if (!lock.hasMutex())
79                                throw PDHException(_T("Failed to get mutex for PdhLookupPerfIndexByName"));
80                        if (pPdhLookupPerfIndexByName == NULL)
81                                throw PDHException(_T("Failed to initalize PdhLookupPerfIndexByName"));
82                        return PDH::PDHError(pPdhLookupPerfIndexByName(szMachineName,szName,dwIndex));
83                }
84
85                virtual PDHError PdhLookupPerfNameByIndex(LPCTSTR szMachineName,DWORD dwNameIndex,LPTSTR szNameBuffer,LPDWORD pcchNameBufferSize) {
86                        MutexLock lock(mutex_);
87                        if (!lock.hasMutex())
88                                throw PDHException(_T("Failed to get mutex for PdhLookupPerfNameByIndex"));
89                        if (pPdhLookupPerfNameByIndex == NULL)
90                                throw PDHException(_T("Failed to initalize PdhLookupPerfNameByIndex :("));
91                        return PDH::PDHError(pPdhLookupPerfNameByIndex(szMachineName,dwNameIndex,szNameBuffer,pcchNameBufferSize));
92                }
93
94                virtual PDHError PdhExpandCounterPath(LPCTSTR szWildCardPath, LPTSTR mszExpandedPathList, LPDWORD pcchPathListLength) {
95                        MutexLock lock(mutex_);
96                        if (!lock.hasMutex())
97                                throw PDHException(_T("Failed to get mutex for PdhExpandCounterPath"));
98                        if (pPdhExpandCounterPath == NULL)
99                                throw PDHException(_T("Failed to initalize PdhLookupPerfNameByIndex :("));
100                        return PDH::PDHError(pPdhExpandCounterPath(szWildCardPath,mszExpandedPathList,pcchPathListLength));
101                }
102                virtual PDHError PdhGetCounterInfo(PDH::PDH_HCOUNTER hCounter, BOOLEAN bRetrieveExplainText, LPDWORD pdwBufferSize, PDH_COUNTER_INFO *lpBuffer) {
103                        MutexLock lock(mutex_);
104                        if (!lock.hasMutex())
105                                throw PDHException(_T("Failed to get mutex for PdhGetCounterInfo"));
106                        if (pPdhGetCounterInfo == NULL)
107                                throw PDHException(_T("Failed to initalize PdhGetCounterInfo :("));
108                        return PDH::PDHError(pPdhGetCounterInfo(hCounter,bRetrieveExplainText,pdwBufferSize,lpBuffer));
109                }
110                virtual PDHError PdhAddCounter(PDH::PDH_HQUERY hQuery, LPCWSTR szFullCounterPath, DWORD_PTR dwUserData, PDH::PDH_HCOUNTER * phCounter) {
111                        MutexLock lock(mutex_);
112                        if (!lock.hasMutex())
113                                throw PDHException(_T("Failed to get mutex for PdhAddCounter"));
114                        if (pPdhAddCounter == NULL)
115                                throw PDHException(_T("Failed to initalize PdhAddCounter :("));
116                        return PDH::PDHError(pPdhAddCounter(hQuery,szFullCounterPath,dwUserData,phCounter));
117                }
118                virtual PDHError PdhRemoveCounter(PDH::PDH_HCOUNTER hCounter) {
119                        MutexLock lock(mutex_);
120                        if (!lock.hasMutex())
121                                throw PDHException(_T("Failed to get mutex for PdhRemoveCounter"));
122                        if (pPdhRemoveCounter == NULL)
123                                throw PDHException(_T("Failed to initalize PdhRemoveCounter :("));
124                        return PDH::PDHError(pPdhRemoveCounter(hCounter));
125                }
126                virtual PDHError PdhGetFormattedCounterValue(PDH_HCOUNTER hCounter, DWORD dwFormat, LPDWORD lpdwType, PPDH_FMT_COUNTERVALUE pValue) {
127                        MutexLock lock(mutex_);
128                        if (!lock.hasMutex())
129                                throw PDHException(_T("Failed to get mutex for PdhGetFormattedCounterValue"));
130                        if (pPdhGetFormattedCounterValue == NULL)
131                                throw PDHException(_T("Failed to initalize PdhGetFormattedCounterValue :("));
132                        return PDH::PDHError(pPdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue));
133                }
134                virtual PDHError PdhOpenQuery(LPCWSTR szDataSource, DWORD_PTR dwUserData, PDH::PDH_HQUERY * phQuery) {
135                        MutexLock lock(mutex_);
136                        if (!lock.hasMutex())
137                                throw PDHException(_T("Failed to get mutex for PdhOpenQuery"));
138                        if (pPdhOpenQuery == NULL)
139                                throw PDHException(_T("Failed to initalize PdhOpenQuery :("));
140                        return PDH::PDHError(pPdhOpenQuery(szDataSource, dwUserData, phQuery));
141                }
142                virtual PDHError PdhCloseQuery(PDH_HQUERY hQuery) {
143                        MutexLock lock(mutex_);
144                        if (!lock.hasMutex())
145                                throw PDHException(_T("Failed to get mutex for PdhCloseQuery"));
146                        if (pPdhCloseQuery == NULL)
147                                throw PDHException(_T("Failed to initalize PdhCloseQuery :("));
148                        return PDH::PDHError(pPdhCloseQuery(hQuery));
149                }
150                virtual PDHError PdhCollectQueryData(PDH_HQUERY hQuery) {
151                        MutexLock lock(mutex_);
152                        if (!lock.hasMutex())
153                                throw PDHException(_T("Failed to get mutex for PdhCollectQueryData"));
154                        if (pPdhCollectQueryData == NULL)
155                                throw PDHException(_T("Failed to initalize PdhCollectQueryData :("));
156                        return PDH::PDHError(pPdhCollectQueryData(hQuery));
157                }
158                virtual PDHError PdhValidatePath(LPCWSTR szFullPathBuffer, bool force_reload) {
159                        MutexLock lock(mutex_);
160                        if (!lock.hasMutex())
161                                throw PDHException(_T("Failed to get mutex for PdhValidatePath"));
162                        if (pPdhValidatePath == NULL)
163                                throw PDHException(_T("Failed to initalize PdhValidatePath :("));
164                        PDH::PDHError status = PDH::PDHError(pPdhValidatePath(szFullPathBuffer));
165                        if (status.is_error() && force_reload) {
166                                reload_unsafe();
167                                status = PDH::PDHError(pPdhValidatePath(szFullPathBuffer));
168                        }
169                        return status;
170                }
171                virtual PDHError PdhEnumObjects(LPCWSTR szDataSource, LPCWSTR szMachineName, LPWSTR mszObjectList, LPDWORD pcchBufferSize, DWORD dwDetailLevel, BOOL bRefresh) {
172                        MutexLock lock(mutex_);
173                        if (!lock.hasMutex())
174                                throw PDHException(_T("Failed to get mutex for PdhEnumObjects"));
175                        if (pPdhEnumObjects == NULL)
176                                throw PDHException(_T("Failed to initalize PdhEnumObjects :("));
177                        return PDH::PDHError(pPdhEnumObjects(szDataSource, szMachineName, mszObjectList, pcchBufferSize, dwDetailLevel, bRefresh));
178                }
179                virtual PDHError PdhEnumObjectItems(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName, LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList, LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags) {
180                        MutexLock lock(mutex_);
181                        if (!lock.hasMutex())
182                                throw PDHException(_T("Failed to get mutex for PdhEnumObjectItems"));
183                        if (pPdhEnumObjectItems == NULL)
184                                throw PDHException(_T("Failed to initalize PdhEnumObjectItems :("));
185                        return PDH::PDHError(pPdhEnumObjectItems(szDataSource, szMachineName, szObjectName, mszCounterList, pcchCounterListLength, mszInstanceList, pcchInstanceListLength, dwDetailLevel, dwFlags));
186                }
187        };
188}
Note: See TracBrowser for help on using the repository browser.