This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathTimerAwaitable.cs
128 lines (108 loc) · 3.72 KB
/
TimerAwaitable.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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Internal
{
internal class TimerAwaitable : IDisposable, ICriticalNotifyCompletion
{
private Timer _timer;
private Action _callback;
private static readonly Action _callbackCompleted = () => { };
private readonly TimeSpan _period;
private readonly TimeSpan _dueTime;
private bool _disposed;
private bool _running = true;
private object _lockObj = new object();
public TimerAwaitable(TimeSpan dueTime, TimeSpan period)
{
_dueTime = dueTime;
_period = period;
}
public void Start()
{
if (_timer == null)
{
lock (_lockObj)
{
if (_disposed)
{
return;
}
if (_timer == null)
{
// Don't capture the current ExecutionContext and its AsyncLocals onto the timer
bool restoreFlow = false;
try
{
if (!ExecutionContext.IsFlowSuppressed())
{
ExecutionContext.SuppressFlow();
restoreFlow = true;
}
_timer = new Timer(state => ((TimerAwaitable)state).Tick(), this, _dueTime, _period);
}
finally
{
// Restore the current ExecutionContext
if (restoreFlow)
{
ExecutionContext.RestoreFlow();
}
}
}
}
}
}
public TimerAwaitable GetAwaiter() => this;
public bool IsCompleted => ReferenceEquals(_callback, _callbackCompleted);
public bool GetResult()
{
_callback = null;
return _running;
}
private void Tick()
{
var continuation = Interlocked.Exchange(ref _callback, _callbackCompleted);
continuation?.Invoke();
}
public void OnCompleted(Action continuation)
{
if (ReferenceEquals(_callback, _callbackCompleted) ||
ReferenceEquals(Interlocked.CompareExchange(ref _callback, continuation, null), _callbackCompleted))
{
Task.Run(continuation);
}
}
public void UnsafeOnCompleted(Action continuation)
{
OnCompleted(continuation);
}
public void Stop()
{
lock (_lockObj)
{
// Stop should be used to trigger the call to end the loop which disposes
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
_running = false;
}
// Call tick here to make sure that we yield the callback,
// if it's currently waiting, we don't need to wait for the next period
Tick();
}
void IDisposable.Dispose()
{
lock (_lockObj)
{
_disposed = true;
_timer?.Dispose();
_timer = null;
}
}
}
}