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