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.

128 lines
3.1 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // timerq.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // Declares the classes Timer and TimerQueue.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/10/2000 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef TIMERQ_H
  19. #define TIMERQ_H
  20. #if _MSC_VER >= 1000
  21. #pragma once
  22. #endif
  23. #include <iaswin32.h>
  24. #include <map>
  25. class Timer;
  26. class TimerQueue;
  27. typedef std::multimap< ULONG64, Timer* > Timers;
  28. typedef Timers::iterator TimerIterator;
  29. ///////////////////////////////////////////////////////////////////////////////
  30. //
  31. // CLASS
  32. //
  33. // Timer
  34. //
  35. // DESCRIPTION
  36. //
  37. // Abstract base class for Timers that will be executed by a TimerQueue.
  38. //
  39. ///////////////////////////////////////////////////////////////////////////////
  40. class Timer
  41. {
  42. public:
  43. Timer() throw ()
  44. : queue(NULL)
  45. { }
  46. virtual void AddRef() throw() = 0;
  47. virtual void Release() throw () = 0;
  48. virtual void onExpiry() throw () = 0;
  49. void cancelTimer() throw ();
  50. private:
  51. friend class TimerQueue;
  52. TimerQueue* queue; // The current queue or NULL if the timer's not set.
  53. TimerIterator self; // Location of this in the timer queue.
  54. ULONG period; // Period of the timer or zero for one-shots.
  55. // Not implemented.
  56. Timer(const Timer&);
  57. Timer& operator=(const Timer&);
  58. };
  59. ///////////////////////////////////////////////////////////////////////////////
  60. //
  61. // CLASS
  62. //
  63. // TimerQueue
  64. //
  65. // DESCRIPTION
  66. //
  67. // Implements a queue of timers.
  68. //
  69. ///////////////////////////////////////////////////////////////////////////////
  70. class TimerQueue : IAS_CALLBACK
  71. {
  72. public:
  73. TimerQueue();
  74. ~TimerQueue() throw ();
  75. // Set a timer in this queue.
  76. bool setTimer(
  77. Timer* timer,
  78. ULONG dueTime,
  79. ULONG period
  80. ) throw ();
  81. // Cancels all timers. This will block until any executing callbacks have
  82. // completed.
  83. void cancelAllTimers() throw ();
  84. private:
  85. friend class Timer;
  86. void cancelTimer(Timer* timer) throw ();
  87. // Create a new thread to watch the queue.
  88. void createWatcher() throw ();
  89. // Wait for the next timer to expire and execute it.
  90. void executeOneTimer() throw ();
  91. // Add a timer to the queue.
  92. bool queueTimer(Timer* timer, ULONG dueTime) throw ();
  93. // Callback routine for watchers.
  94. static void startRoutine(PIAS_CALLBACK This) throw ();
  95. CriticalSection monitor; // Serialize access.
  96. Event nudge; // Nudge the watcher to recheck the queue.
  97. Event idle; // Have all watchers exited?
  98. Timers queue; // Set of timers orderd by expiry.
  99. Count useCount; // Zero when the queue is idle.
  100. bool hasWatcher; // true if the pool has a watcher thread.
  101. // Not implemented.
  102. TimerQueue(const TimerQueue&);
  103. TimerQueue& operator=(const TimerQueue&);
  104. };
  105. #endif // TIMERQ_H