Android-cuttlefish cvd tool
thread_looper.h
Go to the documentation of this file.
1//
2// Copyright (C) 2020 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#pragma once
17
18#include <atomic>
19#include <chrono>
20#include <condition_variable>
21#include <deque>
22#include <functional>
23#include <mutex>
24#include <thread>
25
26namespace cuttlefish {
27
28template <typename T>
29std::function<void()> makeSafeCallback(T *me, std::function<void(T *)> f) {
30 return [f, me] {
31 if (me) {
32 f(me);
33 }
34 };
35}
36
37template<typename T, typename... Params>
38std::function<void()> makeSafeCallback(
39 T *obj, void (T::*f)(const Params&...), const Params&... params) {
40 return makeSafeCallback<T>(obj,
41 [f, params...](T *me) { (me->*f)(params...); });
42}
43
44template<typename T, typename... Params>
45std::function<void()> makeSafeCallback(
46 T *obj, void (T::*f)(Params...), const Params&... params) {
47 return makeSafeCallback<T>(obj,
48 [f, params...](T *me) { (me->*f)(params...); });
49}
50
52 public:
55
56 ThreadLooper(const ThreadLooper &) = delete;
58
59 typedef std::function<void()> Callback;
60 typedef int32_t Serial;
61
63 Serial Post(Callback cb, std::chrono::steady_clock::duration delay);
64
65 void Stop();
66
67 // Returns true if matching event was canceled.
69
70 private:
71 struct Event {
72 std::chrono::steady_clock::time_point when;
75
76 bool operator<=(const Event &other) const;
77 };
78
80 std::thread looper_thread_;
81
82 std::mutex lock_;
83 std::condition_variable cond_;
84 std::deque<Event> queue_;
85 std::atomic<Serial> next_serial_;
86
87 void ThreadLoop();
88
89 void Insert(const Event &event);
90};
91
92}; // namespace cuttlefish
Definition: thread_looper.h:51
bool stopped_
Definition: thread_looper.h:79
void ThreadLoop()
Definition: thread_looper.cpp:84
void Stop()
Definition: thread_looper.cpp:114
void Insert(const Event &event)
Definition: thread_looper.cpp:72
std::mutex lock_
Definition: thread_looper.h:82
std::condition_variable cond_
Definition: thread_looper.h:83
std::function< void()> Callback
Definition: thread_looper.h:59
ThreadLooper()
Definition: thread_looper.cpp:22
bool CancelSerial(Serial serial)
Definition: thread_looper.cpp:55
ThreadLooper & operator=(const ThreadLooper &)=delete
std::thread looper_thread_
Definition: thread_looper.h:80
~ThreadLooper()
Definition: thread_looper.cpp:27
std::deque< Event > queue_
Definition: thread_looper.h:84
ThreadLooper(const ThreadLooper &)=delete
Serial Post(Callback cb)
Definition: thread_looper.cpp:33
int32_t Serial
Definition: thread_looper.h:60
std::atomic< Serial > next_serial_
Definition: thread_looper.h:85
static const char * serial
Definition: fastboot.cpp:96
Event event
Definition: kernel_log_server.cc:56
Definition: alloc_utils.cpp:23
std::function< void()> makeSafeCallback(T *me, std::function< void(T *)> f)
Definition: thread_looper.h:29
Definition: thread_looper.h:71
Callback cb
Definition: thread_looper.h:73
bool operator<=(const Event &other) const
Definition: thread_looper.cpp:29
std::chrono::steady_clock::time_point when
Definition: thread_looper.h:72
Serial serial
Definition: thread_looper.h:74