source: nscp/include/Mutex.h @ 8223547

0.4.00.4.10.4.2stable
Last change on this file since 8223547 was 8223547, checked in by Michael Medin <michael@…>, 8 years ago
  • Improved thread safety in Core + Added exception handling and logging in PDH code
  • Various fixes all over the place + New module for check size of files/directories
  • Changed build to Dynamic link
  • Added regular expression support for EventLog? checker
  • Property mode set to 100644
File size: 3.4 KB
Line 
1#pragma once
2
3#include "Singleton.h"
4#include "strEx.h"
5#include <assert.h>
6
7/**
8 * @ingroup NSClient++
9 * A simple class to wrap the W32 API mutex object.
10 *
11 * @version 1.0
12 * first version
13 *
14 * @date 02-14-2005
15 *
16 * @author mickem
17 *
18 * @par license
19 * This code is absolutely free to use and modify. The code is provided "as is" with
20 * no expressed or implied warranty. The author accepts no liability if it causes
21 * any damage to your computer, causes your pet to fall ill, increases baldness
22 * or makes your car start emitting strange noises when you start it up.
23 * This code has no bugs, just undocumented features!
24 *
25 * @todo
26 *
27 * @bug
28 *
29 */
30class MutexHandler {
31private:
32        HANDLE hMutex;
33public:
34        /**
35         * Default c-tor.
36         * Creates an unnamed mutex.
37         */
38        MutexHandler() : hMutex(NULL) {
39                hMutex = CreateMutex(NULL, FALSE, NULL);
40                if ( GetLastError() == ERROR_ALREADY_EXISTS )
41                        hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, NULL);
42                assert(hMutex != NULL);
43        }
44        /**
45         * Default d-tor.
46         * Destroys releases the mutex handle.
47         */
48        virtual ~MutexHandler() {
49                if (hMutex)
50                        CloseHandle(hMutex);
51                hMutex = NULL;
52        }
53        /**
54         * HANDLe cast operator to retrieve the handle from the enclosed mutex object.
55         * @return a handle to the mutex object.
56         */
57        operator HANDLE () const {
58                return hMutex;
59        }
60};
61
62/**
63 * @ingroup NSClient++
64 * A simple mutex-lock class that makes mutex management simple.
65 * By using the c-tor/d-tor to lock and release the mutex a block only needs to "contain" this object to be "safe".
66 *<pre>
67 * {
68 *   MutexLock mutex(mutexHandler);
69 *   if (!mutex.hasMutex()) {
70 *     // Do stuff thread safe...
71 *   }
72 * }
73 *</pre>
74 *
75 * @version 1.0
76 * first version
77 *
78 * @date 02-14-2005
79 *
80 * @author mickem
81 *
82 * @par license
83 * This code is absolutely free to use and modify. The code is provided "as is" with
84 * no expressed or implied warranty. The author accepts no liability if it causes
85 * any damage to your computer, causes your pet to fall ill, increases baldness
86 * or makes your car start emitting strange noises when you start it up.
87 * This code has no bugs, just undocumented features!
88 *
89 */
90class MutexLock {
91private:
92        bool bHasMutex;         // Status: true if we have a mutex lock
93        HANDLE hMutex_;         // Handle to the mutex object.
94        DWORD dwWaitResult;     // Result from the mutex operations
95public:
96        /**
97         * Default c-tor.
98         * Waits for the mutex object.
99         * @param hMutex The mutex to use
100         */
101        MutexLock(HANDLE hMutex, DWORD timeout = 5000L) : bHasMutex(false), hMutex_(hMutex) {
102                assert(hMutex_ != NULL);
103                dwWaitResult = WaitForSingleObject(hMutex_, timeout);
104                switch (dwWaitResult) {
105                        // The thread got mutex ownership.
106        case WAIT_OBJECT_0:
107                        bHasMutex = true;
108                        break;
109        case WAIT_TIMEOUT:
110                        bHasMutex = false;
111                        break;
112        case WAIT_ABANDONED:
113                        bHasMutex = false;
114                        break;
115                }
116        }
117        /**
118         * An attempt to simplify the has mutex thingy (don't know if it works, haven't tried it since I wrote this class a few years ago :)
119         * @return true if we have a mutex lock.
120         */
121        operator bool () const {
122                return bHasMutex;
123        }
124        /**
125         * Check if we actually got the mutex (might have timed out)
126         * @return
127         */
128        bool hasMutex() const {
129                return bHasMutex;
130        }
131        /**
132         * Default d-tor.
133         * Release the mutex
134         */
135        virtual ~MutexLock() {
136        ReleaseMutex(hMutex_);
137                bHasMutex = false;
138        }
139        /**
140         * Get the result of the wait operation.
141         * @return Result of the wait operation
142         */
143        DWORD getWaitResult() const {
144                return dwWaitResult;
145        }
146};
Note: See TracBrowser for help on using the repository browser.