Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ConcurrentExclusiveSchedulerPair.cs
Go to the documentation of this file.
3
5
6[DebuggerDisplay("Concurrent={ConcurrentTaskCountForDebugger}, Exclusive={ExclusiveTaskCountForDebugger}, Mode={ModeForDebugger}")]
7[DebuggerTypeProxy(typeof(DebugView))]
9{
10 private sealed class CompletionState : Task
11 {
12 internal bool m_completionRequested;
13
14 internal bool m_completionQueued;
15
17 }
18
20 {
22
27
29 {
30 if (_pair.m_processingCount == -1)
31 {
33 }
34 else
35 {
37 }
38 }
39 }
40
41 [DebuggerDisplay("Count={CountForDebugger}, MaxConcurrencyLevel={m_maxConcurrencyLevel}, Id={Id}")]
42 [DebuggerTypeProxy(typeof(DebugView))]
44 {
60
62
63 private readonly int m_maxConcurrencyLevel;
64
66
68
70
72
91
92 protected internal override void QueueTask(Task task)
93 {
95 {
97 {
98 throw new InvalidOperationException(GetType().ToString());
99 }
100 m_tasks.Enqueue(task);
102 }
103 }
104
105 internal void ExecuteTask(Task task)
106 {
108 }
109
111 {
113 {
114 return false;
115 }
116 bool flag = m_pair.m_underlyingTaskScheduler == TaskScheduler.Default;
117 if (flag && taskWasPreviouslyQueued && !Thread.CurrentThread.IsThreadPoolThread)
118 {
119 return false;
120 }
122 {
123 if (!flag || taskWasPreviouslyQueued)
124 {
126 }
127 return TryExecuteTask(task);
128 }
129 return false;
130 }
131
133 {
134 Task<bool> task2 = new Task<bool>(delegate(object s)
135 {
137 return tupleSlim.Item1.TryExecuteTask(tupleSlim.Item2);
139 try
140 {
141 task2.RunSynchronously(m_pair.m_underlyingTaskScheduler);
142 return task2.Result;
143 }
144 catch
145 {
146 _ = task2.Exception;
147 throw;
148 }
149 finally
150 {
151 task2.Dispose();
152 }
153 }
154
156 {
157 return m_tasks;
158 }
159 }
160
190
191 [Flags]
192 private enum ProcessingMode : byte
193 {
197 Completing = 4,
198 Completed = 8
199 }
200
202
204
206
208
209 private readonly int m_maxConcurrencyLevel;
210
211 private readonly int m_maxItemsPerTask;
212
213 private int m_processingCount;
214
216
218
220
222
224
226 {
227 get
228 {
229 if (m_completionState != null)
230 {
232 }
233 return false;
234 }
235 }
236
237 private bool ReadyToComplete
238 {
239 get
240 {
242 {
243 return false;
244 }
246 if (completionState.m_exceptions == null || completionState.m_exceptions.Count <= 0)
247 {
249 {
250 return m_exclusiveTaskScheduler.m_tasks.IsEmpty;
251 }
252 return false;
253 }
254 return true;
255 }
256 }
257
259
261
263
265
267 {
268 get
269 {
271 {
272 return ProcessingMode.Completed;
273 }
274 ProcessingMode processingMode = ProcessingMode.NotCurrentlyProcessing;
275 if (m_processingCount == -1)
276 {
277 processingMode |= ProcessingMode.ProcessingExclusiveTask;
278 }
279 if (m_processingCount >= 1)
280 {
281 processingMode |= ProcessingMode.ProcessingConcurrentTasks;
282 }
284 {
285 processingMode |= ProcessingMode.Completing;
286 }
287 return processingMode;
288 }
289 }
290
295
300
305
307 {
308 if (taskScheduler == null)
309 {
310 throw new ArgumentNullException("taskScheduler");
311 }
313 {
314 throw new ArgumentOutOfRangeException("maxConcurrencyLevel");
315 }
316 if (maxItemsPerTask == 0 || maxItemsPerTask < -1)
317 {
318 throw new ArgumentOutOfRangeException("maxItemsPerTask");
319 }
323 int maximumConcurrencyLevel = taskScheduler.MaximumConcurrencyLevel;
325 {
327 }
328 if (m_maxConcurrencyLevel == -1)
329 {
330 m_maxConcurrencyLevel = int.MaxValue;
331 }
332 if (m_maxItemsPerTask == -1)
333 {
334 m_maxItemsPerTask = int.MaxValue;
335 }
336 m_exclusiveTaskScheduler = new ConcurrentExclusiveTaskScheduler(this, 1, ProcessingMode.ProcessingExclusiveTask);
338 }
339
340 public void Complete()
341 {
343 {
345 {
348 }
349 }
350 }
351
361
366
368 {
369 if (ReadyToComplete)
370 {
372 }
373 }
374
375 private void CompleteTaskAsync()
376 {
378 if (!completionState.m_completionQueued)
379 {
380 completionState.m_completionQueued = true;
382 {
384 List<Exception> exceptions = concurrentExclusiveSchedulerPair.m_completionState.m_exceptions;
385 bool flag = ((exceptions != null && exceptions.Count > 0) ? concurrentExclusiveSchedulerPair.m_completionState.TrySetException(exceptions) : concurrentExclusiveSchedulerPair.m_completionState.TrySetResult());
386 concurrentExclusiveSchedulerPair.m_threadProcessingMode.Dispose();
387 }, this);
388 }
389 }
390
392 {
396 if (completionState2.m_exceptions == null)
397 {
398 completionState2.m_exceptions = new List<Exception>(exception.InnerExceptionCount);
399 }
400 completionState.m_exceptions.AddRange(exception.InternalInnerExceptions);
402 }
403
404 private void ProcessAsyncIfNecessary(bool fairly = false)
405 {
406 if (m_processingCount < 0)
407 {
408 return;
409 }
410 bool flag = !m_exclusiveTaskScheduler.m_tasks.IsEmpty;
411 Task task = null;
412 if (m_processingCount == 0 && flag)
413 {
416 {
417 try
418 {
419 task = new Task(delegate(object thisPair)
420 {
421 ((ConcurrentExclusiveSchedulerPair)thisPair).ProcessExclusiveTasks();
424 }
425 catch (Exception exception)
426 {
429 }
430 }
431 }
432 else
433 {
435 if (count > 0 && !flag && m_processingCount < m_maxConcurrencyLevel)
436 {
437 for (int i = 0; i < count; i++)
438 {
440 {
441 break;
442 }
445 {
446 continue;
447 }
448 try
449 {
450 task = new Task(delegate(object thisPair)
451 {
452 ((ConcurrentExclusiveSchedulerPair)thisPair).ProcessConcurrentTasks();
455 }
456 catch (Exception exception2)
457 {
460 }
461 }
462 }
463 }
465 }
466
477
479 {
480 try
481 {
482 m_threadProcessingMode.Value = ProcessingMode.ProcessingExclusiveTask;
483 for (int i = 0; i < m_maxItemsPerTask; i++)
484 {
485 if (!m_exclusiveTaskScheduler.m_tasks.TryDequeue(out var result))
486 {
487 break;
488 }
489 if (!result.IsFaulted)
490 {
492 }
493 }
494 }
495 finally
496 {
497 m_threadProcessingMode.Value = ProcessingMode.NotCurrentlyProcessing;
499 {
502 }
503 }
504 }
505
507 {
508 try
509 {
510 m_threadProcessingMode.Value = ProcessingMode.ProcessingConcurrentTasks;
511 for (int i = 0; i < m_maxItemsPerTask; i++)
512 {
513 if (!m_concurrentTaskScheduler.m_tasks.TryDequeue(out var result))
514 {
515 break;
516 }
517 if (!result.IsFaulted)
518 {
520 }
522 {
523 break;
524 }
525 }
526 }
527 finally
528 {
529 m_threadProcessingMode.Value = ProcessingMode.NotCurrentlyProcessing;
531 {
532 if (m_processingCount > 0)
533 {
535 }
537 }
538 }
539 }
540
550}
void AddRange(IEnumerable< KeyValuePair< TKey, TValue > > collection)
static int ProcessorCount
static int CompareExchange(ref int location1, int value, int comparand)
ConcurrentExclusiveTaskScheduler(ConcurrentExclusiveSchedulerPair pair, int maxConcurrencyLevel, ProcessingMode processingMode)
static TaskCreationOptions GetCreationOptionsForTask(bool isReplacementReplica=false)
ConcurrentExclusiveSchedulerPair(TaskScheduler taskScheduler, int maxConcurrencyLevel)
ConcurrentExclusiveSchedulerPair(TaskScheduler taskScheduler, int maxConcurrencyLevel, int maxItemsPerTask)
static Task FromException(Exception exception)
Definition Task.cs:3341
static bool QueueUserWorkItem(WaitCallback callBack)
static void UnsafeQueueUserWorkItemInternal(object callBack, bool preferLocal)
static Thread CurrentThread
Definition Thread.cs:312
static bool Read(ref bool location)
Definition Volatile.cs:67