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.

93 lines
2.3 KiB

  1. #include "pch.h"
  2. #include "spool.h"
  3. static BOOL bInitialized;
  4. static HANDLE hQuitRequestEvent;
  5. void InitSleeper (void)
  6. {
  7. HANDLE hThread;
  8. DWORD dwThreadId; // The thread id
  9. DBGMSG(DBG_INFO, ("Sleeper: Constructing..\r\n"));
  10. // Initialize member variables
  11. bInitialized = TRUE;
  12. hQuitRequestEvent = NULL;
  13. // These two events are used to synchronize the working thread and the main thread
  14. // The request event is set when the main thread plan to terminate the working thread.
  15. hQuitRequestEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
  16. if (!hQuitRequestEvent)
  17. goto Cleanup;
  18. // Create a working thread
  19. hThread = CreateThread( NULL,0,
  20. (LPTHREAD_START_ROUTINE) SleeperSchedule,
  21. (LPVOID) hQuitRequestEvent,
  22. 0,
  23. &dwThreadId );
  24. // Cleanup the handles
  25. if (!hThread) {
  26. goto Cleanup;
  27. }
  28. else {
  29. CloseHandle (hThread);
  30. }
  31. DBGMSG(DBG_INFO, ("Sleeper: Constructed\r\n"));
  32. return;
  33. Cleanup:
  34. bInitialized = FALSE;
  35. if (hQuitRequestEvent) {
  36. CloseHandle (hQuitRequestEvent);
  37. }
  38. }
  39. void ExitSleeper ()
  40. {
  41. DBGMSG(DBG_INFO, ("Sleeper: Destructing\r\n"));
  42. if (!bInitialized)
  43. return;
  44. DBGMSG(DBG_INFO, ("Sleeper: release event %x\r\n", hQuitRequestEvent));
  45. if (hQuitRequestEvent)
  46. SetEvent (hQuitRequestEvent);
  47. Sleep (1000);
  48. DBGMSG(DBG_INFO, ("Sleeper: Destructed\r\n"));
  49. }
  50. void SleeperSchedule (HANDLE hQuitRequestEvent)
  51. {
  52. DBGMSG(DBG_INFO, ("Sleeper: Schedule\r\n"));
  53. DWORD dwStatus;
  54. while (1) {
  55. DBGMSG(DBG_INFO, ("Sleeper: Waiting for event %x\r\n", hQuitRequestEvent));
  56. dwStatus = WaitForSingleObject (hQuitRequestEvent, SLEEP_INTERVAL);
  57. DBGMSG(DBG_INFO, ("Sleeper: Wait returns %x\r\n", dwStatus));
  58. switch (dwStatus) {
  59. case WAIT_TIMEOUT:
  60. //Time out, continue working
  61. CleanupOldJob ();
  62. break;
  63. default:
  64. // quit if it is either WAIT_OBJECT_0 or any other events
  65. DBGMSG(DBG_INFO, ("Sleeper: The working thread quit\r\n"));
  66. CloseHandle (hQuitRequestEvent);
  67. ExitThread (0);
  68. }
  69. }
  70. }