Repository navigation
Expand file tree
/
Copy pathLock.cs
More file actions
301 lines (272 loc) · 14.3 KB
/
Copy pathLock.cs
File metadata and controls
301 lines (272 loc) · 14.3 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace System.Threading
{
/// <summary>
/// Provides a way to get mutual exclusion in regions of code between different threads. A lock may be held by one thread at
/// a time.
/// </summary>
/// <remarks>
/// Threads that cannot immediately enter the lock may wait for the lock to be exited or until a specified timeout. A thread
/// that holds a lock may enter the lock repeatedly without exiting it, such as recursively, in which case the thread should
/// eventually exit the lock the same number of times to fully exit the lock and allow other threads to enter the lock.
/// </remarks>
public sealed partial class Lock
{
// Lock participates in class construction and must not have a static constructor.
internal const int UninitializedThreadId = 0;
private int _owningThreadId; // cDAC depends on exact name of this field
private uint _state; // cDAC depends on this field name and bit 0 indicating a held lock
private uint _recursionCount; // cDAC depends on exact name of this field
/// <summary>
/// Initializes a new instance of the <see cref="Lock"/> class.
/// </summary>
public Lock() => Initialize();
#if NATIVEAOT // The method needs to be public in NativeAOT so that other private libraries can access it
public Lock(bool useTrivialWaits)
#else
internal Lock(bool useTrivialWaits)
#endif
: this()
{
InitializeUseTrivialWaits(useTrivialWaits);
}
internal int OwningManagedThreadId => (int)_owningThreadId;
/// <summary>
/// Enters the lock. Once the method returns, the calling thread would be the only thread that holds the lock.
/// </summary>
/// <remarks>
/// If the lock cannot be entered immediately, the calling thread waits for the lock to be exited. If the lock is
/// already held by the calling thread, the lock is entered again. The calling thread should exit the lock as many times
/// as it had entered the lock to fully exit the lock and allow other threads to enter the lock.
/// </remarks>
/// <exception cref="LockRecursionException">
/// The lock has reached the limit of recursive enters. The limit is implementation-defined, but is expected to be high
/// enough that it would typically not be reached when the lock is used properly.
/// </exception>
[MethodImpl(MethodImplOptions.NoInlining)]
public void Enter()
{
int currentThreadId = TryEnter_Inlined(timeoutMs: -1);
Debug.Assert(currentThreadId != UninitializedThreadId);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private int EnterAndGetCurrentThreadId()
{
int currentThreadId = TryEnter_Inlined(timeoutMs: -1);
Debug.Assert(currentThreadId != UninitializedThreadId);
Debug.Assert(currentThreadId == _owningThreadId);
return currentThreadId;
}
/// <summary>
/// Enters the lock and returns a <see cref="Scope"/> that may be disposed to exit the lock. Once the method returns,
/// the calling thread would be the only thread that holds the lock. This method is intended to be used along with a
/// language construct that would automatically dispose the <see cref="Scope"/>, such as with the C# <code>using</code>
/// statement.
/// </summary>
/// <returns>
/// A <see cref="Scope"/> that may be disposed to exit the lock.
/// </returns>
/// <remarks>
/// If the lock cannot be entered immediately, the calling thread waits for the lock to be exited. If the lock is
/// already held by the calling thread, the lock is entered again. The calling thread should exit the lock, such as by
/// disposing the returned <see cref="Scope"/>, as many times as it had entered the lock to fully exit the lock and
/// allow other threads to enter the lock.
/// </remarks>
/// <exception cref="LockRecursionException">
/// The lock has reached the limit of recursive enters. The limit is implementation-defined, but is expected to be high
/// enough that it would typically not be reached when the lock is used properly.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Scope EnterScope() => new Scope(this, EnterAndGetCurrentThreadId());
/// <summary>
/// A disposable structure that is returned by <see cref="EnterScope()"/>, which when disposed, exits the lock.
/// </summary>
public ref struct Scope
{
private Lock? _lockObj;
private int _currentThreadId;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Scope(Lock lockObj, int currentThreadId)
{
_lockObj = lockObj;
_currentThreadId = currentThreadId;
}
/// <summary>
/// Exits the lock.
/// </summary>
/// <remarks>
/// If the calling thread holds the lock multiple times, such as recursively, the lock is exited only once. The
/// calling thread should ensure that each enter is matched with an exit.
/// </remarks>
/// <exception cref="SynchronizationLockException">
/// The calling thread does not hold the lock.
/// </exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
Lock? lockObj = _lockObj;
if (lockObj is not null)
{
_lockObj = null;
lockObj.Exit(_currentThreadId);
}
}
}
/// <summary>
/// Tries to enter the lock without waiting. If the lock is entered, the calling thread would be the only thread that
/// holds the lock.
/// </summary>
/// <returns>
/// <code>true</code> if the lock was entered, <code>false</code> otherwise.
/// </returns>
/// <remarks>
/// If the lock cannot be entered immediately, the method returns <code>false</code>. If the lock is already held by the
/// calling thread, the lock is entered again. The calling thread should exit the lock as many times as it had entered
/// the lock to fully exit the lock and allow other threads to enter the lock.
/// </remarks>
/// <exception cref="LockRecursionException">
/// The lock has reached the limit of recursive enters. The limit is implementation-defined, but is expected to be high
/// enough that it would typically not be reached when the lock is used properly.
/// </exception>
[MethodImpl(MethodImplOptions.NoInlining)]
public bool TryEnter() => TryEnter_Inlined(timeoutMs: 0) != UninitializedThreadId;
/// <summary>
/// Tries to enter the lock, waiting for roughly the specified duration. If the lock is entered, the calling thread
/// would be the only thread that holds the lock.
/// </summary>
/// <param name="millisecondsTimeout">
/// The rough duration in milliseconds for which the method will wait if the lock is not available. A value of
/// <code>0</code> specifies that the method should not wait, and a value of <see cref="Timeout.Infinite"/> or
/// <code>-1</code> specifies that the method should wait indefinitely until the lock is entered.
/// </param>
/// <returns>
/// <code>true</code> if the lock was entered, <code>false</code> otherwise.
/// </returns>
/// <remarks>
/// If the lock cannot be entered immediately, the calling thread waits for roughly the specified duration for the lock
/// to be exited. If the lock is already held by the calling thread, the lock is entered again. The calling thread
/// should exit the lock as many times as it had entered the lock to fully exit the lock and allow other threads to
/// enter the lock.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="millisecondsTimeout"/> is less than <code>-1</code>.
/// </exception>
/// <exception cref="LockRecursionException">
/// The lock has reached the limit of recursive enters. The limit is implementation-defined, but is expected to be high
/// enough that it would typically not be reached when the lock is used properly.
/// </exception>
public bool TryEnter(int millisecondsTimeout)
{
ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1);
return TryEnter_Outlined(millisecondsTimeout);
}
/// <summary>
/// Tries to enter the lock, waiting for roughly the specified duration. If the lock is entered, the calling thread
/// would be the only thread that holds the lock.
/// </summary>
/// <param name="timeout">
/// The rough duration for which the method will wait if the lock is not available. The timeout is converted to a number
/// of milliseconds by casting <see cref="TimeSpan.TotalMilliseconds"/> of the timeout to an integer value. A value
/// representing <code>0</code> milliseconds specifies that the method should not wait, and a value representing
/// <see cref="Timeout.Infinite"/> or <code>-1</code> milliseconds specifies that the method should wait indefinitely
/// until the lock is entered.
/// </param>
/// <returns>
/// <code>true</code> if the lock was entered, <code>false</code> otherwise.
/// </returns>
/// <remarks>
/// If the lock cannot be entered immediately, the calling thread waits for roughly the specified duration for the lock
/// to be exited. If the lock is already held by the calling thread, the lock is entered again. The calling thread
/// should exit the lock as many times as it had entered the lock to fully exit the lock and allow other threads to
/// enter the lock.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="timeout"/>, after its conversion to an integer millisecond value, represents a value that is less
/// than <code>-1</code> milliseconds or greater than <see cref="int.MaxValue"/> milliseconds.
/// </exception>
/// <exception cref="LockRecursionException">
/// The lock has reached the limit of recursive enters. The limit is implementation-defined, but is expected to be high
/// enough that it would typically not be reached when the lock is used properly.
/// </exception>
public bool TryEnter(TimeSpan timeout) => TryEnter_Outlined(WaitHandle.ToTimeoutMilliseconds(timeout));
[MethodImpl(MethodImplOptions.NoInlining)]
internal bool TryEnter_Outlined(int timeoutMs) => TryEnter_Inlined(timeoutMs) != UninitializedThreadId;
/// <summary>
/// Exits the lock.
/// </summary>
/// <remarks>
/// If the calling thread holds the lock multiple times, such as recursively, the lock is exited only once. The
/// calling thread should ensure that each enter is matched with an exit.
/// </remarks>
/// <exception cref="SynchronizationLockException">
/// The calling thread does not hold the lock.
/// </exception>
[MethodImpl(MethodImplOptions.NoInlining)]
public void Exit()
{
var owningThreadId = _owningThreadId;
if (owningThreadId == UninitializedThreadId || owningThreadId != ManagedThreadId.CurrentManagedThreadIdUnchecked)
{
ThrowHelper.ThrowSynchronizationLockException_LockExit();
}
ExitImpl();
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal void Exit(int currentThreadId)
{
Debug.Assert(currentThreadId != UninitializedThreadId);
Debug.Assert(currentThreadId == ManagedThreadId.CurrentManagedThreadIdUnchecked);
if (_owningThreadId != currentThreadId)
{
ThrowHelper.ThrowSynchronizationLockException_LockExit();
}
ExitImpl();
}
internal void Reenter(uint previousRecursionCount)
{
Debug.Assert(!IsHeldByCurrentThread);
#if FEATURE_WASM_MANAGED_THREADS
if (Thread.ThrowOnBlockingWaitOnJSInteropThread || Thread.WarnOnBlockingWaitOnJSInteropThread)
{
// A timed condition wait must restore ownership even if another thread still holds the lock.
while (!TryEnter(int.MaxValue))
{
}
}
else
#endif
{
Enter();
}
_recursionCount = previousRecursionCount;
}
/// <summary>
/// <code>true</code> if the lock is held by the calling thread, <code>false</code> otherwise.
/// </summary>
public bool IsHeldByCurrentThread => IsHeldByCurrentThreadCore;
internal int OwningThreadId => _owningThreadId;
#if CORECLR
[System.Runtime.InteropServices.UnmanagedCallersOnly]
private static unsafe void InitializeForMonitor(Lock* pLock, int managedThreadId, uint recursionCount, Exception* pException)
{
try
{
pLock->InitializeForMonitor(managedThreadId, recursionCount);
}
catch (Exception ex)
{
*pException = ex;
}
}
#endif
internal bool Wait(int millisecondsTimeout)
{
#pragma warning disable CS9216 // A value of type 'System.Threading.Lock' converted to a different type will use likely unintended monitor-based locking in 'lock' statement.
return Wait(millisecondsTimeout, this);
#pragma warning restore CS9216 // A value of type 'System.Threading.Lock' converted to a different type will use likely unintended monitor-based locking in 'lock' statement.
}
}
}