forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageQueue.h
More file actions
273 lines (228 loc) · 5.86 KB
/
MessageQueue.h
File metadata and controls
273 lines (228 loc) · 5.86 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
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
class MessageBase
{
private:
unsigned int m_time;
unsigned int m_id;
static unsigned int s_messageCount;
MessageBase(const MessageBase&);
public:
MessageBase(unsigned int time) : m_time(time), m_id(s_messageCount++) { }
virtual ~MessageBase() { }
void BeginTimer() { m_time += GetTickCount(); };
unsigned int GetTime() { return m_time; };
unsigned int GetId() { return m_id; };
virtual HRESULT Call(LPCSTR fileName) = 0;
};
template <typename T>
class SortedList
{
template <typename T>
struct DListNode
{
T data;
DListNode<T>* prev;
DListNode<T>* next;
public:
DListNode(const T& data) :
data(data),
prev(nullptr),
next(nullptr)
{ }
};
public:
SortedList():
head(nullptr)
{
}
~SortedList()
{
while (head != nullptr)
{
Remove(head);
}
}
// Scan through the sorted list
// Insert before the first node that satisfies the LessThan function
// This function maintains the invariant that the list is always sorted
void Insert(const T& data)
{
DListNode<T>* curr = head;
DListNode<T>* node = new DListNode<T>(data);
DListNode<T>* prev = nullptr;
// Now, if we have to insert, we have to insert *after* some node
while (curr != nullptr)
{
if (T::LessThan(data, curr->data))
{
break;
}
prev = curr;
curr = curr->next;
}
InsertAfter(node, prev);
}
T Pop()
{
T data = head->data;
Remove(head);
return data;
}
template <typename PredicateFn>
void Remove(PredicateFn fn)
{
DListNode<T>* node = head;
while (node != nullptr)
{
if (fn(node->data))
{
Remove(node);
return;
}
}
}
bool IsEmpty()
{
return head == nullptr;
}
private:
void Remove(DListNode<T>* node)
{
if (node->prev == nullptr)
{
head = node->next;
}
else
{
node->prev->next = node->next;
}
if (node->next != nullptr)
{
node->next->prev = node->prev;
}
delete node;
}
void InsertAfter(DListNode<T>* newNode, DListNode<T>* node)
{
// If the list is empty, just set head to newNode
if (head == nullptr)
{
Assert(node == nullptr);
head = newNode;
return;
}
// If node is null here, we must be trying to insert before head
if (node == nullptr)
{
newNode->next = head;
head->prev = newNode;
head = newNode;
return;
}
Assert(node);
newNode->next = node->next;
newNode->prev = node;
if (node->next)
{
node->next->prev = newNode;
}
node->next = newNode;
}
DListNode<T>* head;
};
class MessageQueue
{
struct ListEntry
{
unsigned int time;
MessageBase* message;
ListEntry(unsigned int time, MessageBase* message):
time(time),
message(message)
{ }
static bool LessThan(const ListEntry& first, const ListEntry& second)
{
return first.time < second.time;
}
};
SortedList<ListEntry> m_queue;
public:
void InsertSorted(MessageBase *message)
{
message->BeginTimer();
unsigned int time = message->GetTime();
m_queue.Insert(ListEntry(time, message));
}
MessageBase* PopAndWait()
{
Assert(!m_queue.IsEmpty());
ListEntry entry = m_queue.Pop();
MessageBase *tmp = entry.message;
int waitTime = tmp->GetTime() - GetTickCount();
if(waitTime > 0)
{
Sleep(waitTime);
}
return tmp;
}
bool IsEmpty()
{
return m_queue.IsEmpty();
}
void RemoveById(unsigned int id)
{
// Search for the message with the correct id, and delete it. Can be updated
// to a hash to improve speed, if necessary.
m_queue.Remove([id](const ListEntry& entry)
{
MessageBase *msg = entry.message;
if(msg->GetId() == id)
{
delete msg;
return true;
}
return false;
});
}
void RemoveAll()
{
m_queue.Remove([](const ListEntry& entry) {
MessageBase* msg = entry.message;
delete msg;
return true;
});
}
HRESULT ProcessAll(LPCSTR fileName)
{
while(!IsEmpty())
{
MessageBase *msg = PopAndWait();
// Omit checking return value for async function, since it shouldn't affect others.
msg->Call(fileName);
delete msg;
ChakraRTInterface::JsTTDNotifyYield();
}
return S_OK;
}
};
//
// A custom message helper class to assist defining messages handled by callback functions.
//
template <class Func, class CustomBase>
class CustomMessage : public CustomBase
{
private:
Func m_func;
public:
CustomMessage(unsigned int time, JsValueRef customArg, const Func& func) :
CustomBase(time, customArg), m_func(func)
{}
virtual HRESULT Call(LPCSTR fileName) override
{
return m_func(*this);
}
};