xref: /aosp_15_r20/system/extras/simpleperf/IOEventLoop.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #ifndef SIMPLE_PERF_IOEVENT_LOOP_H_
18*288bf522SAndroid Build Coastguard Worker #define SIMPLE_PERF_IOEVENT_LOOP_H_
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <stdint.h>
21*288bf522SAndroid Build Coastguard Worker #include <sys/time.h>
22*288bf522SAndroid Build Coastguard Worker #include <time.h>
23*288bf522SAndroid Build Coastguard Worker 
24*288bf522SAndroid Build Coastguard Worker #include <functional>
25*288bf522SAndroid Build Coastguard Worker #include <memory>
26*288bf522SAndroid Build Coastguard Worker #include <vector>
27*288bf522SAndroid Build Coastguard Worker 
28*288bf522SAndroid Build Coastguard Worker struct event_base;
29*288bf522SAndroid Build Coastguard Worker 
30*288bf522SAndroid Build Coastguard Worker namespace simpleperf {
31*288bf522SAndroid Build Coastguard Worker 
32*288bf522SAndroid Build Coastguard Worker struct IOEvent;
33*288bf522SAndroid Build Coastguard Worker typedef IOEvent* IOEventRef;
34*288bf522SAndroid Build Coastguard Worker 
35*288bf522SAndroid Build Coastguard Worker enum IOEventPriority {
36*288bf522SAndroid Build Coastguard Worker   // Lower value means higher priority.
37*288bf522SAndroid Build Coastguard Worker   IOEventHighPriority = 0,
38*288bf522SAndroid Build Coastguard Worker   IOEventLowPriority = 1,
39*288bf522SAndroid Build Coastguard Worker };
40*288bf522SAndroid Build Coastguard Worker 
41*288bf522SAndroid Build Coastguard Worker // IOEventLoop is a class wrapper of libevent, it monitors events happened,
42*288bf522SAndroid Build Coastguard Worker // and calls the corresponding callbacks. Possible events are: file ready to
43*288bf522SAndroid Build Coastguard Worker // read, file ready to write, signal happens, periodic timer timeout.
44*288bf522SAndroid Build Coastguard Worker class IOEventLoop {
45*288bf522SAndroid Build Coastguard Worker  public:
46*288bf522SAndroid Build Coastguard Worker   IOEventLoop();
47*288bf522SAndroid Build Coastguard Worker   ~IOEventLoop();
48*288bf522SAndroid Build Coastguard Worker 
49*288bf522SAndroid Build Coastguard Worker   // Register a read Event, so [callback] is called when [fd] can be read
50*288bf522SAndroid Build Coastguard Worker   // without blocking. If registered successfully, return the reference
51*288bf522SAndroid Build Coastguard Worker   // to control the Event, otherwise return nullptr.
52*288bf522SAndroid Build Coastguard Worker   IOEventRef AddReadEvent(int fd, const std::function<bool()>& callback,
53*288bf522SAndroid Build Coastguard Worker                           IOEventPriority priority = IOEventLowPriority);
54*288bf522SAndroid Build Coastguard Worker 
55*288bf522SAndroid Build Coastguard Worker   // Register a write Event, so [callback] is called when [fd] can be written
56*288bf522SAndroid Build Coastguard Worker   // without blocking.
57*288bf522SAndroid Build Coastguard Worker   IOEventRef AddWriteEvent(int fd, const std::function<bool()>& callback,
58*288bf522SAndroid Build Coastguard Worker                            IOEventPriority priority = IOEventLowPriority);
59*288bf522SAndroid Build Coastguard Worker 
60*288bf522SAndroid Build Coastguard Worker   // Register a signal Event, so [callback] is called each time signal [sig]
61*288bf522SAndroid Build Coastguard Worker   // happens.
62*288bf522SAndroid Build Coastguard Worker   bool AddSignalEvent(int sig, const std::function<bool()>& callback,
63*288bf522SAndroid Build Coastguard Worker                       IOEventPriority priority = IOEventLowPriority);
64*288bf522SAndroid Build Coastguard Worker 
65*288bf522SAndroid Build Coastguard Worker   // Register a vector of signal Events.
66*288bf522SAndroid Build Coastguard Worker   bool AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback,
67*288bf522SAndroid Build Coastguard Worker                        IOEventPriority priority = IOEventLowPriority);
68*288bf522SAndroid Build Coastguard Worker 
69*288bf522SAndroid Build Coastguard Worker   // Register a periodic Event, so [callback] is called periodically every
70*288bf522SAndroid Build Coastguard Worker   // [duration].
71*288bf522SAndroid Build Coastguard Worker   IOEventRef AddPeriodicEvent(timeval duration, const std::function<bool()>& callback,
72*288bf522SAndroid Build Coastguard Worker                               IOEventPriority priority = IOEventLowPriority);
73*288bf522SAndroid Build Coastguard Worker 
74*288bf522SAndroid Build Coastguard Worker   // Register a one time Event, so [callback] is called once after [duration].
75*288bf522SAndroid Build Coastguard Worker   IOEventRef AddOneTimeEvent(timeval duration, const std::function<bool()>& callback,
76*288bf522SAndroid Build Coastguard Worker                              IOEventPriority priority = IOEventLowPriority);
77*288bf522SAndroid Build Coastguard Worker 
78*288bf522SAndroid Build Coastguard Worker   // Run a loop polling for Events. It only exits when ExitLoop() is called
79*288bf522SAndroid Build Coastguard Worker   // in a callback function of registered Events.
80*288bf522SAndroid Build Coastguard Worker   bool RunLoop();
81*288bf522SAndroid Build Coastguard Worker 
82*288bf522SAndroid Build Coastguard Worker   // Exit the loop started by RunLoop().
83*288bf522SAndroid Build Coastguard Worker   bool ExitLoop();
84*288bf522SAndroid Build Coastguard Worker 
85*288bf522SAndroid Build Coastguard Worker   // Disable an Event, which can be enabled later.
86*288bf522SAndroid Build Coastguard Worker   static bool DisableEvent(IOEventRef ref);
87*288bf522SAndroid Build Coastguard Worker   // Enable a disabled Event.
88*288bf522SAndroid Build Coastguard Worker   static bool EnableEvent(IOEventRef ref);
89*288bf522SAndroid Build Coastguard Worker 
90*288bf522SAndroid Build Coastguard Worker   // Unregister an Event.
91*288bf522SAndroid Build Coastguard Worker   static bool DelEvent(IOEventRef ref);
92*288bf522SAndroid Build Coastguard Worker 
93*288bf522SAndroid Build Coastguard Worker  private:
94*288bf522SAndroid Build Coastguard Worker   bool EnsureInit();
95*288bf522SAndroid Build Coastguard Worker   IOEventRef AddEvent(int fd_or_sig, int16_t events, timeval* timeout,
96*288bf522SAndroid Build Coastguard Worker                       const std::function<bool()>& callback,
97*288bf522SAndroid Build Coastguard Worker                       IOEventPriority priority = IOEventLowPriority);
98*288bf522SAndroid Build Coastguard Worker   static void EventCallbackFn(int, int16_t, void*);
99*288bf522SAndroid Build Coastguard Worker 
100*288bf522SAndroid Build Coastguard Worker   event_base* ebase_;
101*288bf522SAndroid Build Coastguard Worker   std::vector<std::unique_ptr<IOEvent>> events_;
102*288bf522SAndroid Build Coastguard Worker   bool has_error_;
103*288bf522SAndroid Build Coastguard Worker   bool in_loop_;
104*288bf522SAndroid Build Coastguard Worker };
105*288bf522SAndroid Build Coastguard Worker 
106*288bf522SAndroid Build Coastguard Worker }  // namespace simpleperf
107*288bf522SAndroid Build Coastguard Worker 
108*288bf522SAndroid Build Coastguard Worker #endif  // SIMPLE_PERF_IOEVENT_LOOP_H_
109