forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronization.cpp
More file actions
262 lines (226 loc) · 9.6 KB
/
Copy pathSynchronization.cpp
File metadata and controls
262 lines (226 loc) · 9.6 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include <AppInstallerSynchronization.h>
#include <AppInstallerStrings.h>
namespace AppInstaller::Synchronization
{
using namespace std::string_view_literals;
constexpr std::wstring_view s_CrossProcessReaderWriteLock_MutexSuffix = L".mutex"sv;
// A milliseconds version of INFINITE
constexpr std::chrono::milliseconds s_CrossProcessReaderWriteLock_Infinite = static_cast<std::chrono::milliseconds>(INFINITE);
// The amount of time that we wait in between checking for cancellation
constexpr std::chrono::milliseconds s_CrossProcessReaderWriteLock_WaitLoopTime = 250ms;
// Arbitrary limit that should not ever cause a problem (theoretically 1 per process)
constexpr size_t s_CrossProcessReaderWriteLock_MaxReaders = 8;
namespace
{
wil::unique_mutex OpenControlMutex(const std::wstring& name)
{
std::wstring mutexName = name;
mutexName += s_CrossProcessReaderWriteLock_MutexSuffix;
wil::unique_mutex result;
result.create(mutexName.c_str(), 0, SYNCHRONIZE);
return result;
}
wil::unique_mutex OpenAccessMutex(const std::wstring& name, size_t index)
{
THROW_HR_IF(E_INVALIDARG, index >= s_CrossProcessReaderWriteLock_MaxReaders);
std::wostringstream strstr;
strstr << name << L'.' << index;
wil::unique_mutex result;
result.create(strstr.str().c_str(), 0, SYNCHRONIZE);
return result;
}
}
CrossProcessReaderWriteLock::~CrossProcessReaderWriteLock()
{
Release();
}
CrossProcessReaderWriteLock CrossProcessReaderWriteLock::LockShared(std::string_view name)
{
return Lock(true, name, s_CrossProcessReaderWriteLock_Infinite, nullptr);
}
CrossProcessReaderWriteLock CrossProcessReaderWriteLock::LockShared(std::string_view name, IProgressCallback& progress)
{
return Lock(true, name, s_CrossProcessReaderWriteLock_Infinite, &progress);
}
CrossProcessReaderWriteLock CrossProcessReaderWriteLock::LockExclusive(std::string_view name)
{
return Lock(false, name, s_CrossProcessReaderWriteLock_Infinite, nullptr);
}
CrossProcessReaderWriteLock CrossProcessReaderWriteLock::LockExclusive(std::string_view name, IProgressCallback& progress)
{
return Lock(false, name, s_CrossProcessReaderWriteLock_Infinite, &progress);
}
CrossProcessReaderWriteLock CrossProcessReaderWriteLock::LockExclusive(std::string_view name, std::chrono::milliseconds timeout)
{
return Lock(false, name, timeout, nullptr);
}
CrossProcessReaderWriteLock::operator bool() const
{
return !m_mutexesHeld.empty();
}
void CrossProcessReaderWriteLock::Release()
{
for (auto& mutex : m_mutexesHeld)
{
ReleaseMutex(mutex.get());
}
m_mutexesHeld.clear();
}
CrossProcessReaderWriteLock CrossProcessReaderWriteLock::Lock(
bool shared,
std::string_view name,
std::chrono::milliseconds timeout,
IProgressCallback* progress)
{
auto start = std::chrono::steady_clock::now();
// Verify inputs
THROW_HR_IF(E_INVALIDARG, name.find('\\') != std::string::npos);
THROW_HR_IF(E_INVALIDARG, timeout.count() > INFINITE);
CrossProcessReaderWriteLock result;
std::wstring wideName = Utility::ConvertToUTF16(name);
// Acquire overall control mutex
DWORD status = 0;
wil::unique_mutex controlMutex = OpenControlMutex(wideName);
auto lock = controlMutex.acquire(&status, static_cast<DWORD>(timeout.count()));
THROW_LAST_ERROR_IF(status == WAIT_FAILED);
if (status == WAIT_TIMEOUT || (progress && progress->IsCancelled()))
{
return result;
}
// Open all needed access mutexes
std::vector<wil::unique_mutex> allAccessMutexes;
HANDLE waitHandles[s_CrossProcessReaderWriteLock_MaxReaders]{};
if (shared)
{
// Acquire the first access mutex we can find that is open, or all of them if needed.
// Use the process id as an arbitrary value in an attempt to reduce collisions
// while still allowing for re-entrance to not be arbitrary.
size_t offset = GetProcessId(GetCurrentProcess()) % s_CrossProcessReaderWriteLock_MaxReaders;
for (size_t i = 0; i < s_CrossProcessReaderWriteLock_MaxReaders; ++i)
{
size_t index = (i + offset) % s_CrossProcessReaderWriteLock_MaxReaders;
wil::unique_mutex current = OpenAccessMutex(wideName, index);
status = ::WaitForSingleObjectEx(current.get(), 0, FALSE);
if (status == WAIT_OBJECT_0 || status == WAIT_ABANDONED)
{
// We found an empty one, continue on with it
result.m_mutexesHeld.emplace_back(std::move(current));
return result;
}
else if (status == WAIT_TIMEOUT)
{
waitHandles[i] = current.get();
allAccessMutexes.emplace_back(std::move(current));
}
else
{
THROW_LAST_ERROR();
}
}
}
else
{
// Open all of the access mutexes.
for (size_t i = 0; i < s_CrossProcessReaderWriteLock_MaxReaders; ++i)
{
wil::unique_mutex current = OpenAccessMutex(wideName, i);
waitHandles[i] = current.get();
allAccessMutexes.emplace_back(std::move(current));
}
}
// Wait for one/all of the mutexes (or cancellation)
bool waitAgain = true;
while (waitAgain && (!progress || !progress->IsCancelled()))
{
DWORD millisecondsToWait = 0;
if (progress)
{
if (timeout == s_CrossProcessReaderWriteLock_Infinite)
{
millisecondsToWait = static_cast<DWORD>(s_CrossProcessReaderWriteLock_WaitLoopTime.count());
}
else
{
auto currentDuration = std::chrono::steady_clock::now() - start;
if (currentDuration >= timeout)
{
// Allow an attempt to acquire with no wait
millisecondsToWait = 0;
waitAgain = false;
}
else
{
auto durationToWait = timeout - currentDuration;
if (durationToWait > s_CrossProcessReaderWriteLock_WaitLoopTime)
{
durationToWait = s_CrossProcessReaderWriteLock_WaitLoopTime;
}
else
{
waitAgain = false;
}
millisecondsToWait = static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(durationToWait).count());
}
}
}
else
{
// If there is no progress, we will do the full wait this time
waitAgain = false;
if (timeout == s_CrossProcessReaderWriteLock_Infinite)
{
millisecondsToWait = INFINITE;
}
else
{
auto currentDuration = std::chrono::steady_clock::now() - start;
if (currentDuration >= timeout)
{
// Allow an attempt to acquire with no wait
millisecondsToWait = 0;
}
else
{
millisecondsToWait = static_cast<DWORD>(std::chrono::duration_cast<std::chrono::milliseconds>(timeout - currentDuration).count());
}
}
}
status = WaitForMultipleObjectsEx(s_CrossProcessReaderWriteLock_MaxReaders, waitHandles, (shared ? FALSE : TRUE), millisecondsToWait, FALSE);
THROW_LAST_ERROR_IF(status == WAIT_FAILED);
if (status != WAIT_TIMEOUT)
{
break;
}
}
if (status == WAIT_TIMEOUT || (progress && progress->IsCancelled()))
{
return result;
}
if (shared)
{
size_t acquiredIndex = 0;
if (status >= WAIT_OBJECT_0 && status < (WAIT_OBJECT_0 + s_CrossProcessReaderWriteLock_MaxReaders))
{
acquiredIndex = status - WAIT_OBJECT_0;
}
else if (status >= WAIT_ABANDONED_0 && status < (WAIT_ABANDONED_0 + s_CrossProcessReaderWriteLock_MaxReaders))
{
acquiredIndex = status - WAIT_ABANDONED_0;
}
else
{
THROW_HR(E_UNEXPECTED);
}
// Take the one that was acquired
result.m_mutexesHeld.emplace_back(std::move(allAccessMutexes[acquiredIndex]));
}
else
{
result.m_mutexesHeld = std::move(allAccessMutexes);
}
return result;
}
}