Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.1 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: Queue.cpp
  3. //
  4. // Copyright (c) 1999-2000, Microsoft Corporation
  5. //
  6. // This file contains a class to handle a queue element and a class to handle
  7. // a queue of queue elements.
  8. //
  9. // History: 1999-11-07 vtan created
  10. // 2000-08-25 vtan moved from Neptune to Whistler
  11. // --------------------------------------------------------------------------
  12. #include "StandardHeader.h"
  13. #include "Queue.h"
  14. #include "SingleThreadedExecution.h"
  15. // --------------------------------------------------------------------------
  16. // CQueueElement::CQueueElement
  17. //
  18. // Arguments: <none>
  19. //
  20. // Returns: <none>
  21. //
  22. // Purpose: Constructor for CQueueElement.
  23. //
  24. // History: 1999-11-07 vtan created
  25. // 2000-08-25 vtan moved from Neptune to Whistler
  26. // --------------------------------------------------------------------------
  27. CQueueElement::CQueueElement (void) :
  28. _pNextElement(NULL)
  29. {
  30. }
  31. // --------------------------------------------------------------------------
  32. // CQueueElement::~CQueueElement
  33. //
  34. // Arguments: <none>
  35. //
  36. // Returns: <none>
  37. //
  38. // Purpose: Destructor for CQueueElement.
  39. //
  40. // History: 1999-11-07 vtan created
  41. // 2000-08-25 vtan moved from Neptune to Whistler
  42. // --------------------------------------------------------------------------
  43. CQueueElement::~CQueueElement (void)
  44. {
  45. }
  46. // --------------------------------------------------------------------------
  47. // CQueue::CQueue
  48. //
  49. // Arguments: <none>
  50. //
  51. // Returns: <none>
  52. //
  53. // Purpose: Constructor for CQueue.
  54. //
  55. // History: 1999-11-07 vtan created
  56. // 2000-08-25 vtan moved from Neptune to Whistler
  57. // --------------------------------------------------------------------------
  58. CQueue::CQueue (void) :
  59. _pQueue(NULL)
  60. {
  61. }
  62. // --------------------------------------------------------------------------
  63. // CQueue::~CQueue
  64. //
  65. // Arguments: <none>
  66. //
  67. // Returns: <none>
  68. //
  69. // Purpose: Destructor for CQueue.
  70. //
  71. // History: 1999-11-07 vtan created
  72. // 2000-08-25 vtan moved from Neptune to Whistler
  73. // --------------------------------------------------------------------------
  74. CQueue::~CQueue (void)
  75. {
  76. while (_pQueue != NULL)
  77. {
  78. CQueueElement *pNextElement;
  79. pNextElement = _pQueue->_pNextElement;
  80. delete _pQueue;
  81. _pQueue = pNextElement;
  82. }
  83. }
  84. // --------------------------------------------------------------------------
  85. // CQueue::Add
  86. //
  87. // Arguments: pQueueElement = CQueueElement to add to the queue.
  88. //
  89. // Returns: <none>
  90. //
  91. // Purpose: Adds the CQueueElement to the queue. Queue manipulation is
  92. // guarded by a critical section because one thread may be
  93. // queuing elements while another thread is processesing them.
  94. //
  95. // You must provide a dynamically created CQueueElement object.
  96. //
  97. // History: 1999-11-07 vtan created
  98. // 2000-08-25 vtan moved from Neptune to Whistler
  99. // --------------------------------------------------------------------------
  100. void CQueue::Add (CQueueElement *pQueueElement)
  101. {
  102. if (pQueueElement != NULL)
  103. {
  104. CQueueElement *pCurrentElement, *pLastElement;
  105. CSingleThreadedExecution queueLock(_lock);
  106. pLastElement = pCurrentElement = _pQueue;
  107. while (pCurrentElement != NULL)
  108. {
  109. pLastElement = pCurrentElement;
  110. pCurrentElement = pCurrentElement->_pNextElement;
  111. }
  112. if (pLastElement != NULL)
  113. {
  114. pLastElement->_pNextElement = pQueueElement;
  115. }
  116. else
  117. {
  118. _pQueue = pQueueElement;
  119. }
  120. }
  121. }
  122. // --------------------------------------------------------------------------
  123. // CQueue::Remove
  124. //
  125. // Arguments: <none>
  126. //
  127. // Returns: <none>
  128. //
  129. // Purpose: Removes the first element from the queue. The queue is a
  130. // standard FIFO structure. The CQueueElement is deleted. There
  131. // is no reference counting because these are internal items.
  132. //
  133. // History: 1999-11-07 vtan created
  134. // 2000-08-25 vtan moved from Neptune to Whistler
  135. // --------------------------------------------------------------------------
  136. void CQueue::Remove (void)
  137. {
  138. CSingleThreadedExecution queueLock(_lock);
  139. if (_pQueue != NULL)
  140. {
  141. CQueueElement *pNextElement;
  142. pNextElement = _pQueue->_pNextElement;
  143. delete _pQueue;
  144. _pQueue = pNextElement;
  145. }
  146. }
  147. // --------------------------------------------------------------------------
  148. // CQueue::Get
  149. //
  150. // Arguments: <none>
  151. //
  152. // Returns: CQueueElement*
  153. //
  154. // Purpose: Returns the first CQueueElement in the queue.
  155. //
  156. // History: 1999-11-07 vtan created
  157. // 2000-08-25 vtan moved from Neptune to Whistler
  158. // --------------------------------------------------------------------------
  159. CQueueElement* CQueue::Get (void) const
  160. {
  161. return(_pQueue);
  162. }