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.

238 lines
7.6 KiB

  1. /* Timer.h
  2. *
  3. * Copyright (c) 1993-1995 by DataBeam Corporation, Lexington, KY
  4. *
  5. * Abstract:
  6. * This class maintains the current time and dispathes function calls
  7. * when timers expire. If a routine wants to be called in X amount of
  8. * time, it creates a timer event with this class. When the time expires,
  9. * the function is called. If the user wants to cancel the timer before
  10. * it expires, he can use the DeleteTimerEvent() function.
  11. *
  12. * When a person creates a timer event, one of the parameters is the
  13. * control flags. The user can set the flag so that the event is a
  14. * one-time thing or a constant event that occurs every X milliseconds.
  15. *
  16. * This timer has a millisecond granularity. The time passed into the
  17. * CreateEventTimer() function is assumed to be in milliseconds.
  18. *
  19. * The ProcessTimerEvents() function must be called frequently and
  20. * regularly so that the timeouts occur promptly.
  21. *
  22. * Caveats:
  23. * None.
  24. *
  25. * Author:
  26. * James P. Galvin
  27. * James W. Lawwill
  28. */
  29. #ifndef _TIMER_
  30. #define _TIMER_
  31. /*
  32. ** Possible Error values
  33. */
  34. typedef enum
  35. {
  36. TIMER_NO_ERROR,
  37. TIMER_NO_TIMERS_AVAILABLE,
  38. TIMER_NO_TIMER_MEMORY,
  39. TIMER_INVALID_TIMER_HANDLE
  40. }
  41. TimerError, * PTimerError;
  42. /*
  43. ** These defines are used in the control_flags variable in the TimerEvent
  44. ** structure. TIMER_EVENT_IN_USE is a flag used internal to the timer
  45. ** procedure. TIMER_EVENT_ONE_SHOT can be passed in by the user to
  46. ** signify that the timer should only occur once.
  47. */
  48. #define TIMER_EVENT_IN_USE 0x0001
  49. #define TIMER_EVENT_ONE_SHOT 0x0002
  50. #define TRANSPORT_HASHING_BUCKETS 3
  51. typedef USHORT TimerEventHandle;
  52. typedef TimerEventHandle *PTimerEventHandle;
  53. typedef void (IObject::*PTimerFunction) (TimerEventHandle);
  54. /*
  55. ** Each timer event has a TimerEvent structure associated with it.
  56. */
  57. typedef struct TimerEventStruct
  58. {
  59. TimerEventHandle event_handle;
  60. ULong timer_duration;
  61. ULong total_duration;
  62. IObject *object_ptr;
  63. PTimerFunction timer_func_ptr;
  64. USHORT control_flags;
  65. TimerEventStruct *next_timer_event;
  66. TimerEventStruct *previous_timer_event;
  67. }
  68. TimerEvent, * PTimerEvent;
  69. class Timer
  70. {
  71. public:
  72. Timer (
  73. Void);
  74. ~Timer (
  75. Void);
  76. TimerEventHandle CreateTimerEvent (
  77. ULong timer_duration,
  78. USHORT control_flags,
  79. IObject * object_ptr,
  80. PTimerFunction timer_function);
  81. TimerError DeleteTimerEvent (
  82. TimerEventHandle timer_event);
  83. Void ProcessTimerEvents (
  84. Void);
  85. private:
  86. USHORT Maximum_Timer_Events;
  87. USHORT Timer_Event_Count;
  88. SListClass Timer_Event_Free_Stack;
  89. PTimerEvent First_Timer_Event_In_Chain;
  90. DWORD Last_Timer_Value;
  91. DictionaryClass Timer_List;
  92. };
  93. typedef Timer * PTimer;
  94. extern PTimer System_Timer;
  95. #define InstallTimerEvent(duration, control, func) \
  96. (g_pSystemTimer->CreateTimerEvent((duration),(control),this,(PTimerFunction)(func)))
  97. #endif
  98. /*
  99. * Timer (Void)
  100. *
  101. * Functional Description:
  102. * This is the constructor for the timer class. This procedure gets
  103. * thc current Windows system time.
  104. *
  105. * Formal Parameters:
  106. * None
  107. *
  108. * Return Value:
  109. * None
  110. *
  111. * Side Effects:
  112. * None
  113. *
  114. * Caveats:
  115. * None
  116. */
  117. /*
  118. * ~Timer (Void)
  119. *
  120. * Functional Description:
  121. * This is the destructor for the timer class. This routine frees all
  122. * memory associated with timer events.
  123. *
  124. * Formal Parameters:
  125. * None
  126. *
  127. * Return Value:
  128. * None
  129. *
  130. * Side Effects:
  131. * None
  132. *
  133. * Caveats:
  134. * None
  135. */
  136. /*
  137. * TimerEventHandle CreateTimerEvent (
  138. * ULong timer_duration,
  139. * USHORT control_flags,
  140. * IObject * object_ptr,
  141. * PTimerFunction timer_function);
  142. *
  143. * Functional Description:
  144. * This routine is called to create a timer event. The routine stores the
  145. * information passed-in in a TimerEvent structure. When the timer expires
  146. * the function will be called.
  147. *
  148. * Formal Parameters:
  149. * timer_duration - (i) Amount of time to wait before calling the
  150. * function. The granularity of the timer
  151. * is milliseconds.
  152. * control_flags - (i) This is a USHORT but currently we only
  153. * look at one of the bits. The
  154. * TIMER_EVENT_ONE_SHOT can be passed-in by
  155. * the user if they only want this timeout
  156. * to occur once. If this value is 0, the
  157. * event will occur time after time.
  158. * object_ptr - (i) This is the data address of the object. It
  159. * is the 'this' pointer of the calling object.
  160. * timer_function - (i) This is the address of the function to
  161. * call after the timer expires.
  162. *
  163. * Return Value:
  164. * TimerEventHandle - This is a handle to the timer event. If you
  165. * need to delete the timer event, pass this
  166. * handle to the DeleteTimer() function. A NULL
  167. * handle is returned if the create failed.
  168. *
  169. * Side Effects:
  170. * None
  171. *
  172. * Caveats:
  173. * None
  174. */
  175. /*
  176. * TimerError DeleteTimerEvent (TimerEventHandle timer_event)
  177. *
  178. * Functional Description:
  179. * This routine is called by the user to delete a timer event that is
  180. * currently active.
  181. *
  182. * Formal Parameters:
  183. * timer_event - (i) Handle to a timer event
  184. *
  185. * Return Value:
  186. * TIMER_NO_ERROR - Successful Delete
  187. * TIMER_NO_TIMER_MEMORY - The timer_event can't exist because
  188. * there was never any Timer memory
  189. * TIMER_INVALID_TIMER_HANDLE - timer_event is not in our list
  190. * of timers
  191. *
  192. * Side Effects:
  193. * None
  194. *
  195. * Caveats:
  196. * None
  197. */
  198. /*
  199. * Void ProcessTimerEvents (Void)
  200. *
  201. * Functional Description:
  202. * This routine MUST be called frequently and regularly so that we can
  203. * manage our timers. This function gets the current system time and
  204. * goes through each of the timers to see which have expired. If a timer
  205. * has expired, we call the function associated with it. Upon return,
  206. * if the timer was marked as a one-shot event, we remove it from our
  207. * list of timers.
  208. *
  209. * Formal Parameters:
  210. * None
  211. *
  212. * Return Value:
  213. * None
  214. *
  215. * Side Effects:
  216. * None
  217. *
  218. * Caveats:
  219. * None
  220. */