Sync DrDump crash handler with TortoiseSVN codebase
[TortoiseGit.git] / ext / CrashServer / CommonLibs / Log / synchro.h
blobee1cfcd4a87c9452bf1eaa4fd608f9f9d61d93a8
1 // Copyright 2014 Idol Software, Inc.
2 //
3 // This file is part of Doctor Dump SDK.
4 //
5 // Doctor Dump SDK is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef _SYNCHRO_H__INCLUDE_
19 #define _SYNCHRO_H__INCLUDE_
21 #include <windows.h>
23 class Handle
25 protected:
26 HANDLE m_hHandle;
27 public:
28 Handle(HANDLE hHandle = NULL): m_hHandle(hHandle) {}
29 ~Handle() { Close(); }
30 operator HANDLE() { return m_hHandle; }
31 Handle& operator = (HANDLE hHandle) { Close(); m_hHandle = hHandle; return *this; }
32 void Close() { if (m_hHandle != NULL) { CloseHandle(m_hHandle); m_hHandle = NULL; } }
35 class FileHandle
37 protected:
38 HANDLE m_hHandle;
39 public:
40 FileHandle(HANDLE hHandle = INVALID_HANDLE_VALUE): m_hHandle(hHandle) {}
41 ~FileHandle() { Close(); }
42 operator HANDLE() const { return m_hHandle; }
43 FileHandle& operator = (HANDLE hHandle) { Close(); m_hHandle = hHandle; return *this; }
44 void Close() { if (m_hHandle != INVALID_HANDLE_VALUE) { CloseHandle(m_hHandle); m_hHandle = INVALID_HANDLE_VALUE; } }
48 class Event: public Handle
50 public:
51 Event(LPSECURITY_ATTRIBUTES pAttr = NULL, bool bManual = true, bool bState = false, LPCTSTR pName = NULL)
52 : Handle( CreateEvent(pAttr, bManual, bState, pName)) {}
53 void Set() { SetEvent(m_hHandle); }
54 void Reset() { ResetEvent(m_hHandle); }
57 template <class T>
58 class SyncLockT
60 public:
61 SyncLockT(SyncLockT& lock): m_SyncObj(lock.m_SyncObj) { m_SyncObj.Lock(); }
62 SyncLockT(T& SyncObj): m_SyncObj(SyncObj) { m_SyncObj.Lock(); }
63 ~SyncLockT() { m_SyncObj.Unlock(); }
64 private:
65 SyncLockT &operator=(const SyncLockT& lock);
66 T& m_SyncObj;
69 class CriticalSection
71 public:
72 CriticalSection() { InitializeCriticalSection(&m_cs); }
73 ~CriticalSection() { DeleteCriticalSection(&m_cs); }
74 void Lock() { EnterCriticalSection(&m_cs); }
75 void Unlock() { LeaveCriticalSection(&m_cs); }
77 typedef SyncLockT<CriticalSection> SyncLock;
78 private:
79 CRITICAL_SECTION m_cs;
82 #endif