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.

254 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1998, Microsoft Corporation
  3. Module Name:
  4. timer.c
  5. Abstract:
  6. This module contains routines for manipulating the timer-queue
  7. which is shared by all the components in this module.
  8. Author:
  9. Abolade Gbadegesin (aboladeg) 1-April-1998
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. HANDLE NhpTimerQueueHandle = NULL;
  15. CRITICAL_SECTION NhpTimerQueueLock;
  16. typedef struct _NH_TIMER_CONTEXT {
  17. WAITORTIMERCALLBACKFUNC TimerRoutine;
  18. PVOID Context;
  19. HANDLE Handle;
  20. } NH_TIMER_CONTEXT, *PNH_TIMER_CONTEXT;
  21. ULONG
  22. NhInitializeTimerManagement(
  23. VOID
  24. )
  25. /*++
  26. Routine Description:
  27. This routine is called to initialize the timer-management module.
  28. Arguments:
  29. none.
  30. Return Value:
  31. ULONG - Win32 status code.
  32. --*/
  33. {
  34. ULONG Error = NO_ERROR;
  35. __try {
  36. InitializeCriticalSection(&NhpTimerQueueLock);
  37. }
  38. __except(EXCEPTION_EXECUTE_HANDLER) {
  39. NhTrace(
  40. TRACE_FLAG_TIMER,
  41. "NhInitializeTimerManagement: exception %d creating lock",
  42. Error = GetExceptionCode()
  43. );
  44. }
  45. return Error;
  46. } // NhInitializeTimerManagement
  47. VOID NTAPI
  48. NhpTimerCallbackRoutine(
  49. PVOID Context,
  50. BOOLEAN TimedOut
  51. )
  52. {
  53. ((PNH_TIMER_CONTEXT)Context)->TimerRoutine(
  54. ((PNH_TIMER_CONTEXT)Context)->Context, TimedOut
  55. );
  56. EnterCriticalSection(&NhpTimerQueueLock);
  57. if (NhpTimerQueueHandle) {
  58. LeaveCriticalSection(&NhpTimerQueueLock);
  59. RtlDeleteTimer(
  60. NhpTimerQueueHandle, ((PNH_TIMER_CONTEXT)Context)->Handle, NULL
  61. );
  62. } else {
  63. LeaveCriticalSection(&NhpTimerQueueLock);
  64. }
  65. NH_FREE(Context);
  66. } // NhpTimerCallbackRoutine
  67. NTSTATUS
  68. NhSetTimer(
  69. PCOMPONENT_REFERENCE Component OPTIONAL,
  70. OUT HANDLE* Handlep OPTIONAL,
  71. WAITORTIMERCALLBACKFUNC TimerRoutine,
  72. PVOID Context,
  73. ULONG DueTime
  74. )
  75. /*++
  76. Routine Description:
  77. This routine is called to install a timer.
  78. Arguments:
  79. Component - optionally supplies a component to be referenced
  80. Handlep - optionally receives the handle of the timer created
  81. TimerRoutine - invoked upon completion of the countdown
  82. Context - supplied to 'TimerRoutine' upon completion of the countdown
  83. DueTime - countdown time in milliseconds
  84. Return Value:
  85. NTSTATUS - status code.
  86. --*/
  87. {
  88. HANDLE Handle;
  89. NTSTATUS status;
  90. PNH_TIMER_CONTEXT TimerContext;
  91. EnterCriticalSection(&NhpTimerQueueLock);
  92. if (!NhpTimerQueueHandle) {
  93. status = RtlCreateTimerQueue(&NhpTimerQueueHandle);
  94. if (!NT_SUCCESS(status)) {
  95. NhpTimerQueueHandle = NULL;
  96. LeaveCriticalSection(&NhpTimerQueueLock);
  97. NhTrace(
  98. TRACE_FLAG_TIMER,
  99. "NhSetTimer: RtlCreateTimerQueue=%x", status
  100. );
  101. return status;
  102. }
  103. }
  104. LeaveCriticalSection(&NhpTimerQueueLock);
  105. if (Component) {
  106. REFERENCE_COMPONENT_OR_RETURN(Component, STATUS_UNSUCCESSFUL);
  107. }
  108. TimerContext =
  109. reinterpret_cast<PNH_TIMER_CONTEXT>(NH_ALLOCATE(sizeof(*TimerContext)));
  110. if (!TimerContext) {
  111. if (Component) { DEREFERENCE_COMPONENT(Component); }
  112. return STATUS_NO_MEMORY;
  113. }
  114. TimerContext->TimerRoutine = TimerRoutine;
  115. TimerContext->Context = Context;
  116. status =
  117. RtlCreateTimer(
  118. NhpTimerQueueHandle,
  119. &TimerContext->Handle,
  120. NhpTimerCallbackRoutine,
  121. TimerContext,
  122. DueTime,
  123. 0,
  124. 0
  125. );
  126. if (!NT_SUCCESS(status)) {
  127. if (Component) { DEREFERENCE_COMPONENT(Component); }
  128. } else if (Handlep) {
  129. *Handlep = TimerContext->Handle;
  130. }
  131. return status;
  132. } // NhSetTimer
  133. VOID
  134. NhShutdownTimerManagement(
  135. VOID
  136. )
  137. /*++
  138. Routine Description:
  139. This routine is called to clean up the timer-management module.
  140. Arguments:
  141. none.
  142. Return Value:
  143. none.
  144. --*/
  145. {
  146. EnterCriticalSection(&NhpTimerQueueLock);
  147. if (NhpTimerQueueHandle) { RtlDeleteTimerQueue(NhpTimerQueueHandle); }
  148. NhpTimerQueueHandle = NULL;
  149. LeaveCriticalSection(&NhpTimerQueueLock);
  150. DeleteCriticalSection(&NhpTimerQueueLock);
  151. } // NhShutdownTimerManagement
  152. NTSTATUS
  153. NhUpdateTimer(
  154. HANDLE Handle,
  155. ULONG DueTime
  156. )
  157. /*++
  158. Routine Description:
  159. This routine modifies the countdown for a timer.
  160. Arguments:
  161. Handle - the handle of the timer to be modified
  162. DueTime - the new countdown in milliseconds
  163. Return Value:
  164. NTSTATUS - status code.
  165. --*/
  166. {
  167. EnterCriticalSection(&NhpTimerQueueLock);
  168. if (!NhpTimerQueueHandle) {
  169. LeaveCriticalSection(&NhpTimerQueueLock);
  170. return STATUS_INVALID_PARAMETER;
  171. }
  172. LeaveCriticalSection(&NhpTimerQueueLock);
  173. return
  174. RtlUpdateTimer(
  175. NhpTimerQueueHandle,
  176. Handle,
  177. DueTime,
  178. 0
  179. );
  180. } // NhUpdateTimer