blob: 45b881e221aa6a3074a937b75c13d588a5c8e5aa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/*
Copyright (C) 1998 Danilo Almeida. All rights reserved.
automatic stack-based locking object
This file is part of FIFS (Framework for Implementing File Systems).
This software is distributed with NO WARRANTY OF ANY KIND. No
author or distributor accepts any responsibility for the
consequences of using it, or for whether it serves any particular
purpose or works at all, unless he or she says so in writing.
Refer to the included modified Alladin Free Public License (the
"License") for full details.
Every copy of this software must include a copy of the License, in
a plain ASCII text file named COPYING. The License grants you the
right to copy, modify and redistribute this software, but only
under certain conditions described in the License. Among other
things, the License requires that the copyright notice and this
notice be preserved on all copies.
*/
#ifndef __AUTOLOCK_HXX__
#define __AUTOLOCK_HXX__
#include <windows.h>
class CcOsLock {
CRITICAL_SECTION cs;
bool valid;
public:
CcOsLock() {InitializeCriticalSection(&cs); valid = true; }
~CcOsLock() {DeleteCriticalSection(&cs); valid = false;}
void lock() {if (valid) EnterCriticalSection(&cs);}
void unlock() {if (valid) LeaveCriticalSection(&cs);}
bool trylock() {return valid ? (TryEnterCriticalSection(&cs) ? true : false)
: false; }
};
class CcAutoLock {
CcOsLock& m_lock;
public:
static void Start(CcAutoLock*& a, CcOsLock& lock) { a = new CcAutoLock(lock); };
static void Stop (CcAutoLock*& a) { delete a; a = 0; };
CcAutoLock(CcOsLock& lock):m_lock(lock) { m_lock.lock(); }
~CcAutoLock() { m_lock.unlock(); }
};
class CcAutoTryLock {
CcOsLock& m_lock;
bool m_locked;
public:
CcAutoTryLock(CcOsLock& lock):m_lock(lock) { m_locked = m_lock.trylock(); }
~CcAutoTryLock() { if (m_locked) m_lock.unlock(); m_locked = false; }
bool IsLocked() const { return m_locked; }
};
#endif /* __AUTOLOCK_HXX */
|