-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncReaderWriterLock.cs
223 lines (199 loc) · 7.63 KB
/
AsyncReaderWriterLock.cs
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace RecNet.Common.Synchronization
{
/// <summary>
/// An asynchronous locker that provides read and write locking policies and uses an IDisposable
/// pattern for releasing the lock.
/// <para>
/// This is based on the following blog post:
/// https://devblogs.microsoft.com/pfxteam/building-async-coordination-primitives-part-7-asyncreaderwriterlock/
/// </para>
/// </summary>
public class AsyncReaderWriterLock
{
#region Types
private sealed class Releaser : IDisposable
{
private readonly AsyncReaderWriterLock _toRelease;
private readonly bool _writer;
internal Releaser(AsyncReaderWriterLock toRelease, bool writer)
{
_toRelease = toRelease;
_writer = writer;
}
public void Dispose()
{
if (_writer)
{
_toRelease.WriterRelease();
}
else
{
_toRelease.ReaderRelease();
}
}
}
#endregion
#region Fields
private readonly IDisposable _writerReleaser;
private readonly IDisposable _readerReleaser;
private readonly Task<IDisposable> _writerReleaserTask;
private readonly Task<IDisposable> _readerReleaserTask;
private readonly Queue<TaskCompletionSource<IDisposable>> _waitingWriters;
private TaskCompletionSource<IDisposable>? _waitingReader;
private int _readersWaiting;
/// <summary>
/// Tracks the current status of the lock:
/// 0 == lock is unheld
/// -1 == lock is held by a single writer
/// >0 == lock is held by this number of concurrent readers
/// </summary>
private int _status;
#endregion
#region Properties
/// <summary>
/// Gets or sets the callback that should be invoked whenever this lock is released.
/// </summary>
public Action? OnRelease { get; set; }
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="AsyncReaderWriterLock"/> class.
/// </summary>
public AsyncReaderWriterLock()
{
_writerReleaser = new Releaser(this, true);
_readerReleaser = new Releaser(this, false);
_writerReleaserTask = Task.FromResult(_writerReleaser);
_readerReleaserTask = Task.FromResult(_readerReleaser);
_waitingWriters = new Queue<TaskCompletionSource<IDisposable>>();
_waitingReader = null;
_readersWaiting = 0;
_status = 0;
}
#endregion
#region APIs
/// <summary>
/// Asynchronously obtains the lock in shared reader mode. Dispose the returned <see cref="IDisposable"/>
/// to release the lock.
/// </summary>
/// <returns>
/// The <see cref="Task{IDisposable}"/> that will release the lock.
/// </returns>
public Task<IDisposable> ReaderLockAsync()
{
lock (_waitingWriters)
{
if (_status >= 0 && _waitingWriters.Count == 0)
{
// Lock is not held by a writer and no writers are waiting, so allow this reader to obtain
// the lock immediately and return the pre-allocated reader releaser.
++_status;
return _readerReleaserTask;
}
else
{
// This reader has to wait to obtain the lock. Lazy instantiate the _waitingReader tcs, and
// then return a unique continuation of that tcs (this ensures that all waiting readers can be
// released simultaneously).
++_readersWaiting;
if (_waitingReader == null)
{
_waitingReader = new TaskCompletionSource<IDisposable>();
}
return _waitingReader.Task.ContinueWith(t => t.Result, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
}
}
/// <summary>
/// Asynchronously obtains the lock in exclusive writer mode. Dispose the returned <see cref="IDisposable"/>
/// to release the lock.
/// </summary>
/// <returns>
/// The <see cref="Task{IDisposable}"/> that will release the lock.
/// </returns>
public Task<IDisposable> WriterLockAsync()
{
lock (_waitingWriters)
{
if (_status == 0)
{
// Lock is currently unheld, so allow this writer to obtain the lock immediately and return the
// pre-allocated writer releaser.
_status = -1;
return _writerReleaserTask;
}
else
{
// This writer has to wait to obtain the lock. Create a new tcs for this writer, add it to the
// queue of waiting writers, and return the task.
var waiter = new TaskCompletionSource<IDisposable>();
_waitingWriters.Enqueue(waiter);
return waiter.Task;
}
}
}
private void ReaderRelease()
{
try
{
TaskCompletionSource<IDisposable>? toWake = null;
lock (_waitingWriters)
{
--_status;
if (_status == 0 && _waitingWriters.Count > 0)
{
// The lock is now unheld, and there's a writer waiting, so give the lock to the first writer in the queue.
_status = -1;
toWake = _waitingWriters.Dequeue();
}
}
toWake?.SetResult(_writerReleaser);
}
finally
{
OnRelease?.Invoke();
}
}
private void WriterRelease()
{
try
{
TaskCompletionSource<IDisposable>? toWake = null;
bool toWakeIsWriter = false;
lock (_waitingWriters)
{
if (_waitingWriters.Count > 0)
{
// There's another writer waiting, so pass the lock on to the next writer.
toWake = _waitingWriters.Dequeue();
toWakeIsWriter = true;
}
else if (_readersWaiting > 0)
{
// There are readers waiting. Wake them all up at the same time since they can all concurrently
// hold the reader lock.
toWake = _waitingReader;
_status = _readersWaiting;
_readersWaiting = 0;
_waitingReader = null;
}
else
{
// Nobody is waiting, so the lock is now unheld.
_status = 0;
}
}
toWake?.SetResult(toWakeIsWriter ? _writerReleaser : _readerReleaser);
}
finally
{
OnRelease?.Invoke();
}
}
#endregion
}
}