-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
ChannelMicroThreadAwaiter.cs
89 lines (72 loc) · 2.56 KB
/
ChannelMicroThreadAwaiter.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
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Stride.Core.MicroThreading
{
public class ChannelMicroThreadAwaiter<T> : ICriticalNotifyCompletion
{
private static List<ChannelMicroThreadAwaiter<T>> pool = new List<ChannelMicroThreadAwaiter<T>>();
private bool isCompleted = false;
internal MicroThread MicroThread;
internal Action Continuation;
internal T Result;
public static ChannelMicroThreadAwaiter<T> New(MicroThread microThread)
{
lock (pool)
{
if (pool.Count > 0)
{
var index = pool.Count - 1;
var lastItem = pool[index];
pool.RemoveAt(index);
lastItem.MicroThread = microThread;
return lastItem;
}
return new ChannelMicroThreadAwaiter<T>(microThread);
}
}
public ChannelMicroThreadAwaiter(MicroThread microThread)
{
MicroThread = microThread;
}
public ChannelMicroThreadAwaiter<T> GetAwaiter()
{
return this;
}
public void OnCompleted(Action continuation)
{
Continuation = continuation;
}
public void UnsafeOnCompleted(Action continuation)
{
Continuation = continuation;
}
public T GetResult()
{
// Check Task Result (exception, etc...)
MicroThread.CancellationToken.ThrowIfCancellationRequested();
var result = Result;
// After result has been taken, we can reuse this item, so put it in the pool
// We mitigate pool size, but another approach than hard limit might be interesting
lock (pool)
{
if (pool.Count < 4096)
{
isCompleted = false;
MicroThread = null;
Continuation = null;
Result = default(T);
}
pool.Add(this);
}
return result;
}
public bool IsCompleted
{
get { return isCompleted || (MicroThread != null && MicroThread.IsOver); }
set { isCompleted = value; }
}
}
}