blob: e804308ae1bbb5b5a9455fddc3507f244b4b04ca [file] [log] [blame]
Sami Kyostila2c6c2f52017-11-21 16:08:16 +00001/*
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
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080017#ifndef PERFETTO_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
18#define PERFETTO_PERFETTO_BASE_UNIX_TASK_RUNNER_H_
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000019
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080020#include "perfetto_base/scoped_file.h"
21#include "perfetto_base/task_runner.h"
22#include "perfetto_base/thread_checker.h"
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000023
24#include <poll.h>
25#include <chrono>
26#include <deque>
27#include <map>
28#include <mutex>
29#include <vector>
30
31namespace perfetto {
32namespace base {
33
34// Runs a task runner on the current thread.
35class UnixTaskRunner : public TaskRunner {
36 public:
37 UnixTaskRunner();
38 ~UnixTaskRunner() override;
39
40 // Start executing tasks. Doesn't return until Quit() is called. Run() may be
41 // called multiple times on the same task runner.
42 void Run();
43 void Quit();
44
Sami Kyostilaeaed9562017-11-22 12:48:10 +000045 // Checks whether there are any pending immediate tasks to run. Note that
46 // delayed tasks don't count even if they are due to run.
47 bool IsIdleForTesting();
48
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000049 // TaskRunner implementation:
50 void PostTask(std::function<void()>) override;
51 void PostDelayedTask(std::function<void()>, int delay_ms) override;
52 void AddFileDescriptorWatch(int fd, std::function<void()>) override;
53 void RemoveFileDescriptorWatch(int fd) override;
54
55 private:
56 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
57 using TimeDurationMs = std::chrono::milliseconds;
58 TimePoint GetTime() const;
59
60 void WakeUp();
61
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000062 void UpdateWatchTasksLocked();
63
64 TimeDurationMs GetDelayToNextTaskLocked() const;
65 void RunImmediateAndDelayedTask();
66 void PostFileDescriptorWatches();
67 void RunFileDescriptorWatch(int fd);
68
69 ThreadChecker thread_checker_;
70
71 ScopedFile control_read_;
72 ScopedFile control_write_;
73
74 std::vector<struct pollfd> poll_fds_;
75
76 // --- Begin lock-protected members ---
77
78 std::mutex lock_;
79
80 std::deque<std::function<void()>> immediate_tasks_;
81 std::multimap<TimePoint, std::function<void()>> delayed_tasks_;
82 bool quit_ = false;
83
Sami Kyostila78b72d32017-11-23 18:08:57 +000084 struct WatchTask {
85 std::function<void()> callback;
86 size_t poll_fd_index; // Index into |poll_fds_|.
87 };
88
89 std::map<int, WatchTask> watch_tasks_;
Sami Kyostila2c6c2f52017-11-21 16:08:16 +000090 bool watch_tasks_changed_ = false;
91
92 // --- End lock-protected members ---
93};
94
95} // namespace base
96} // namespace perfetto
97
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080098#endif // PERFETTO_PERFETTO_BASE_UNIX_TASK_RUNNER_H_