| OLD | NEW |
| (Empty) |
| 1 // Copyright 2007-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 // QueueTimer is a wrapper for the kernel queue timer. | |
| 17 // | |
| 18 // There are two ways to use the QueueTimer: | |
| 19 // - alarm, where the timer goes off only once. | |
| 20 // - periodic timer. | |
| 21 // When working as an alarm, the timer must be restarted after it fired. There | |
| 22 // is no need to destroy the whole object. | |
| 23 // When working with a periodic timer, the timer can only be started once. | |
| 24 // Alarm timers fire only once, so they will have to be restarted every time. | |
| 25 // It is easy to deadlock when working with timers. As a general rule, never | |
| 26 // destroy a QueueTimer from its callback. | |
| 27 | |
| 28 #ifndef OMAHA_COMMON_QUEUE_TIMER_H__ | |
| 29 #define OMAHA_COMMON_QUEUE_TIMER_H__ | |
| 30 | |
| 31 #include <windows.h> | |
| 32 | |
| 33 #include "base/basictypes.h" | |
| 34 | |
| 35 namespace omaha { | |
| 36 | |
| 37 class QueueTimer { | |
| 38 public: | |
| 39 typedef void (*Callback)(QueueTimer* timer); | |
| 40 | |
| 41 QueueTimer(HANDLE timer_queue, // Caller provided timer queue. | |
| 42 Callback callback, // Callback to call when the timer fires. | |
| 43 void* ctx); // Caller provided context. | |
| 44 | |
| 45 // The destructor waits for the pending callbacks to finish since we do not | |
| 46 // want the callback to fire after the C++ object was destroyed. | |
| 47 ~QueueTimer(); | |
| 48 | |
| 49 // Starts a timer. The time is in milliseconds. | |
| 50 HRESULT Start(int due_time, int period, uint32 flags); | |
| 51 | |
| 52 void* ctx() const { return ctx_; } | |
| 53 | |
| 54 int due_time() const { return due_time_; } | |
| 55 | |
| 56 int period() const { return period_; } | |
| 57 | |
| 58 uint32 flags() const { return flags_; } | |
| 59 | |
| 60 private: | |
| 61 static void _stdcall TimerCallback(void* param, BOOLEAN timer_or_wait); | |
| 62 | |
| 63 HRESULT DoStart(int due_time, int period, uint32 flags); | |
| 64 void DoCallback(); | |
| 65 | |
| 66 CRITICAL_SECTION cs_; // Serializes access to shared state. | |
| 67 CRITICAL_SECTION dtor_cs_; // Serializes the destruction of the object. | |
| 68 DWORD callback_tid_; // The thread id of the callback, if any. | |
| 69 void* ctx_; | |
| 70 int due_time_; | |
| 71 int period_; | |
| 72 uint32 flags_; | |
| 73 HANDLE timer_handle_; | |
| 74 HANDLE timer_queue_; | |
| 75 Callback callback_; | |
| 76 | |
| 77 DISALLOW_EVIL_CONSTRUCTORS(QueueTimer); | |
| 78 }; | |
| 79 | |
| 80 } // namespace omaha | |
| 81 | |
| 82 #endif // OMAHA_COMMON_QUEUE_TIMER_H__ | |
| 83 | |
| OLD | NEW |