• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INCLUDE_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
18 #define INCLUDE_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
19 
20 #include "perfetto/base/scoped_file.h"
21 #include "perfetto/base/task_runner.h"
22 #include "perfetto/base/thread_checker.h"
23 #include "perfetto/base/time.h"
24 
25 #include <poll.h>
26 #include <chrono>
27 #include <deque>
28 #include <map>
29 #include <mutex>
30 #include <vector>
31 
32 namespace perfetto {
33 namespace base {
34 
35 // Runs a task runner on the current thread.
36 class UnixTaskRunner : public TaskRunner {
37  public:
38   UnixTaskRunner();
39   ~UnixTaskRunner() override;
40 
41   // Start executing tasks. Doesn't return until Quit() is called. Run() may be
42   // called multiple times on the same task runner.
43   void Run();
44   void Quit();
45 
46   // Checks whether there are any pending immediate tasks to run. Note that
47   // delayed tasks don't count even if they are due to run.
48   bool IsIdleForTesting();
49 
50   // TaskRunner implementation:
51   void PostTask(std::function<void()>) override;
52   void PostDelayedTask(std::function<void()>, uint32_t delay_ms) override;
53   void AddFileDescriptorWatch(int fd, std::function<void()>) override;
54   void RemoveFileDescriptorWatch(int fd) override;
55 
56  private:
57   void WakeUp();
58 
59   void UpdateWatchTasksLocked();
60 
61   int GetDelayMsToNextTaskLocked() const;
62   void RunImmediateAndDelayedTask();
63   void PostFileDescriptorWatches();
64   void RunFileDescriptorWatch(int fd);
65 
66   ThreadChecker thread_checker_;
67 
68   ScopedFile control_read_;
69   ScopedFile control_write_;
70 
71   std::vector<struct pollfd> poll_fds_;
72 
73   // --- Begin lock-protected members ---
74 
75   std::mutex lock_;
76 
77   std::deque<std::function<void()>> immediate_tasks_;
78   std::multimap<TimeMillis, std::function<void()>> delayed_tasks_;
79   bool quit_ = false;
80 
81   struct WatchTask {
82     std::function<void()> callback;
83     size_t poll_fd_index;  // Index into |poll_fds_|.
84   };
85 
86   std::map<int, WatchTask> watch_tasks_;
87   bool watch_tasks_changed_ = false;
88 
89   // --- End lock-protected members ---
90 };
91 
92 }  // namespace base
93 }  // namespace perfetto
94 
95 #endif  // INCLUDE_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
96