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.

140 lines
2.1 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. timer.c
  5. Abstract:
  6. Work Items Timer
  7. Author:
  8. Stefan Solomon 07/20/1995
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. /*++
  14. Function: StartWiTimer
  15. Descr: Inserts a work item in the timer queue for the specified time
  16. Remark: has to take and release the queues lock
  17. --*/
  18. VOID
  19. StartWiTimer(PWORK_ITEM reqwip,
  20. ULONG timeout)
  21. {
  22. PLIST_ENTRY lep;
  23. PWORK_ITEM timqwip;
  24. reqwip->DueTime = GetTickCount() + timeout;
  25. ACQUIRE_QUEUES_LOCK;
  26. lep = TimerQueue.Blink;
  27. while(lep != &TimerQueue)
  28. {
  29. timqwip = CONTAINING_RECORD(lep, WORK_ITEM, Linkage);
  30. if(IsLater(reqwip->DueTime, timqwip->DueTime)) {
  31. break;
  32. }
  33. lep = lep->Blink;
  34. }
  35. InsertHeadList(lep, &reqwip->Linkage);
  36. SetEvent(WorkerThreadObjects[TIMER_EVENT]);
  37. RELEASE_QUEUES_LOCK;
  38. }
  39. /*++
  40. Function: ProcessTimerQueue
  41. Descr: called when the timer queue due time has come.
  42. Dequeues all wi with expired timeout and queues them in the
  43. workers work items queue
  44. Remark: has to take and release the queues lock
  45. --*/
  46. ULONG
  47. ProcessTimerQueue(VOID)
  48. {
  49. ULONG dueTime = GetTickCount() + MAXULONG/2;
  50. PWORK_ITEM wip;
  51. ACQUIRE_QUEUES_LOCK;
  52. while(!IsListEmpty(&TimerQueue))
  53. {
  54. // check the first in the list
  55. wip = CONTAINING_RECORD(TimerQueue.Flink, WORK_ITEM, Linkage);
  56. if(IsLater(GetTickCount(), wip->DueTime)) {
  57. RemoveEntryList(&wip->Linkage);
  58. RtlQueueWorkItem (ProcessWorkItem , wip, 0);
  59. }
  60. else
  61. {
  62. dueTime = wip->DueTime;
  63. break;
  64. }
  65. }
  66. RELEASE_QUEUES_LOCK;
  67. return dueTime;
  68. }
  69. /*++
  70. Function: FlushTimerQueue
  71. Descr: Dequeues all items in the timer queue and queues them into
  72. the workers work items queue
  73. Remark: has to take and release the queues lock
  74. --*/
  75. VOID
  76. FlushTimerQueue(VOID)
  77. {
  78. PLIST_ENTRY lep;
  79. PWORK_ITEM wip;
  80. ACQUIRE_QUEUES_LOCK;
  81. while(!IsListEmpty(&TimerQueue))
  82. {
  83. lep = RemoveHeadList(&TimerQueue);
  84. wip = CONTAINING_RECORD(lep, WORK_ITEM, Linkage);
  85. RtlQueueWorkItem (ProcessWorkItem , wip, 0);
  86. }
  87. RELEASE_QUEUES_LOCK;
  88. }