Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SingleConsumerUnboundedChannel.cs
Go to the documentation of this file.
6
8
9[DebuggerDisplay("Items={ItemsCountForDebugger}, Closed={ChannelIsClosedForDebugger}")]
12{
13 [DebuggerDisplay("Items={ItemsCountForDebugger}")]
16 {
18
20
22
23 public override Task Completion => _parent._completion.Task;
24
25 public override bool CanPeek => true;
26
27 private int ItemsCountForDebugger => _parent._items.Count;
28
30 {
31 _parent = parent;
32 _readerSingleton = new AsyncOperation<T>(parent._runContinuationsAsynchronously, default(CancellationToken), pooled: true);
33 _waiterSingleton = new AsyncOperation<bool>(parent._runContinuationsAsynchronously, default(CancellationToken), pooled: true);
34 }
35
37 {
38 if (cancellationToken.IsCancellationRequested)
39 {
41 }
42 if (TryRead(out var item))
43 {
44 return new ValueTask<T>(item);
45 }
49 lock (parent.SyncObj)
50 {
51 if (TryRead(out item))
52 {
53 return new ValueTask<T>(item);
54 }
55 if (parent._doneWriting != null)
56 {
57 return ChannelUtilities.GetInvalidCompletionValueTask<T>(parent._doneWriting);
58 }
59 asyncOperation = parent._blockedReader;
61 {
64 {
65 asyncOperation = null;
66 }
67 }
68 else
69 {
70 asyncOperation2 = new AsyncOperation<T>(_parent._runContinuationsAsynchronously, cancellationToken);
71 }
72 parent._blockedReader = asyncOperation2;
73 }
74 asyncOperation?.TrySetCanceled();
75 return asyncOperation2.ValueTaskOfT;
76 }
77
78 public override bool TryRead([MaybeNullWhen(false)] out T item)
79 {
81 if (parent._items.TryDequeue(out item))
82 {
83 if (parent._doneWriting != null && parent._items.IsEmpty)
84 {
85 ChannelUtilities.Complete(parent._completion, parent._doneWriting);
86 }
87 return true;
88 }
89 return false;
90 }
91
92 public override bool TryPeek([MaybeNullWhen(false)] out T item)
93 {
94 return _parent._items.TryPeek(out item);
95 }
96
98 {
99 if (cancellationToken.IsCancellationRequested)
100 {
102 }
103 if (!_parent._items.IsEmpty)
104 {
105 return new ValueTask<bool>(result: true);
106 }
110 lock (parent.SyncObj)
111 {
112 if (!parent._items.IsEmpty)
113 {
114 return new ValueTask<bool>(result: true);
115 }
116 if (parent._doneWriting != null)
117 {
118 return (parent._doneWriting != ChannelUtilities.s_doneWritingSentinel) ? new ValueTask<bool>(Task.FromException<bool>(parent._doneWriting)) : default(ValueTask<bool>);
119 }
120 asyncOperation = parent._waitingReader;
121 if (!cancellationToken.CanBeCanceled && _waiterSingleton.TryOwnAndReset())
122 {
125 {
126 asyncOperation = null;
127 }
128 }
129 else
130 {
131 asyncOperation2 = new AsyncOperation<bool>(_parent._runContinuationsAsynchronously, cancellationToken);
132 }
133 parent._waitingReader = asyncOperation2;
134 }
135 asyncOperation?.TrySetCanceled();
136 return asyncOperation2.ValueTaskOfT;
137 }
138
143 }
144
145 [DebuggerDisplay("Items={ItemsCountForDebugger}")]
148 {
150
151 private int ItemsCountForDebugger => _parent._items.Count;
152
154 {
155 _parent = parent;
156 }
157
158 public override bool TryComplete(Exception error)
159 {
162 bool flag = false;
164 lock (parent.SyncObj)
165 {
166 if (parent._doneWriting != null)
167 {
168 return false;
169 }
170 parent._doneWriting = error ?? ChannelUtilities.s_doneWritingSentinel;
171 if (parent._items.IsEmpty)
172 {
173 flag = true;
174 if (parent._blockedReader != null)
175 {
176 asyncOperation = parent._blockedReader;
177 parent._blockedReader = null;
178 }
179 if (parent._waitingReader != null)
180 {
181 asyncOperation2 = parent._waitingReader;
182 parent._waitingReader = null;
183 }
184 }
185 }
186 if (flag)
187 {
188 ChannelUtilities.Complete(parent._completion, error);
189 }
190 if (asyncOperation != null)
191 {
193 asyncOperation.TrySetException(error);
194 }
195 if (asyncOperation2 != null)
196 {
197 if (error != null)
198 {
199 asyncOperation2.TrySetException(error);
200 }
201 else
202 {
203 asyncOperation2.TrySetResult(item: false);
204 }
205 }
206 return true;
207 }
208
209 public override bool TryWrite(T item)
210 {
213 do
214 {
215 asyncOperation = null;
217 lock (parent.SyncObj)
218 {
219 if (parent._doneWriting != null)
220 {
221 return false;
222 }
223 asyncOperation = parent._blockedReader;
224 if (asyncOperation != null)
225 {
226 parent._blockedReader = null;
227 }
228 else
229 {
230 parent._items.Enqueue(item);
231 asyncOperation2 = parent._waitingReader;
232 if (asyncOperation2 == null)
233 {
234 return true;
235 }
236 parent._waitingReader = null;
237 }
238 }
239 if (asyncOperation2 != null)
240 {
241 asyncOperation2.TrySetResult(item: true);
242 return true;
243 }
244 }
245 while (!asyncOperation.TrySetResult(item));
246 return true;
247 }
248
250 {
251 Exception doneWriting = _parent._doneWriting;
252 if (!cancellationToken.IsCancellationRequested)
253 {
254 if (doneWriting != null)
255 {
257 {
258 return default(ValueTask<bool>);
259 }
261 }
262 return new ValueTask<bool>(result: true);
263 }
265 }
266
268 {
269 if (!cancellationToken.IsCancellationRequested)
270 {
271 if (!TryWrite(item))
272 {
274 }
275 return default(ValueTask);
276 }
278 }
279
284 }
285
287
289
290 private readonly bool _runContinuationsAsynchronously;
291
292 private volatile Exception _doneWriting;
293
295
297
298 private object SyncObj => _items;
299
301
303
311
316}
static readonly Exception s_doneWritingSentinel
static void Complete(TaskCompletionSource tcs, Exception error=null)
static Exception CreateInvalidCompletionException(Exception inner=null)
override ValueTask< bool > WaitToReadAsync(CancellationToken cancellationToken)
override ValueTask< bool > WaitToWriteAsync(CancellationToken cancellationToken)
override ValueTask WriteAsync(T item, CancellationToken cancellationToken)
static Task FromException(Exception exception)
Definition Task.cs:3341
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363