-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathAbstractScheduledEventExecutor.cs
More file actions
226 lines (181 loc) · 8.21 KB
/
AbstractScheduledEventExecutor.cs
File metadata and controls
226 lines (181 loc) · 8.21 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DotNetty.Common.Concurrency
{
using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DotNetty.Common.Utilities;
/// <summary>
/// Abstract base class for <see cref="IEventExecutor" />s that need to support scheduling.
/// </summary>
public abstract class AbstractScheduledEventExecutor : AbstractEventExecutor
{
protected readonly PriorityQueue<IScheduledRunnable> ScheduledTaskQueue = new PriorityQueue<IScheduledRunnable>();
protected AbstractScheduledEventExecutor()
{
}
protected AbstractScheduledEventExecutor(IEventExecutorGroup parent)
: base(parent)
{
}
protected static PreciseTimeSpan GetNanos() => PreciseTimeSpan.FromStart;
protected static bool IsNullOrEmpty<T>(PriorityQueue<T> taskQueue)
where T : class
{
return taskQueue == null || taskQueue.Count == 0;
}
/// <summary>
/// Cancel all scheduled tasks
/// This method MUST be called only when <see cref="IEventExecutor.InEventLoop" /> is <c>true</c>.
/// </summary>
protected virtual void CancelScheduledTasks()
{
Contract.Assert(this.InEventLoop);
PriorityQueue<IScheduledRunnable> scheduledTaskQueue = this.ScheduledTaskQueue;
if (IsNullOrEmpty(scheduledTaskQueue))
{
return;
}
IScheduledRunnable[] tasks = scheduledTaskQueue.ToArray();
foreach (IScheduledRunnable t in tasks)
{
t.Cancel();
}
this.ScheduledTaskQueue.Clear();
}
protected IScheduledRunnable PollScheduledTask() => this.PollScheduledTask(GetNanos());
protected IScheduledRunnable PollScheduledTask(PreciseTimeSpan nanoTime)
{
Contract.Assert(this.InEventLoop);
IScheduledRunnable scheduledTask = this.ScheduledTaskQueue.Peek();
if (scheduledTask == null)
{
return null;
}
if (scheduledTask.Deadline <= nanoTime)
{
this.ScheduledTaskQueue.Dequeue();
return scheduledTask;
}
return null;
}
protected PreciseTimeSpan NextScheduledTaskNanos()
{
IScheduledRunnable nextScheduledRunnable = this.PeekScheduledTask();
return nextScheduledRunnable?.Deadline ?? PreciseTimeSpan.MinusOne;
}
protected IScheduledRunnable PeekScheduledTask()
{
PriorityQueue<IScheduledRunnable> scheduledTaskQueue = this.ScheduledTaskQueue;
return IsNullOrEmpty(scheduledTaskQueue) ? null : scheduledTaskQueue.Peek();
}
protected bool HasScheduledTasks()
{
IScheduledRunnable scheduledTask = this.ScheduledTaskQueue.Peek();
return scheduledTask != null && scheduledTask.Deadline <= PreciseTimeSpan.FromStart;
}
public override IScheduledTask Schedule(IRunnable action, TimeSpan delay)
{
Contract.Requires(action != null);
return this.Schedule(new RunnableScheduledTask(this, action, PreciseTimeSpan.Deadline(delay)));
}
public override IScheduledTask Schedule(Action action, TimeSpan delay)
{
Contract.Requires(action != null);
return this.Schedule(new ActionScheduledTask(this, action, PreciseTimeSpan.Deadline(delay)));
}
public override IScheduledTask Schedule(Action<object> action, object state, TimeSpan delay)
{
Contract.Requires(action != null);
return this.Schedule(new StateActionScheduledTask(this, action, state, PreciseTimeSpan.Deadline(delay)));
}
public override IScheduledTask Schedule(Action<object, object> action, object context, object state, TimeSpan delay)
{
Contract.Requires(action != null);
return this.Schedule(new StateActionWithContextScheduledTask(this, action, context, state, PreciseTimeSpan.Deadline(delay)));
}
public override IScheduledTask ScheduleAtFixedRate(Action action, TimeSpan initialDelay, TimeSpan period)
{
Contract.Requires(action != null);
return this.Schedule(new FixedRateScheduledTask(this, action, PreciseTimeSpan.Deadline(initialDelay), PreciseTimeSpan.FromTimeSpan(period)));
}
public override IScheduledTask ScheduleAtFixedRate(IRunnable action, TimeSpan initialDelay, TimeSpan period)
{
Contract.Requires(action != null);
return this.Schedule(new FixedRateScheduledTask(this, action, PreciseTimeSpan.Deadline(initialDelay), PreciseTimeSpan.FromTimeSpan(period)));
}
public override IScheduledTask ScheduleWithFixedDelay(Action action, TimeSpan initialDelay, TimeSpan delay)
{
Contract.Requires(action != null);
return this.Schedule(new FixedDelayScheduledTask(this, action, PreciseTimeSpan.Deadline(initialDelay), PreciseTimeSpan.FromTimeSpan(delay)));
}
public override IScheduledTask ScheduleWithFixedDelay(IRunnable action, TimeSpan initialDelay, TimeSpan delay)
{
Contract.Requires(action != null);
return this.Schedule(new FixedDelayScheduledTask(this, action, PreciseTimeSpan.Deadline(initialDelay), PreciseTimeSpan.FromTimeSpan(delay)));
}
public override Task ScheduleAsync(Action action, TimeSpan delay, CancellationToken cancellationToken)
{
Contract.Requires(action != null);
if (cancellationToken.IsCancellationRequested)
{
return TaskEx.Cancelled;
}
if (!cancellationToken.CanBeCanceled)
{
return this.Schedule(action, delay).Completion;
}
return this.Schedule(new ActionScheduledAsyncTask(this, action, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion;
}
public override Task ScheduleAsync(Action<object> action, object state, TimeSpan delay, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return TaskEx.Cancelled;
}
if (!cancellationToken.CanBeCanceled)
{
return this.Schedule(action, state, delay).Completion;
}
return this.Schedule(new StateActionScheduledAsyncTask(this, action, state, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion;
}
public override Task ScheduleAsync(Action<object, object> action, object context, object state, TimeSpan delay, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return TaskEx.Cancelled;
}
if (!cancellationToken.CanBeCanceled)
{
return this.Schedule(action, context, state, delay).Completion;
}
return this.Schedule(new StateActionWithContextScheduledAsyncTask(this, action, context, state, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion;
}
internal IScheduledRunnable Schedule(IScheduledRunnable task)
{
if (this.InEventLoop)
{
this.ScheduledTaskQueue.Enqueue(task);
}
else
{
this.Execute((e, t) => ((AbstractScheduledEventExecutor)e).ScheduledTaskQueue.Enqueue((IScheduledRunnable)t), this, task);
}
return task;
}
internal void RemoveScheduled(IScheduledRunnable task)
{
if (this.InEventLoop)
{
this.ScheduledTaskQueue.Remove(task);
}
else
{
this.Execute((e, t) => ((AbstractScheduledEventExecutor)e).ScheduledTaskQueue.Remove((IScheduledRunnable)t), this, task);
}
}
}
}