henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2006, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_TASKRUNNER_H__ |
| 29 | #define TALK_BASE_TASKRUNNER_H__ |
| 30 | |
| 31 | #include <vector> |
| 32 | |
| 33 | #include "talk/base/basictypes.h" |
| 34 | #include "talk/base/sigslot.h" |
| 35 | #include "talk/base/taskparent.h" |
| 36 | |
| 37 | namespace talk_base { |
| 38 | class Task; |
| 39 | |
| 40 | const int64 kSecToMsec = 1000; |
| 41 | const int64 kMsecTo100ns = 10000; |
| 42 | const int64 kSecTo100ns = kSecToMsec * kMsecTo100ns; |
| 43 | |
| 44 | class TaskRunner : public TaskParent, public sigslot::has_slots<> { |
| 45 | public: |
| 46 | TaskRunner(); |
| 47 | virtual ~TaskRunner(); |
| 48 | |
| 49 | virtual void WakeTasks() = 0; |
| 50 | |
| 51 | // Returns the current time in 100ns units. It is used for |
| 52 | // determining timeouts. The origin is not important, only |
| 53 | // the units and that rollover while the computer is running. |
| 54 | // |
| 55 | // On Windows, GetSystemTimeAsFileTime is the typical implementation. |
| 56 | virtual int64 CurrentTime() = 0 ; |
| 57 | |
| 58 | void StartTask(Task *task); |
| 59 | void RunTasks(); |
| 60 | void PollTasks(); |
| 61 | |
| 62 | void UpdateTaskTimeout(Task *task, int64 previous_task_timeout_time); |
| 63 | |
| 64 | #ifdef _DEBUG |
| 65 | bool is_ok_to_delete(Task* task) { |
| 66 | return task == deleting_task_; |
| 67 | } |
| 68 | |
| 69 | void IncrementAbortCount() { |
| 70 | ++abort_count_; |
| 71 | } |
| 72 | |
| 73 | void DecrementAbortCount() { |
| 74 | --abort_count_; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | // Returns the next absolute time when a task times out |
| 79 | // OR "0" if there is no next timeout. |
| 80 | int64 next_task_timeout() const; |
| 81 | |
| 82 | protected: |
| 83 | // The primary usage of this method is to know if |
| 84 | // a callback timer needs to be set-up or adjusted. |
| 85 | // This method will be called |
| 86 | // * when the next_task_timeout() becomes a smaller value OR |
| 87 | // * when next_task_timeout() has changed values and the previous |
| 88 | // value is in the past. |
| 89 | // |
| 90 | // If the next_task_timeout moves to the future, this method will *not* |
| 91 | // get called (because it subclass should check next_task_timeout() |
| 92 | // when its timer goes off up to see if it needs to set-up a new timer). |
| 93 | // |
| 94 | // Note that this maybe called conservatively. In that it may be |
| 95 | // called when no time change has happened. |
| 96 | virtual void OnTimeoutChange() { |
| 97 | // by default, do nothing. |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | void InternalRunTasks(bool in_destructor); |
| 102 | void CheckForTimeoutChange(int64 previous_timeout_time); |
| 103 | |
| 104 | std::vector<Task *> tasks_; |
| 105 | Task *next_timeout_task_; |
| 106 | bool tasks_running_; |
| 107 | #ifdef _DEBUG |
| 108 | int abort_count_; |
| 109 | Task* deleting_task_; |
| 110 | #endif |
| 111 | |
| 112 | void RecalcNextTimeout(Task *exclude_task); |
| 113 | }; |
| 114 | |
| 115 | } // namespace talk_base |
| 116 | |
| 117 | #endif // TASK_BASE_TASKRUNNER_H__ |