forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwaitable.cs
More file actions
294 lines (272 loc) · 10.1 KB
/
Copy pathAwaitable.cs
File metadata and controls
294 lines (272 loc) · 10.1 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Threading;
namespace UnityEngine
{
/* Awaitable Coroutines are async/await compatible types specially tailored for running in Unity. (they can be awaited, and they can be used as return type
* of an async method)
* Compared to System.Threading.Tasks it is much less smart and takes shortcuts to improve performance based on
* Unity specific assumption.
* Main differences compared to .net tasks:
- Can be awaited only once: a given Awaitable object can't be awaited by multiple async functions. This is considered undefined behaviour
(considering it undefined behaviour allows for optimizations non-achievable otherwise)
- Never capture an ExecutionContext: for security purposes, .net Tasks capture execution contexts on awaiting to be able to handle things like
propagating impersonnation contexts accross async calls. We don't want to support that and can elliminate associated costs
- Never capture Synchronization context:
Coroutines continuations are run synchronously from the code raising the completion. In most cases, it will be from the Unity main frame (eg. for Delayed Call Coroutines and existing AsyncOperations)
Methods returning awaitable coroutines with completion being raised in a background thread will need to mention it explicitly in their documentation
- Awaiter.GetResults won't block until completion. Calling GetResults() before the operation is completed is an undefined behaviour
- Are pooled object:
Those are reference types, so that they can be referenced accross different stacks, efficiently copied etc, but to avoid too many allocations,
they are pooled. ObjectPool has been enhanced a bit to account to avoid Stack<T> bounds checks with common get/release patterns produced by async state machines
- Can be implemented in either managed or native code:
There is Scripting::Awaitables::Awaitable base class providing interop with this managed coroutine type, that can be extended so that
Native asynchronous code can be exposed as an await-compatible type in user code.
See AsyncOperationCoroutine implementation as an example
* */
[AsyncMethodBuilder(typeof(AwaitableAsyncMethodBuilder))]
public partial class Awaitable : IEnumerator
{
readonly struct AwaitableHandle
{
private readonly IntPtr _handle;
public bool IsNull => _handle == IntPtr.Zero;
public bool IsManaged => _handle == ManagedHandle._handle;
public AwaitableHandle(IntPtr handle) => _handle = handle;
public static AwaitableHandle ManagedHandle = new AwaitableHandle(new IntPtr(-1));
public static AwaitableHandle NullHandle = new AwaitableHandle(IntPtr.Zero);
public static implicit operator IntPtr(AwaitableHandle handle) => handle._handle;
public static implicit operator AwaitableHandle(IntPtr handle) => new AwaitableHandle(handle);
}
private readonly ManagedLockWithSingleThreadBias _spinLock = default;
static readonly ThreadSafeObjectPool<Awaitable> _pool = new(() => new());
AwaitableHandle _handle;
ExceptionDispatchInfo _exceptionToRethrow;
bool _managedAwaitableDone;
Action _continuation;
CancellationTokenRegistration? _cancelTokenRegistration;
private Awaitable() { }
internal static Awaitable NewManagedAwaitable()
{
var awaitable = _pool.Get();
awaitable._handle = AwaitableHandle.ManagedHandle;
return awaitable;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Awaitable FromNativeAwaitableHandle(IntPtr nativeHandle, CancellationToken cancellationToken)
{
var awaitable = _pool.Get();
awaitable._handle = nativeHandle;
unsafe
{
AttachManagedGCHandleToNativeAwaitable(nativeHandle, (UIntPtr)(void*)GCHandle.ToIntPtr(GCHandle.Alloc(awaitable)));
}
if (cancellationToken.CanBeCanceled)
{
WireupCancellation(awaitable, cancellationToken);
}
return awaitable;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WireupCancellation(Awaitable awaitable, CancellationToken cancellationToken)
{
if (awaitable == null)
{
throw new ArgumentNullException(nameof(awaitable));
}
try
{
awaitable._spinLock.Acquire();
awaitable._cancelTokenRegistration = cancellationToken.Register(coroutine => ((Awaitable)coroutine).Cancel(), awaitable);
}
finally
{
awaitable._spinLock.Release();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void RaiseManagedCompletion(Exception exception)
{
Action continuation = null;
try
{
_spinLock.Acquire();
if (exception != null)
{
_exceptionToRethrow = ExceptionDispatchInfo.Capture(exception);
}
_managedAwaitableDone = true;
continuation = _continuation;
_continuation = null;
}
finally
{
_spinLock.Release();
}
continuation?.Invoke();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void RaiseManagedCompletion()
{
Action continuation = null;
try
{
_spinLock.Acquire();
_managedAwaitableDone = true;
continuation = _continuation;
_continuation = null;
}
finally
{
_spinLock.Release();
}
continuation?.Invoke();
}
internal void PropagateExceptionAndRelease()
{
try
{
_spinLock.Acquire();
CheckPointerValidity();
if (_cancelTokenRegistration.HasValue)
{
_cancelTokenRegistration.Value.Dispose();
_cancelTokenRegistration = null;
}
_managedAwaitableDone = false;
var ptr = _handle;
_handle = AwaitableHandle.NullHandle;
var toRethrow = _exceptionToRethrow;
_exceptionToRethrow = null;
_continuation = null;
if (!ptr.IsManaged && !ptr.IsNull)
{
ReleaseNativeAwaitable(ptr);
}
_pool.Release(this);
toRethrow?.Throw();
}
finally
{
_spinLock.Release();
}
}
public void Cancel()
{
var handle = CheckPointerValidity();
if (handle.IsManaged)
{
RaiseManagedCompletion(new OperationCanceledException());
}
else
{
CancelNativeAwaitable(handle);
}
}
private bool IsCompletedNoLock
{
get
{
CheckPointerValidity();
if (_handle.IsManaged)
{
return _managedAwaitableDone;
}
return IsNativeAwaitableCompleted(_handle) != 0;
}
}
public bool IsCompleted
{
get
{
try
{
_spinLock.Acquire();
return IsCompletedNoLock;
}
finally
{
_spinLock.Release();
}
}
}
internal bool IsDettachedOrCompleted
{
get
{
try
{
_spinLock.Acquire();
if (_handle.IsNull)
{
return true;
}
CheckPointerValidity();
if (_handle.IsManaged)
{
return _managedAwaitableDone;
}
return IsNativeAwaitableCompleted(_handle) != 0;
}
finally
{
_spinLock.Release();
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private AwaitableHandle CheckPointerValidity()
{
var handle = _handle;
if (handle.IsNull)
{
throw new InvalidOperationException("Awaitable is in detached state");
}
return handle;
}
internal void SetContinuation(Action continuation)
{
bool done = false;
try
{
_spinLock.Acquire();
if (IsCompletedNoLock)
{
done = true;
}
else
{
_continuation = continuation;
}
}
finally
{
_spinLock.Release();
}
if (done)
{
continuation();
}
}
// legacy coroutine interop
bool IEnumerator.MoveNext()
{
if (IsCompleted)
{
PropagateExceptionAndRelease();
return false;
}
return true;
}
void IEnumerator.Reset()
{
}
object IEnumerator.Current => null;
}
}