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.

293 lines
3.9 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. INFSCAN
  5. worker.cpp
  6. Abstract:
  7. WorkerThread and JobItem implementations
  8. Classes to simplify working with threads
  9. History:
  10. Created July 2001 - JamieHun
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. //
  15. // Worker thread basics
  16. //
  17. WorkerThread::WorkerThread()
  18. /*++
  19. Routine Description:
  20. Initialize WorkerThread
  21. --*/
  22. {
  23. ThreadHandle = NULL;
  24. ThreadId = 0;
  25. }
  26. WorkerThread::~WorkerThread()
  27. /*++
  28. Routine Description:
  29. Cleanup WorkerThread
  30. release allocated resources
  31. --*/
  32. {
  33. if(ThreadHandle) {
  34. Wait();
  35. }
  36. }
  37. bool WorkerThread::Begin()
  38. /*++
  39. Routine Description:
  40. Kick off thread
  41. Arguments:
  42. NONE
  43. Return Value:
  44. true if successful
  45. --*/
  46. {
  47. if(ThreadHandle) {
  48. return false;
  49. }
  50. ThreadHandle = reinterpret_cast<HANDLE>(_beginthreadex(NULL,0,WrapWorker,this,CREATE_SUSPENDED,&ThreadId));
  51. if(!ThreadHandle) {
  52. return false;
  53. }
  54. //
  55. // this side of the thread object done, child can continue
  56. //
  57. ResumeThread(ThreadHandle);
  58. return true;
  59. }
  60. unsigned WorkerThread::Wait()
  61. /*++
  62. Routine Description:
  63. Wait until thread terminates
  64. Arguments:
  65. NONE
  66. Return Value:
  67. exit code
  68. --*/
  69. {
  70. if(!ThreadHandle) {
  71. return (unsigned)(-1);
  72. }
  73. WaitForSingleObject(ThreadHandle,INFINITE);
  74. DWORD ExitCode;
  75. if(!GetExitCodeThread(ThreadHandle,&ExitCode)) {
  76. CloseHandle(ThreadHandle);
  77. return (unsigned)(-1);
  78. }
  79. CloseHandle(ThreadHandle);
  80. ThreadHandle = NULL;
  81. return static_cast<unsigned>(ExitCode);
  82. }
  83. unsigned WorkerThread::Worker()
  84. /*++
  85. Routine Description:
  86. overridable thread operation
  87. Arguments:
  88. NONE
  89. Return Value:
  90. exit code
  91. --*/
  92. {
  93. return 0;
  94. }
  95. unsigned WorkerThread::WrapWorker(void * This)
  96. /*++
  97. Routine Description:
  98. _beginthreadex expects static function, invoke real worker
  99. Arguments:
  100. pointer to class instance
  101. Return Value:
  102. exit code
  103. --*/
  104. {
  105. //
  106. // static function, invoke protected member function
  107. //
  108. WorkerThread * TypedThis = reinterpret_cast<WorkerThread*>(This);
  109. if(TypedThis == NULL) {
  110. return 0;
  111. }
  112. return TypedThis->Worker();
  113. }
  114. JobItem::~JobItem()
  115. /*++
  116. Routine Description:
  117. Job variation cleanup
  118. --*/
  119. {
  120. //
  121. // nothing
  122. //
  123. }
  124. int JobItem::Run()
  125. /*++
  126. Routine Description:
  127. Job item dummy Run
  128. Arguments:
  129. NONE
  130. Return Value:
  131. 0 on success
  132. --*/
  133. {
  134. //
  135. // nothing
  136. //
  137. return 0;
  138. }
  139. int JobItem::PartialCleanup()
  140. /*++
  141. Routine Description:
  142. Job item dummy PartialCleanup
  143. Arguments:
  144. NONE
  145. Return Value:
  146. 0 on success
  147. --*/
  148. {
  149. //
  150. // nothing
  151. //
  152. return 0;
  153. }
  154. int JobItem::Results()
  155. /*++
  156. Routine Description:
  157. Job item dummy Results
  158. Arguments:
  159. NONE
  160. Return Value:
  161. 0 on success
  162. --*/
  163. {
  164. //
  165. // nothing
  166. //
  167. return 0;
  168. }
  169. int JobItem::PreResults()
  170. /*++
  171. Routine Description:
  172. Job item dummy PreResults
  173. Arguments:
  174. NONE
  175. Return Value:
  176. 0 on success
  177. --*/
  178. {
  179. //
  180. // nothing
  181. //
  182. return 0;
  183. }
  184. unsigned JobThread::Worker()
  185. /*++
  186. Routine Description:
  187. Job thread. Pull job from GlobalScan::GetNextJob
  188. execute it
  189. do partial cleanup
  190. rinse repeat
  191. Arguments:
  192. NONE
  193. Return Value:
  194. 0 on success
  195. --*/
  196. {
  197. //
  198. // simple task
  199. //
  200. JobEntry * pJob;
  201. if(!pGlobalScan) {
  202. return 0;
  203. }
  204. for(;;) {
  205. pJob = pGlobalScan->GetNextJob();
  206. if(!pJob) {
  207. return 0;
  208. }
  209. int res = pJob->Run();
  210. pJob->PartialCleanup();
  211. if(res != 0) {
  212. return res;
  213. }
  214. }
  215. return 0;
  216. }