forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwaitableT.cs
More file actions
92 lines (78 loc) · 2.44 KB
/
Copy pathAwaitableT.cs
File metadata and controls
92 lines (78 loc) · 2.44 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
// 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.Runtime.CompilerServices;
using System.Security;
using UnityEngine.Internal;
namespace UnityEngine
{
[AsyncMethodBuilder(typeof(Awaitable.AwaitableAsyncMethodBuilder<>))]
public class Awaitable<T>
{
static Awaitable.ThreadSafeObjectPool<Awaitable<T>> _pool = new (()=>new ());
private Awaitable _awaitable;
T _result;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ContinueWith(Action continuation)
{
_awaitable.SetContinuation(continuation);
}
private T GetResult()
{
try
{
_awaitable.PropagateExceptionAndRelease();
return _result;
}
finally
{
_awaitable = null;
_result = default;
_pool.Release(this);
}
}
internal void SetResultAndRaiseContinuation(T result)
{
_result = result;
_awaitable.RaiseManagedCompletion(null);
}
internal void SetExceptionAndRaiseContinuation(Exception exception)
{
_awaitable.RaiseManagedCompletion(exception);
}
public void Cancel()
{
_awaitable.Cancel();
}
private Awaitable() { }
internal static Awaitable<T> GetManaged()
{
var innerCoroutine = Awaitable.NewManagedAwaitable();
var result = _pool.Get();
result._awaitable = innerCoroutine;
return result;
}
[ExcludeFromDocs]
public Awaiter GetAwaiter()
{
return new Awaiter(this);
}
[ExcludeFromDocs]
public struct Awaiter : INotifyCompletion
{
private readonly Awaitable<T> _coroutine;
public Awaiter(Awaitable<T> coroutine)
{
_coroutine = coroutine;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void OnCompleted(Action continuation)
{
_coroutine.ContinueWith(continuation);
}
public bool IsCompleted => _coroutine._awaitable.IsCompleted;
public T GetResult() => _coroutine.GetResult();
}
}
}