MessageQueue.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class MessageBase
  7. {
  8. private:
  9. unsigned int m_time;
  10. unsigned int m_id;
  11. static unsigned int s_messageCount;
  12. MessageBase(const MessageBase&);
  13. public:
  14. MessageBase(unsigned int time) : m_time(time), m_id(s_messageCount++) { }
  15. virtual ~MessageBase() { }
  16. void BeginTimer() { m_time += GetTickCount(); };
  17. unsigned int GetTime() { return m_time; };
  18. unsigned int GetId() { return m_id; };
  19. virtual HRESULT Call(LPCSTR fileName) = 0;
  20. };
  21. template <typename T>
  22. class SortedList
  23. {
  24. template <typename T>
  25. struct DListNode
  26. {
  27. T data;
  28. DListNode<T>* prev;
  29. DListNode<T>* next;
  30. public:
  31. DListNode(const T& data) :
  32. data(data),
  33. prev(nullptr),
  34. next(nullptr)
  35. { }
  36. };
  37. public:
  38. SortedList():
  39. head(nullptr)
  40. {
  41. }
  42. ~SortedList()
  43. {
  44. while (head != nullptr)
  45. {
  46. Remove(head);
  47. }
  48. }
  49. // Scan through the sorted list
  50. // Insert before the first node that satisfies the LessThan function
  51. // This function maintains the invariant that the list is always sorted
  52. void Insert(const T& data)
  53. {
  54. DListNode<T>* curr = head;
  55. DListNode<T>* node = new DListNode<T>(data);
  56. DListNode<T>* prev = nullptr;
  57. // Now, if we have to insert, we have to insert *after* some node
  58. while (curr != nullptr)
  59. {
  60. if (T::LessThan(data, curr->data))
  61. {
  62. break;
  63. }
  64. prev = curr;
  65. curr = curr->next;
  66. }
  67. InsertAfter(node, prev);
  68. }
  69. T Pop()
  70. {
  71. T data = head->data;
  72. Remove(head);
  73. return data;
  74. }
  75. template <typename PredicateFn>
  76. void Remove(PredicateFn fn)
  77. {
  78. DListNode<T>* node = head;
  79. while (node != nullptr)
  80. {
  81. if (fn(node->data))
  82. {
  83. Remove(node);
  84. return;
  85. }
  86. }
  87. }
  88. template <typename PredicateFn>
  89. void RemoveAll(PredicateFn fn)
  90. {
  91. while (head != nullptr)
  92. {
  93. fn(head->data);
  94. Remove(head);
  95. }
  96. }
  97. bool IsEmpty()
  98. {
  99. return head == nullptr;
  100. }
  101. private:
  102. void Remove(DListNode<T>* node)
  103. {
  104. if (node->prev == nullptr)
  105. {
  106. head = node->next;
  107. }
  108. else
  109. {
  110. node->prev->next = node->next;
  111. }
  112. if (node->next != nullptr)
  113. {
  114. node->next->prev = node->prev;
  115. }
  116. delete node;
  117. }
  118. void InsertAfter(DListNode<T>* newNode, DListNode<T>* node)
  119. {
  120. // If the list is empty, just set head to newNode
  121. if (head == nullptr)
  122. {
  123. Assert(node == nullptr);
  124. head = newNode;
  125. return;
  126. }
  127. // If node is null here, we must be trying to insert before head
  128. if (node == nullptr)
  129. {
  130. newNode->next = head;
  131. head->prev = newNode;
  132. head = newNode;
  133. return;
  134. }
  135. Assert(node);
  136. newNode->next = node->next;
  137. newNode->prev = node;
  138. if (node->next)
  139. {
  140. node->next->prev = newNode;
  141. }
  142. node->next = newNode;
  143. }
  144. DListNode<T>* head;
  145. };
  146. class MessageQueue
  147. {
  148. struct ListEntry
  149. {
  150. unsigned int time;
  151. MessageBase* message;
  152. ListEntry(unsigned int time, MessageBase* message):
  153. time(time),
  154. message(message)
  155. { }
  156. static bool LessThan(const ListEntry& first, const ListEntry& second)
  157. {
  158. return first.time < second.time;
  159. }
  160. };
  161. SortedList<ListEntry> m_queue;
  162. public:
  163. void InsertSorted(MessageBase *message)
  164. {
  165. message->BeginTimer();
  166. unsigned int time = message->GetTime();
  167. m_queue.Insert(ListEntry(time, message));
  168. }
  169. MessageBase* PopAndWait()
  170. {
  171. Assert(!m_queue.IsEmpty());
  172. ListEntry entry = m_queue.Pop();
  173. MessageBase *tmp = entry.message;
  174. int waitTime = tmp->GetTime() - GetTickCount();
  175. if(waitTime > 0)
  176. {
  177. Sleep(waitTime);
  178. }
  179. return tmp;
  180. }
  181. bool IsEmpty()
  182. {
  183. return m_queue.IsEmpty();
  184. }
  185. void RemoveById(unsigned int id)
  186. {
  187. // Search for the message with the correct id, and delete it. Can be updated
  188. // to a hash to improve speed, if necessary.
  189. m_queue.Remove([id](const ListEntry& entry)
  190. {
  191. MessageBase *msg = entry.message;
  192. if(msg->GetId() == id)
  193. {
  194. delete msg;
  195. return true;
  196. }
  197. return false;
  198. });
  199. }
  200. void RemoveAll()
  201. {
  202. m_queue.RemoveAll([](const ListEntry& entry) {
  203. MessageBase* msg = entry.message;
  204. delete msg;
  205. });
  206. }
  207. HRESULT ProcessAll(LPCSTR fileName)
  208. {
  209. while(!IsEmpty())
  210. {
  211. MessageBase *msg = PopAndWait();
  212. // Omit checking return value for async function, since it shouldn't affect others.
  213. msg->Call(fileName);
  214. delete msg;
  215. ChakraRTInterface::JsTTDNotifyYield();
  216. }
  217. return S_OK;
  218. }
  219. };
  220. //
  221. // A custom message helper class to assist defining messages handled by callback functions.
  222. //
  223. template <class Func, class CustomBase>
  224. class CustomMessage : public CustomBase
  225. {
  226. private:
  227. Func m_func;
  228. public:
  229. CustomMessage(unsigned int time, JsValueRef customArg, const Func& func) :
  230. CustomBase(time, customArg), m_func(func)
  231. {}
  232. virtual HRESULT Call(LPCSTR fileName) override
  233. {
  234. return m_func(*this);
  235. }
  236. };