-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.h
75 lines (61 loc) · 1.18 KB
/
lock.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include <string.h>
namespace tp
{
struct dummy_lock
{
void lock() {}
void unlock() {}
};
struct critical_section_lock
{
critical_section_lock()
{
__try
{
::InitializeCriticalSection(&m_cs);
}
__except (GetExceptionCode()==EXCEPTION_ACCESS_VIOLATION ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_EXECUTE_HANDLER)
{
memset(&m_cs, 0, sizeof(m_cs));
}
}
~critical_section_lock()
{
::DeleteCriticalSection(&m_cs);
}
#if (_MSC_VER >= 1700)
_Acquires_lock_(this->m_cs)
#endif
void lock()
{
::EnterCriticalSection(&m_cs);
}
#if (_MSC_VER >= 1700)
_Releases_lock_(this->m_cs)
#endif
void unlock()
{
::LeaveCriticalSection(&m_cs);
}
private:
CRITICAL_SECTION m_cs;
};
template <typename T>
class autolocker
{
public:
explicit autolocker(T& l) : locker(l)
{
locker.lock();
}
~autolocker()
{
locker.unlock();
}
private:
autolocker& operator= (const autolocker& a);
T& locker;
};
}