Leaked source code of windows server 2003
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.

243 lines
6.0 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000-2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: JobQueue.cpp
  6. * Content: Job queue for use in the thread pool
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 01/21/2000 jtk Created
  13. ***************************************************************************/
  14. #include "dnmdmi.h"
  15. #undef DPF_SUBCOMP
  16. #define DPF_SUBCOMP DN_SUBCOMP_MODEM
  17. //**********************************************************************
  18. // Constant definitions
  19. //**********************************************************************
  20. //**********************************************************************
  21. // Macro definitions
  22. //**********************************************************************
  23. //**********************************************************************
  24. // Structure definitions
  25. //**********************************************************************
  26. //**********************************************************************
  27. // Variable definitions
  28. //**********************************************************************
  29. //**********************************************************************
  30. // Function prototypes
  31. //**********************************************************************
  32. //**********************************************************************
  33. // Function definitions
  34. //**********************************************************************
  35. //**********************************************************************
  36. // ------------------------------
  37. // CJobQueue::Initialize - initialize
  38. //
  39. // Entry: Nothing
  40. //
  41. // Exit: Boolean indicating success
  42. // TRUE = success
  43. // FALSE = failure
  44. // ------------------------------
  45. #undef DPF_MODNAME
  46. #define DPF_MODNAME "CJobQueue::Initialize"
  47. BOOL CJobQueue::Initialize( void )
  48. {
  49. BOOL fReturn;
  50. m_pQueueHead = NULL;
  51. m_pQueueTail = NULL;
  52. m_hPendingJob = NULL;
  53. //
  54. // initialize
  55. //
  56. fReturn = TRUE;
  57. if ( DNInitializeCriticalSection( &m_Lock ) == FALSE )
  58. {
  59. DPFX(DPFPREP, 0, "Failed to initialize critical section on job queue!" );
  60. goto Failure;
  61. }
  62. DebugSetCriticalSectionGroup( &m_Lock, &g_blDPNModemCritSecsHeld ); // separate dpnmodem CSes from the rest of DPlay's CSes
  63. m_hPendingJob = DNCreateEvent( NULL, // pointer to security attributes (none)
  64. TRUE, // manual reset
  65. FALSE, // start unsignalled
  66. NULL ); // pointer to name (none)
  67. if ( m_hPendingJob == NULL )
  68. {
  69. DPFX(DPFPREP, 0, "Failed to create event for pending job!" );
  70. goto Failure;
  71. }
  72. Exit:
  73. return fReturn;
  74. Failure:
  75. fReturn = FALSE;
  76. Deinitialize();
  77. goto Exit;
  78. }
  79. //**********************************************************************
  80. //**********************************************************************
  81. // ------------------------------
  82. // CJobQueue::Deinitialize - deinitialize
  83. //
  84. // Entry: Nothing
  85. //
  86. // Exit: Nothing
  87. // ------------------------------
  88. #undef DPF_MODNAME
  89. #define DPF_MODNAME "CJobQueue::Deinitialize"
  90. void CJobQueue::Deinitialize( void )
  91. {
  92. DNASSERT( m_pQueueHead == NULL );
  93. DNASSERT( m_pQueueTail == NULL );
  94. DNDeleteCriticalSection( &m_Lock );
  95. if ( m_hPendingJob != NULL )
  96. {
  97. if ( DNCloseHandle( m_hPendingJob ) == FALSE )
  98. {
  99. DWORD dwError;
  100. dwError = GetLastError();
  101. DPFX(DPFPREP, 0, "Problem closing job queue handle" );
  102. DisplayErrorCode( 0, dwError );
  103. }
  104. m_hPendingJob = NULL;
  105. }
  106. }
  107. //**********************************************************************
  108. //**********************************************************************
  109. // ------------------------------
  110. // CJobQueue::SignalPendingJob - set flag to signal a pending job
  111. //
  112. // Entry: Nothing
  113. //
  114. // Exit: Boolean indicating success
  115. // TRUE = success
  116. // FALSE = failure
  117. // ------------------------------
  118. #undef DPF_MODNAME
  119. #define DPF_MODNAME "CJobQueue::SignalPendingJob"
  120. BOOL CJobQueue::SignalPendingJob( void )
  121. {
  122. BOOL fReturn;
  123. //
  124. // initialize
  125. //
  126. fReturn = TRUE;
  127. if ( DNSetEvent( GetPendingJobHandle() ) == FALSE )
  128. {
  129. DWORD dwError;
  130. dwError = GetLastError();
  131. DPFX(DPFPREP, 0, "Cannot set event for pending job!" );
  132. DisplayErrorCode( 0, dwError );
  133. fReturn = FALSE;
  134. }
  135. return fReturn;
  136. }
  137. //**********************************************************************
  138. //**********************************************************************
  139. // ------------------------------
  140. // CJobQueue::EnqueueJob - add a job to the job list
  141. //
  142. // Entry: Pointer to job
  143. //
  144. // Exit: Nothing
  145. // ------------------------------
  146. #undef DPF_MODNAME
  147. #define DPF_MODNAME "CJobQueue::EnqueueJob"
  148. void CJobQueue::EnqueueJob( THREAD_POOL_JOB *const pJob )
  149. {
  150. DNASSERT( pJob != NULL );
  151. AssertCriticalSectionIsTakenByThisThread( &m_Lock, TRUE );
  152. if ( m_pQueueTail != NULL )
  153. {
  154. DNASSERT( m_pQueueHead != NULL );
  155. DNASSERT( m_pQueueTail->pNext == NULL );
  156. m_pQueueTail->pNext = pJob;
  157. }
  158. else
  159. {
  160. m_pQueueHead = pJob;
  161. }
  162. m_pQueueTail = pJob;
  163. pJob->pNext = NULL;
  164. }
  165. //**********************************************************************
  166. //**********************************************************************
  167. // ------------------------------
  168. // CJobQueue::DequeueJob - remove job from job queue
  169. //
  170. // Entry: Nothing
  171. //
  172. // Exit: Pointer to job
  173. // ------------------------------
  174. #undef DPF_MODNAME
  175. #define DPF_MODNAME "CJobQueue::DequeueJob"
  176. THREAD_POOL_JOB *CJobQueue::DequeueJob( void )
  177. {
  178. THREAD_POOL_JOB *pJob;
  179. AssertCriticalSectionIsTakenByThisThread( &m_Lock, TRUE );
  180. pJob = NULL;
  181. if ( m_pQueueHead != NULL )
  182. {
  183. pJob = m_pQueueHead;
  184. m_pQueueHead = pJob->pNext;
  185. if ( m_pQueueHead == NULL )
  186. {
  187. DNASSERT( m_pQueueTail == pJob );
  188. m_pQueueTail = NULL;
  189. }
  190. DEBUG_ONLY( pJob->pNext = NULL );
  191. }
  192. return pJob;
  193. }
  194. //**********************************************************************