blob: e0cf17513a6fd0d4376a235d6d93c0943e61100f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 <vector>
15
pbos7eb0e232017-01-02 07:32:25 -080016#include "webrtc/base/basictypes.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/sigslot.h"
18#include "webrtc/base/taskparent.h"
19
20namespace rtc {
21class Task;
22
Peter Boström0c4e06b2015-10-07 12:23:21 +020023const int64_t kSecToMsec = 1000;
24const int64_t kMsecTo100ns = 10000;
25const int64_t kSecTo100ns = kSecToMsec * kMsecTo100ns;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
27class TaskRunner : public TaskParent, public sigslot::has_slots<> {
28 public:
29 TaskRunner();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000030 ~TaskRunner() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031
32 virtual void WakeTasks() = 0;
33
34 // Returns the current time in 100ns units. It is used for
35 // determining timeouts. The origin is not important, only
36 // the units and that rollover while the computer is running.
37 //
38 // On Windows, GetSystemTimeAsFileTime is the typical implementation.
Peter Boström0c4e06b2015-10-07 12:23:21 +020039 virtual int64_t CurrentTime() = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040
41 void StartTask(Task *task);
42 void RunTasks();
43 void PollTasks();
44
Peter Boström0c4e06b2015-10-07 12:23:21 +020045 void UpdateTaskTimeout(Task* task, int64_t previous_task_timeout_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
tfarinaa41ab932015-10-30 16:08:48 -070047#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 bool is_ok_to_delete(Task* task) {
49 return task == deleting_task_;
50 }
51
52 void IncrementAbortCount() {
53 ++abort_count_;
54 }
55
56 void DecrementAbortCount() {
57 --abort_count_;
58 }
59#endif
60
61 // Returns the next absolute time when a task times out
62 // OR "0" if there is no next timeout.
Peter Boström0c4e06b2015-10-07 12:23:21 +020063 int64_t next_task_timeout() const;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
65 protected:
66 // The primary usage of this method is to know if
67 // a callback timer needs to be set-up or adjusted.
68 // This method will be called
69 // * when the next_task_timeout() becomes a smaller value OR
70 // * when next_task_timeout() has changed values and the previous
71 // value is in the past.
72 //
73 // If the next_task_timeout moves to the future, this method will *not*
74 // get called (because it subclass should check next_task_timeout()
75 // when its timer goes off up to see if it needs to set-up a new timer).
76 //
77 // Note that this maybe called conservatively. In that it may be
78 // called when no time change has happened.
79 virtual void OnTimeoutChange() {
80 // by default, do nothing.
81 }
82
83 private:
84 void InternalRunTasks(bool in_destructor);
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 void CheckForTimeoutChange(int64_t previous_timeout_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086
87 std::vector<Task *> tasks_;
88 Task *next_timeout_task_;
89 bool tasks_running_;
tfarinaa41ab932015-10-30 16:08:48 -070090#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091 int abort_count_;
92 Task* deleting_task_;
93#endif
94
95 void RecalcNextTimeout(Task *exclude_task);
96};
97
98} // namespace rtc
99
100#endif // TASK_BASE_TASKRUNNER_H__