aboutsummaryrefslogtreecommitdiff
path: root/webrtc/base/taskrunner.h
blob: 9a43aac068106e24392e37ea84ee5e79751b4629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_BASE_TASKRUNNER_H__
#define WEBRTC_BASE_TASKRUNNER_H__

#include <vector>

#include "webrtc/base/basictypes.h"
#include "webrtc/base/sigslot.h"
#include "webrtc/base/taskparent.h"

namespace rtc {
class Task;

const int64_t kSecToMsec = 1000;
const int64_t kMsecTo100ns = 10000;
const int64_t kSecTo100ns = kSecToMsec * kMsecTo100ns;

class TaskRunner : public TaskParent, public sigslot::has_slots<> {
 public:
  TaskRunner();
  ~TaskRunner() override;

  virtual void WakeTasks() = 0;

  // Returns the current time in 100ns units.  It is used for
  // determining timeouts.  The origin is not important, only
  // the units and that rollover while the computer is running.
  //
  // On Windows, GetSystemTimeAsFileTime is the typical implementation.
  virtual int64_t CurrentTime() = 0;

  void StartTask(Task *task);
  void RunTasks();
  void PollTasks();

  void UpdateTaskTimeout(Task* task, int64_t previous_task_timeout_time);

#ifdef _DEBUG
  bool is_ok_to_delete(Task* task) {
    return task == deleting_task_;
  }

  void IncrementAbortCount() {
    ++abort_count_;
  }

  void DecrementAbortCount() {
    --abort_count_;
  }
#endif

  // Returns the next absolute time when a task times out
  // OR "0" if there is no next timeout.
  int64_t next_task_timeout() const;

 protected:
  // The primary usage of this method is to know if
  // a callback timer needs to be set-up or adjusted.
  // This method will be called
  //  * when the next_task_timeout() becomes a smaller value OR
  //  * when next_task_timeout() has changed values and the previous
  //    value is in the past.
  //
  // If the next_task_timeout moves to the future, this method will *not*
  // get called (because it subclass should check next_task_timeout()
  // when its timer goes off up to see if it needs to set-up a new timer).
  //
  // Note that this maybe called conservatively.  In that it may be
  // called when no time change has happened.
  virtual void OnTimeoutChange() {
    // by default, do nothing.
  }

 private:
  void InternalRunTasks(bool in_destructor);
  void CheckForTimeoutChange(int64_t previous_timeout_time);

  std::vector<Task *> tasks_;
  Task *next_timeout_task_;
  bool tasks_running_;
#ifdef _DEBUG
  int abort_count_;
  Task* deleting_task_;
#endif

  void RecalcNextTimeout(Task *exclude_task);
};

} // namespace rtc

#endif  // TASK_BASE_TASKRUNNER_H__