OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_BASE_TASKRUNNER_H__ | |
12 #define WEBRTC_BASE_TASKRUNNER_H__ | |
13 | |
14 #include <stdint.h> | |
15 | |
16 #include <vector> | |
17 | |
18 #include "webrtc/base/checks.h" | |
19 #include "webrtc/base/sigslot.h" | |
20 #include "webrtc/base/taskparent.h" | |
21 | |
22 namespace rtc { | |
23 class Task; | |
24 | |
25 const int64_t kSecToMsec = 1000; | |
26 const int64_t kMsecTo100ns = 10000; | |
27 const int64_t kSecTo100ns = kSecToMsec * kMsecTo100ns; | |
28 | |
29 class TaskRunner : public TaskParent, public sigslot::has_slots<> { | |
30 public: | |
31 TaskRunner(); | |
32 ~TaskRunner() override; | |
33 | |
34 virtual void WakeTasks() = 0; | |
35 | |
36 // Returns the current time in 100ns units. It is used for | |
37 // determining timeouts. The origin is not important, only | |
38 // the units and that rollover while the computer is running. | |
39 // | |
40 // On Windows, GetSystemTimeAsFileTime is the typical implementation. | |
41 virtual int64_t CurrentTime() = 0; | |
42 | |
43 void StartTask(Task *task); | |
44 void RunTasks(); | |
45 void PollTasks(); | |
46 | |
47 void UpdateTaskTimeout(Task* task, int64_t previous_task_timeout_time); | |
48 | |
49 #if RTC_DCHECK_IS_ON | |
50 bool is_ok_to_delete(Task* task) { | |
51 return task == deleting_task_; | |
52 } | |
53 | |
54 void IncrementAbortCount() { | |
55 ++abort_count_; | |
56 } | |
57 | |
58 void DecrementAbortCount() { | |
59 --abort_count_; | |
60 } | |
61 #endif | |
62 | |
63 // Returns the next absolute time when a task times out | |
64 // OR "0" if there is no next timeout. | |
65 int64_t next_task_timeout() const; | |
66 | |
67 protected: | |
68 // The primary usage of this method is to know if | |
69 // a callback timer needs to be set-up or adjusted. | |
70 // This method will be called | |
71 // * when the next_task_timeout() becomes a smaller value OR | |
72 // * when next_task_timeout() has changed values and the previous | |
73 // value is in the past. | |
74 // | |
75 // If the next_task_timeout moves to the future, this method will *not* | |
76 // get called (because it subclass should check next_task_timeout() | |
77 // when its timer goes off up to see if it needs to set-up a new timer). | |
78 // | |
79 // Note that this maybe called conservatively. In that it may be | |
80 // called when no time change has happened. | |
81 virtual void OnTimeoutChange() { | |
82 // by default, do nothing. | |
83 } | |
84 | |
85 private: | |
86 void InternalRunTasks(bool in_destructor); | |
87 void CheckForTimeoutChange(int64_t previous_timeout_time); | |
88 | |
89 std::vector<Task *> tasks_; | |
90 Task *next_timeout_task_ = nullptr; | |
91 bool tasks_running_ = false; | |
92 #if RTC_DCHECK_IS_ON | |
93 int abort_count_ = 0; | |
94 Task* deleting_task_ = nullptr; | |
95 #endif | |
96 | |
97 void RecalcNextTimeout(Task *exclude_task); | |
98 }; | |
99 | |
100 } // namespace rtc | |
101 | |
102 #endif // TASK_BASE_TASKRUNNER_H__ | |
OLD | NEW |