-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedTimerScheduler.cs
396 lines (321 loc) · 13.3 KB
/
SharedTimerScheduler.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//******************************************************************************************************
// SharedTimerScheduler.cs - Gbtc
//
// Copyright © 2016, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 11/10/2016 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Gemstone.Threading;
/// <summary>
/// Represents a timer manager which is the scheduler of <see cref="SharedTimer"/>.
/// </summary>
/// <remarks>
/// A <see cref="SharedTimer"/> with the same scheduler will use the same ThreadPool thread to process
/// all of the <see cref="SharedTimer"/> instances in series when they have a common interval. Call
/// order, based on registration sequence, will be preserved.
/// </remarks>
public sealed class SharedTimerScheduler : IDisposable
{
#region [ Members ]
// Represents a timer scheduler with of a common fire rate
private class SharedTimerInstance : IDisposable
{
#region [ Members ]
// Fields
private readonly ConcurrentQueue<WeakAction<DateTime>> m_additionalQueueItems;
private readonly LinkedList<WeakAction<DateTime>> m_callbacks;
private readonly Timer m_timer;
private readonly int m_interval;
private readonly SharedTimerScheduler m_parentTimer;
private readonly object m_syncRunning;
private readonly object m_syncStats;
private bool m_disposed;
// Counter of the number of times an interval has been skipped
// because the callbacks did not complete before the timer fired
// again since the last time it was reset
private long m_skippedIntervals;
// Total CPU time spent processing the timer events.
// since the last time it was reset
private double m_elapsedWorkerTime;
// Total number of times the timer events have fired
// since the last time it was reset
private int m_elapsedIntervals;
/// Total number of callbacks that have occurred
private int m_sumOfCallbacks;
/// Count of the number of timer callbacks that exists in this factory
private int m_sharedTimersCount;
#endregion
#region [ Constructors ]
public SharedTimerInstance(SharedTimerScheduler parentTimer, int interval)
{
if (interval <= 0)
throw new ArgumentOutOfRangeException(nameof(interval));
m_parentTimer = parentTimer ?? throw new ArgumentNullException(nameof(parentTimer));
m_additionalQueueItems = new ConcurrentQueue<WeakAction<DateTime>>();
m_syncRunning = new object();
m_syncStats = new object();
m_interval = interval;
m_callbacks = new LinkedList<WeakAction<DateTime>>();
m_timer = new Timer(Callback!, null, interval, interval);
}
#endregion
#region [ Methods ]
public WeakAction<DateTime> RegisterCallback(Action<DateTime> callback)
{
if (m_disposed)
throw new ObjectDisposedException(GetType().FullName);
WeakAction<DateTime> weakAction = new(callback);
m_additionalQueueItems.Enqueue(weakAction);
return weakAction;
}
public WeakAction<DateTime> RegisterCallback(WeakAction<DateTime> weakAction)
{
if (m_disposed)
throw new ObjectDisposedException(GetType().FullName);
m_additionalQueueItems.Enqueue(weakAction);
return weakAction;
}
private void Callback(object state)
{
if (m_disposed)
return;
ShortTime fireTime = ShortTime.Now;
bool lockTaken = false;
try
{
Monitor.TryEnter(m_syncRunning, 0, ref lockTaken);
if (!lockTaken)
{
lock (m_syncStats)
m_skippedIntervals++;
return;
}
DateTime fireTimeDatetime = fireTime.UtcTime;
int loopCount = 0;
LinkedListNode<WeakAction<DateTime>>? timerAction = m_callbacks.First;
while (timerAction is not null)
{
if (m_disposed)
return;
// Removing the linked list item will invalidate the "Next" property, so we store it
LinkedListNode<WeakAction<DateTime>>? nextNode = timerAction.Next;
try
{
if (!timerAction.Value.TryInvoke(fireTimeDatetime))
m_callbacks.Remove(timerAction);
}
catch (Exception ex)
{
LibraryEvents.OnSuppressedException(this, ex);
}
loopCount++;
timerAction = nextNode;
}
lock (m_syncStats)
{
m_sharedTimersCount = m_callbacks.Count;
m_sumOfCallbacks += loopCount;
m_elapsedIntervals++;
m_elapsedWorkerTime += fireTime.ElapsedMilliseconds();
}
WeakAction<DateTime>? newCallbacks;
while (m_additionalQueueItems.TryDequeue(out newCallbacks))
m_callbacks.AddLast(newCallbacks);
if (m_callbacks.Count == 0)
{
lock (m_parentTimer.m_syncRoot)
{
while (m_additionalQueueItems.TryDequeue(out newCallbacks))
m_parentTimer.RegisterCallback(m_interval, newCallbacks);
if (m_callbacks.Count == 0)
{
Dispose();
m_parentTimer.m_schedulesByInterval.Remove(m_interval);
}
}
}
}
finally
{
if (lockTaken)
Monitor.Exit(m_syncRunning);
}
}
public void Dispose()
{
m_disposed = true;
m_timer.Dispose();
}
public void ResetStats()
{
lock (m_syncStats)
{
m_skippedIntervals = 0;
m_elapsedIntervals = 0;
m_elapsedWorkerTime = 0;
m_elapsedIntervals = 0;
m_sumOfCallbacks = 0;
}
}
public string Status
{
get
{
try
{
lock (m_syncStats)
{
StringBuilder status = new();
double averageCpuTime = 0.0D;
if (m_elapsedIntervals > 0)
averageCpuTime = m_elapsedWorkerTime / m_elapsedIntervals;
status.AppendLine($" Shared Timer Interval: {m_interval:N0}");
status.AppendLine($" Skipped Intervals: {m_skippedIntervals:N0}");
status.AppendLine($" Elapsed Intervals: {m_elapsedIntervals:N0}");
status.AppendLine($" Average CPU Time: {averageCpuTime:N2}ms");
status.AppendLine($" Sum of Callbacks: {m_sumOfCallbacks:N0}");
status.AppendLine($" Shared Timer Callbacks: {m_sharedTimersCount:N0}");
status.AppendLine();
return status.ToString();
}
}
catch (Exception ex)
{
LibraryEvents.OnSuppressedException(this, ex);
return Environment.NewLine;
}
}
}
#endregion
}
// Fields
private readonly Dictionary<int, SharedTimerInstance> m_schedulesByInterval;
private readonly object m_syncRoot;
private bool m_disposed;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new instance of the <see cref="SharedTimerScheduler"/> class.
/// </summary>
public SharedTimerScheduler()
{
m_syncRoot = new object();
m_schedulesByInterval = new Dictionary<int, SharedTimerInstance>();
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets flag that determines if this <see cref="SharedTimerScheduler"/> instance has been disposed.
/// </summary>
public bool IsDisposed => m_disposed;
#endregion
#region [ Methods ]
/// <summary>
/// Creates a <see cref="SharedTimer"/> using the current <see cref="SharedTimerScheduler"/>.
/// </summary>
/// <param name="interval">The interval of the timer, default is 100.</param>
/// <returns>A shared timer instance that fires at the given interval.</returns>
public SharedTimer CreateTimer(int interval = 100) => new(this, interval);
/// <summary>
/// Attempts to get status of <see cref="SharedTimerInstance"/> for specified <paramref name="interval"/>.
/// </summary>
/// <param name="interval">The interval of the timer.</param>
/// <returns>Timer status.</returns>
internal string GetStatus(int interval)
{
if (interval <= 0)
return "";
lock (m_syncRoot)
{
if (m_disposed)
return "";
if (m_schedulesByInterval.TryGetValue(interval, out SharedTimerInstance? instance))
return instance?.Status ?? "";
}
return "";
}
/// <summary>
/// Registers the given callback with the timer running at the given interval.
/// </summary>
/// <param name="interval">The interval at which to run the timer.</param>
/// <param name="callback">The action to be performed when the timer is triggered.</param>
/// <returns>
/// The weak reference callback that will be executed when this timer fires. To unregister
/// the callback, call <see cref="WeakAction.Clear"/>.
/// </returns>
internal WeakAction<DateTime> RegisterCallback(int interval, Action<DateTime> callback)
{
if (callback is null)
throw new ArgumentNullException(nameof(callback));
if (interval <= 0)
throw new ArgumentOutOfRangeException(nameof(interval));
if (m_disposed)
throw new ObjectDisposedException(GetType().FullName);
lock (m_syncRoot)
{
if (m_disposed)
throw new ObjectDisposedException(GetType().FullName);
if (!m_schedulesByInterval.TryGetValue(interval, out SharedTimerInstance? instance))
{
instance = new SharedTimerInstance(this, interval);
m_schedulesByInterval.Add(interval, instance);
}
return instance.RegisterCallback(callback);
}
}
// ReSharper disable once UnusedMethodReturnValue.Local
private WeakAction<DateTime> RegisterCallback(int interval, WeakAction<DateTime> weakAction)
{
if (weakAction is null)
throw new ArgumentNullException(nameof(weakAction));
if (interval <= 0)
throw new ArgumentOutOfRangeException(nameof(interval));
if (m_disposed)
throw new ObjectDisposedException(GetType().FullName);
lock (m_syncRoot)
{
if (m_disposed)
throw new ObjectDisposedException(GetType().FullName);
if (!m_schedulesByInterval.TryGetValue(interval, out SharedTimerInstance? instance))
{
instance = new SharedTimerInstance(this, interval);
m_schedulesByInterval.Add(interval, instance);
}
return instance.RegisterCallback(weakAction);
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
lock (m_syncRoot)
{
m_disposed = true;
foreach (SharedTimerInstance instance in m_schedulesByInterval.Values)
instance.Dispose();
m_schedulesByInterval.Clear();
}
}
#endregion
}