forked from microsoft/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Locks.h
109 lines (90 loc) · 2.71 KB
/
Locks.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#pragma once
#include <windows.h>
class PrioritizedLock {
public:
PrioritizedLock() throw()
{
InitializeSRWLock(&srwlock);
InitializeCriticalSectionEx(&cs, 4000, 0);
}
~PrioritizedLock() throw()
{
DeleteCriticalSection(&cs);
}
// taking an *exclusive* lock to interrupt the *shared* lock taken by the deque IO path
// - we want to interrupt the IO path so we can initiate more IO if we need to grow the CQ
_Acquires_exclusive_lock_(this->srwlock)
_Acquires_lock_(this->cs)
void PriorityLock() throw()
{
AcquireSRWLockExclusive(&srwlock);
EnterCriticalSection(&cs);
}
_Releases_lock_(this->cs)
_Releases_exclusive_lock_(this->srwlock)
void PriorityRelease() throw()
{
LeaveCriticalSection(&cs);
ReleaseSRWLockExclusive(&srwlock);
}
_Acquires_shared_lock_(this->srwlock)
_Acquires_lock_(this->cs)
void DefaultLock() throw()
{
AcquireSRWLockShared(&srwlock);
EnterCriticalSection(&cs);
}
_Releases_lock_(this->cs)
_Releases_shared_lock_(this->srwlock)
void DefaultRelease() throw()
{
LeaveCriticalSection(&cs);
ReleaseSRWLockShared(&srwlock);
}
/// not copyable
PrioritizedLock(const PrioritizedLock&) = delete;
PrioritizedLock& operator=(const PrioritizedLock&) = delete;
private:
SRWLOCK srwlock;
CRITICAL_SECTION cs;
};
class AutoReleasePriorityLock {
public:
explicit AutoReleasePriorityLock(PrioritizedLock &priorityLock) throw()
: prioritizedLock(priorityLock)
{
prioritizedLock.PriorityLock();
}
~AutoReleasePriorityLock() throw()
{
prioritizedLock.PriorityRelease();
}
/// no default ctor
AutoReleasePriorityLock() = delete;
/// non-copyable
AutoReleasePriorityLock(const AutoReleasePriorityLock&) = delete;
AutoReleasePriorityLock operator=(const AutoReleasePriorityLock&) = delete;
private:
PrioritizedLock &prioritizedLock;
};
class AutoReleaseDefaultLock {
public:
explicit AutoReleaseDefaultLock(PrioritizedLock &priorityLock) throw()
: prioritizedLock(priorityLock)
{
prioritizedLock.DefaultLock();
}
~AutoReleaseDefaultLock() throw()
{
prioritizedLock.DefaultRelease();
}
/// no default ctor
AutoReleaseDefaultLock() = delete;
/// non-copyable
AutoReleaseDefaultLock(const AutoReleaseDefaultLock&) = delete;
AutoReleaseDefaultLock operator=(const AutoReleaseDefaultLock&) = delete;
private:
PrioritizedLock &prioritizedLock;
};