• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
15 
16 #include "webrtc/base/basictypes.h"
17 #include "webrtc/base/sigslot.h"
18 #include "webrtc/base/taskparent.h"
19 
20 namespace rtc {
21 class Task;
22 
23 const int64_t kSecToMsec = 1000;
24 const int64_t kMsecTo100ns = 10000;
25 const int64_t kSecTo100ns = kSecToMsec * kMsecTo100ns;
26 
27 class TaskRunner : public TaskParent, public sigslot::has_slots<> {
28  public:
29   TaskRunner();
30   ~TaskRunner() override;
31 
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.
39   virtual int64_t CurrentTime() = 0;
40 
41   void StartTask(Task *task);
42   void RunTasks();
43   void PollTasks();
44 
45   void UpdateTaskTimeout(Task* task, int64_t previous_task_timeout_time);
46 
47 #if !defined(NDEBUG)
is_ok_to_delete(Task * task)48   bool is_ok_to_delete(Task* task) {
49     return task == deleting_task_;
50   }
51 
IncrementAbortCount()52   void IncrementAbortCount() {
53     ++abort_count_;
54   }
55 
DecrementAbortCount()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.
63   int64_t next_task_timeout() const;
64 
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.
OnTimeoutChange()79   virtual void OnTimeoutChange() {
80     // by default, do nothing.
81   }
82 
83  private:
84   void InternalRunTasks(bool in_destructor);
85   void CheckForTimeoutChange(int64_t previous_timeout_time);
86 
87   std::vector<Task *> tasks_;
88   Task *next_timeout_task_;
89   bool tasks_running_;
90 #if !defined(NDEBUG)
91   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__
101