Android-cuttlefish cvd tool
fdevent.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006 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 __FDEVENT_H
18#define __FDEVENT_H
19
20#include <stddef.h>
21#include <stdint.h>
22
23#include <atomic>
24#include <chrono>
25#include <deque>
26#include <functional>
27#include <map>
28#include <mutex>
29#include <optional>
30#include <set>
31#include <variant>
32
33#include <android-base/thread_annotations.h>
34#include <android-base/threads.h>
35
36#include "adb_unique_fd.h"
37
38// Events that may be observed
39#define FDE_READ 0x0001
40#define FDE_WRITE 0x0002
41#define FDE_ERROR 0x0004
42#define FDE_TIMEOUT 0x0008
43
44struct fdevent;
45
46typedef void (*fd_func)(int fd, unsigned events, void *userdata);
47typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata);
48
49void invoke_fde(struct fdevent* fde, unsigned events);
50std::string dump_fde(const fdevent* fde);
51
54 unsigned events;
55 fdevent_event(fdevent* pfde, unsigned ev) : fde(pfde), events(ev) {}
56};
57
58struct fdevent final {
59 uint64_t id;
60
62
63 uint16_t state = 0;
64 std::optional<std::chrono::milliseconds> timeout;
65 std::chrono::steady_clock::time_point last_active;
66
67 std::variant<fd_func, fd_func2> func;
68 void* arg = nullptr;
69};
70
72 public:
73 virtual ~fdevent_context() = default;
74
75 // Allocate and initialize a new fdevent object.
76 fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg);
77
78 // Deallocate an fdevent object, returning the file descriptor that was owned by it.
79 // Note that this calls Set, which is a virtual method, so destructors that call this must be
80 // final.
82
83 protected:
84 virtual void Register(fdevent*) = 0;
85 virtual void Unregister(fdevent*) = 0;
86
87 public:
88 // Change which events should cause notifications.
89 virtual void Set(fdevent* fde, unsigned events) = 0;
90 void Add(fdevent* fde, unsigned events);
91 void Del(fdevent* fde, unsigned events);
92
93 // Set a timeout on an fdevent.
94 // If no events are triggered by the timeout, an FDE_TIMEOUT will be generated.
95 // Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will
96 // trigger repeatedly every |timeout| ms.
97 void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
98
99 protected:
100 std::optional<std::chrono::milliseconds> CalculatePollDuration();
101 void HandleEvents(const std::vector<fdevent_event>& events);
102
103 private:
104 // Run all pending functions enqueued via Run().
105 void FlushRunQueue() EXCLUDES(run_queue_mutex_);
106
107 public:
108 // Loop until TerminateLoop is called, handling events.
109 // Implementations should call FlushRunQueue on every iteration, and check the value of
110 // terminate_loop_ to determine whether to stop.
111 virtual void Loop() = 0;
112
113 // Queue an operation to be run on the looper thread.
114 void Run(std::function<void()> fn);
115
116 // Test-only functionality:
117 void TerminateLoop();
118 virtual size_t InstalledCount() = 0;
119
120 std::optional<uint64_t> looper_thread_id_ = std::nullopt;
121
122 protected:
123 // Interrupt the run loop.
124 virtual void Interrupt() = 0;
125
126 // Assert that the caller is executing in the context of the execution
127 // thread that invoked Loop().
128 void CheckLooperThread() const;
129
130 std::atomic<bool> terminate_loop_ = false;
131
133
134 private:
135 uint64_t fdevent_id_ = 0;
137 std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_);
138
140};
141
142// Backwards compatibility shims that forward to the global fdevent_context.
143fdevent* fdevent_create(int fd, fd_func func, void* arg);
144fdevent* fdevent_create(int fd, fd_func2 func, void* arg);
145
147void fdevent_destroy(fdevent* fde);
148
149void fdevent_set(fdevent *fde, unsigned events);
150void fdevent_add(fdevent *fde, unsigned events);
151void fdevent_del(fdevent *fde, unsigned events);
152void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
153void fdevent_loop();
154
155// Queue an operation to run on the looper event thread.
156void fdevent_run_on_looper(std::function<void()> fn);
157
158// The following functions are used only for tests.
161void fdevent_reset();
162
164
165// We use macro so _FILE_ and _FUNCTION_ get the proper value if the check fails.
166
167// Assert that the caller is executing in the context of the execution
168// thread that invoked Loop().
169#define CHECK_LOOPER_THREAD() \
170 if (fdevent_get_ambient()->looper_thread_id_) \
171 CHECK_EQ(*fdevent_get_ambient()->looper_thread_id_, android::base::GetThreadId())
172
173// Assert that the caller is NOT executing in the context of the execution
174// thread that invoked Loop().
175#define CHECK_NOT_LOOPER_THREAD() \
176 if (fdevent_get_ambient()->looper_thread_id_) \
177 CHECK_NE(*fdevent_get_ambient()->looper_thread_id_, android::base::GetThreadId())
178
179#endif
android::base::unique_fd unique_fd
Definition: adb_unique_fd.h:32
unique_fd fdevent_release(fdevent *fde)
Definition: fdevent.cpp:230
void invoke_fde(struct fdevent *fde, unsigned events)
Definition: fdevent.cpp:38
void fdevent_destroy(fdevent *fde)
Definition: fdevent.cpp:234
void fdevent_run_on_looper(std::function< void()> fn)
Definition: fdevent.cpp:254
void fdevent_terminate_loop()
Definition: fdevent.cpp:262
fdevent * fdevent_create(int fd, fd_func func, void *arg)
Definition: fdevent.cpp:220
fdevent_context * fdevent_get_ambient()
Definition: fdevent.cpp:216
size_t fdevent_installed_count()
Definition: fdevent.cpp:266
std::string dump_fde(const fdevent *fde)
Definition: fdevent.cpp:48
void fdevent_add(fdevent *fde, unsigned events)
Definition: fdevent.cpp:242
void(* fd_func2)(struct fdevent *fde, unsigned events, void *userdata)
Definition: fdevent.h:47
void fdevent_set(fdevent *fde, unsigned events)
Definition: fdevent.cpp:238
void fdevent_loop()
Definition: fdevent.cpp:258
void(* fd_func)(int fd, unsigned events, void *userdata)
Definition: fdevent.h:46
void fdevent_del(fdevent *fde, unsigned events)
Definition: fdevent.cpp:246
void fdevent_reset()
Definition: fdevent.cpp:270
void fdevent_set_timeout(fdevent *fde, std::optional< std::chrono::milliseconds > timeout)
Definition: fdevent.cpp:250
absl::string_view userdata
Definition: android_composite_disk_config.cc:57
__u32 set
Definition: nl80211.h:1
Definition: fdevent.h:71
virtual void Loop()=0
std::atomic< bool > terminate_loop_
Definition: fdevent.h:130
virtual void Register(fdevent *)=0
virtual void Unregister(fdevent *)=0
virtual size_t InstalledCount()=0
std::mutex run_queue_mutex_
Definition: fdevent.h:136
unique_fd Destroy(fdevent *fde)
Definition: fdevent.cpp:91
void Del(fdevent *fde, unsigned events)
Definition: fdevent.cpp:115
uint64_t fdevent_id_
Definition: fdevent.h:135
std::set< fdevent * > fdevent_set_
Definition: fdevent.h:139
void FlushRunQueue() EXCLUDES(run_queue_mutex_)
Definition: fdevent.cpp:166
virtual ~fdevent_context()=default
std::optional< std::chrono::milliseconds > CalculatePollDuration()
Definition: fdevent.cpp:127
void CheckLooperThread() const
Definition: fdevent.cpp:183
virtual void Set(fdevent *fde, unsigned events)=0
void SetTimeout(fdevent *fde, std::optional< std::chrono::milliseconds > timeout)
Definition: fdevent.cpp:120
std::map< int, fdevent > installed_fdevents_
Definition: fdevent.h:132
void TerminateLoop()
Definition: fdevent.cpp:198
fdevent * Create(unique_fd fd, std::variant< fd_func, fd_func2 > func, void *arg)
Definition: fdevent.cpp:63
std::optional< uint64_t > looper_thread_id_
Definition: fdevent.h:120
void HandleEvents(const std::vector< fdevent_event > &events)
Definition: fdevent.cpp:154
void Add(fdevent *fde, unsigned events)
Definition: fdevent.cpp:110
void Run(std::function< void()> fn)
Definition: fdevent.cpp:189
virtual void Interrupt()=0
std::deque< std::function< void()> > run_queue_ GUARDED_BY(run_queue_mutex_)
Definition: fdevent.h:52
fdevent_event(fdevent *pfde, unsigned ev)
Definition: fdevent.h:55
fdevent * fde
Definition: fdevent.h:53
unsigned events
Definition: fdevent.h:54
Definition: fdevent.h:58
std::optional< std::chrono::milliseconds > timeout
Definition: fdevent.h:64
std::variant< fd_func, fd_func2 > func
Definition: fdevent.h:67
uint16_t state
Definition: fdevent.h:63
uint64_t id
Definition: fdevent.h:59
void * arg
Definition: fdevent.h:68
unique_fd fd
Definition: fdevent.h:61
std::chrono::steady_clock::time_point last_active
Definition: fdevent.h:65