MessageQueue.h 6.1 KB

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