blob: f59677a8467cf3f0f9a667a7a977bbe6e0174298 [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
pbosc7c26a02017-01-02 08:42:32 -080014#include <stdint.h>
15
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include <vector>
17
nisseede5da42017-01-12 05:15:36 -080018#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#include "webrtc/base/sigslot.h"
20#include "webrtc/base/taskparent.h"
21
22namespace rtc {
23class Task;
24
Peter Boström0c4e06b2015-10-07 12:23:21 +020025const int64_t kSecToMsec = 1000;
26const int64_t kMsecTo100ns = 10000;
27const int64_t kSecTo100ns = kSecToMsec * kMsecTo100ns;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
29class TaskRunner : public TaskParent, public sigslot::has_slots<> {
30 public:
31 TaskRunner();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000032 ~TaskRunner() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033
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.
Peter Boström0c4e06b2015-10-07 12:23:21 +020041 virtual int64_t CurrentTime() = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
43 void StartTask(Task *task);
44 void RunTasks();
45 void PollTasks();
46
Peter Boström0c4e06b2015-10-07 12:23:21 +020047 void UpdateTaskTimeout(Task* task, int64_t previous_task_timeout_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
nisseede5da42017-01-12 05:15:36 -080049#if RTC_DCHECK_IS_ON
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050 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.
Peter Boström0c4e06b2015-10-07 12:23:21 +020065 int64_t next_task_timeout() const;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
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);
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 void CheckForTimeoutChange(int64_t previous_timeout_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088
89 std::vector<Task *> tasks_;
nisseede5da42017-01-12 05:15:36 -080090 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;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095#endif
96
97 void RecalcNextTimeout(Task *exclude_task);
98};
99
100} // namespace rtc
101
102#endif // TASK_BASE_TASKRUNNER_H__