-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimerQueue.cs
More file actions
159 lines (137 loc) · 5.61 KB
/
Copy pathTimerQueue.cs
File metadata and controls
159 lines (137 loc) · 5.61 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
using System;
using System.Collections.Generic;
using System.Threading;
namespace TimerQueueTimer
{
// Represents Timer-queue that enable you to specify
// callback functions to be called at a specified time.
public class TimerQueue
{
// Represents an event in the event (timer) queue.
class TimerQueueEvent : ICloneable
{
// The routine to be invoked on time-out.
public WaitCallback CallBack { get; set; }
// The argument to be passed to routine on time-out.
public Object State { get; set; }
// The time in future when the routine gets invoked.
public TimeSpan TimeOut { get; set; }
public object Clone()
{
return this.MemberwiseClone();
}
}
// Used to make the heap as min heap, we store the events
// in the queue such that event with minium time-out must
// be in the top.
public class DoubleComparer : IComparer<double>
{
public int Compare(double value1, double value2)
{
return (int)(value1 - value2);
}
}
// EventQueue uses a Min-Heap with Time-out as priority.
Heap<double, TimerQueueEvent> eventQueue;
// Wait handle for signalling, used by event processor thread
// to wait for a new event to come or wait for an event to
// time-out
// Overhead involved in calling AutoResetEvent::Set is 1000
// nano seconds (0.001 microseconds)
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
// true for this flag indicates user called 'Initialize' hence
// event thread is ready for processing.
bool initialized = false;
// The event processor thread.
Thread eventThread;
// Used to notify the event processor thread that new event request created and queued
TimerQueueEvent newEvent;
// To synchronize access to event (timer) queue object between main thread
// and event processor thread.
readonly object synchNewEventLock = new object();
// Creates new TimerQueue where queueSize is the maximum number
// of events that can stay in the queue at any given point of
// time.
public TimerQueue(int queueSize)
{
eventQueue = new Heap<double, TimerQueueEvent>(queueSize,
new DoubleComparer(), null);
eventThread = new Thread(new ThreadStart(this.EventProcessor));
}
// Initializes the TimerQueue
public void Initialize()
{
initialized = true;
eventThread.Start();
}
// This timer expires at the specified wait time, when the timer
// expires, the callback function is called. waitTimeInMs is
// amount of time in milliseconds relative to the current time
// that must elapse before the timer invokes callBack.
public void SetTimer(WaitCallback callBack, Object state, double waitTimeInMs)
{
if (!initialized)
throw new InvalidOperationException("method Initialize must be called before using the TimerQueue for scheduling actions");
lock (synchNewEventLock)
{
// prepare a new event.
newEvent = new TimerQueueEvent
{
CallBack = callBack,
State = state,
TimeOut =
new TimeSpan(DateTime.Now.AddMilliseconds(waitTimeInMs).Ticks)
};
// add the new event to timer queue
eventQueue.Push(newEvent.TimeOut.TotalMilliseconds, newEvent);
// Signal the arrival of new event to event processor
autoResetEvent.Set();
};
}
// Returns the current time in milliseconds.
private double NowInMilliSeconds
{
get
{
return TimeSpan.FromTicks(DateTime.Now.Ticks).TotalMilliseconds;
}
}
private void EventProcessor()
{
// EventQueue is empty now, wait for an event to come.
autoResetEvent.WaitOne();
while (true)
{
TimerQueueEvent minEvent = null;
lock (synchNewEventLock)
{
minEvent = eventQueue.Peek();
}
if (minEvent == null)
{
// EventQueue is empty, wait for an event to come.
autoResetEvent.WaitOne();
}
else
{
// Calculate how much time we need to wait before processing
// event with minimum time-out and wait for that much time.
double waitTimeInMs =
minEvent.TimeOut.TotalMilliseconds - NowInMilliSeconds;
if (!autoResetEvent.WaitOne((int)waitTimeInMs))
{
// wait timed-out, invoke the callback associated with the
// event we waited for.
lock (synchNewEventLock)
{
minEvent = eventQueue.Pop();
}
ThreadPool.QueueUserWorkItem(minEvent.CallBack, minEvent.State);
}
// wait timed-out or wait released via signal (ManualResetEvent::Set)
// as a result of arrival of new event.
}
}
}
};
}