MessageQueue.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. bool IsEmpty()
  89. {
  90. return head == nullptr;
  91. }
  92. private:
  93. void Remove(DListNode<T>* node)
  94. {
  95. if (node->prev == nullptr)
  96. {
  97. head = node->next;
  98. }
  99. else
  100. {
  101. node->prev->next = node->next;
  102. }
  103. if (node->next != nullptr)
  104. {
  105. node->next->prev = node->prev;
  106. }
  107. delete node;
  108. }
  109. void InsertAfter(DListNode<T>* newNode, DListNode<T>* node)
  110. {
  111. // If the list is empty, just set head to newNode
  112. if (head == nullptr)
  113. {
  114. Assert(node == nullptr);
  115. head = newNode;
  116. return;
  117. }
  118. // If node is null here, we must be trying to insert before head
  119. if (node == nullptr)
  120. {
  121. newNode->next = head;
  122. head->prev = newNode;
  123. head = newNode;
  124. return;
  125. }
  126. Assert(node);
  127. newNode->next = node->next;
  128. newNode->prev = node;
  129. if (node->next)
  130. {
  131. node->next->prev = newNode;
  132. }
  133. node->next = newNode;
  134. }
  135. DListNode<T>* head;
  136. };
  137. class MessageQueue
  138. {
  139. struct ListEntry
  140. {
  141. unsigned int time;
  142. MessageBase* message;
  143. ListEntry(unsigned int time, MessageBase* message):
  144. time(time),
  145. message(message)
  146. { }
  147. static bool LessThan(const ListEntry& first, const ListEntry& second)
  148. {
  149. return first.time < second.time;
  150. }
  151. };
  152. SortedList<ListEntry> m_queue;
  153. public:
  154. void InsertSorted(MessageBase *message)
  155. {
  156. message->BeginTimer();
  157. unsigned int time = message->GetTime();
  158. m_queue.Insert(ListEntry(time, message));
  159. }
  160. MessageBase* PopAndWait()
  161. {
  162. Assert(!m_queue.IsEmpty());
  163. ListEntry entry = m_queue.Pop();
  164. MessageBase *tmp = entry.message;
  165. int waitTime = tmp->GetTime() - GetTickCount();
  166. if(waitTime > 0)
  167. {
  168. Sleep(waitTime);
  169. }
  170. return tmp;
  171. }
  172. bool IsEmpty()
  173. {
  174. return m_queue.IsEmpty();
  175. }
  176. void RemoveById(unsigned int id)
  177. {
  178. // Search for the message with the correct id, and delete it. Can be updated
  179. // to a hash to improve speed, if necessary.
  180. m_queue.Remove([id](const ListEntry& entry)
  181. {
  182. MessageBase *msg = entry.message;
  183. if(msg->GetId() == id)
  184. {
  185. delete msg;
  186. return true;
  187. }
  188. return false;
  189. });
  190. }
  191. void RemoveAll()
  192. {
  193. m_queue.Remove([](const ListEntry& entry) {
  194. MessageBase* msg = entry.message;
  195. delete msg;
  196. return true;
  197. });
  198. }
  199. HRESULT ProcessAll(LPCSTR fileName)
  200. {
  201. while(!IsEmpty())
  202. {
  203. MessageBase *msg = PopAndWait();
  204. // Omit checking return value for async function, since it shouldn't affect others.
  205. msg->Call(fileName);
  206. delete msg;
  207. ChakraRTInterface::JsTTDNotifyYield();
  208. }
  209. return S_OK;
  210. }
  211. };
  212. //
  213. // A custom message helper class to assist defining messages handled by callback functions.
  214. //
  215. template <class Func, class CustomBase>
  216. class CustomMessage : public CustomBase
  217. {
  218. private:
  219. Func m_func;
  220. public:
  221. CustomMessage(unsigned int time, JsValueRef customArg, const Func& func) :
  222. CustomBase(time, customArg), m_func(func)
  223. {}
  224. virtual HRESULT Call(LPCSTR fileName) override
  225. {
  226. return m_func(*this);
  227. }
  228. };