1 /*
2  * Copyright (C) 2016 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 SIMPLE_PERF_IOEVENT_LOOP_H_
18 #define SIMPLE_PERF_IOEVENT_LOOP_H_
19 
20 #include <stdint.h>
21 #include <sys/time.h>
22 #include <time.h>
23 
24 #include <functional>
25 #include <memory>
26 #include <vector>
27 
28 struct event_base;
29 
30 namespace simpleperf {
31 
32 struct IOEvent;
33 typedef IOEvent* IOEventRef;
34 
35 enum IOEventPriority {
36   // Lower value means higher priority.
37   IOEventHighPriority = 0,
38   IOEventLowPriority = 1,
39 };
40 
41 // IOEventLoop is a class wrapper of libevent, it monitors events happened,
42 // and calls the corresponding callbacks. Possible events are: file ready to
43 // read, file ready to write, signal happens, periodic timer timeout.
44 class IOEventLoop {
45  public:
46   IOEventLoop();
47   ~IOEventLoop();
48 
49   // Register a read Event, so [callback] is called when [fd] can be read
50   // without blocking. If registered successfully, return the reference
51   // to control the Event, otherwise return nullptr.
52   IOEventRef AddReadEvent(int fd, const std::function<bool()>& callback,
53                           IOEventPriority priority = IOEventLowPriority);
54 
55   // Register a write Event, so [callback] is called when [fd] can be written
56   // without blocking.
57   IOEventRef AddWriteEvent(int fd, const std::function<bool()>& callback,
58                            IOEventPriority priority = IOEventLowPriority);
59 
60   // Register a signal Event, so [callback] is called each time signal [sig]
61   // happens.
62   bool AddSignalEvent(int sig, const std::function<bool()>& callback,
63                       IOEventPriority priority = IOEventLowPriority);
64 
65   // Register a vector of signal Events.
66   bool AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback,
67                        IOEventPriority priority = IOEventLowPriority);
68 
69   // Register a periodic Event, so [callback] is called periodically every
70   // [duration].
71   IOEventRef AddPeriodicEvent(timeval duration, const std::function<bool()>& callback,
72                               IOEventPriority priority = IOEventLowPriority);
73 
74   // Register a one time Event, so [callback] is called once after [duration].
75   IOEventRef AddOneTimeEvent(timeval duration, const std::function<bool()>& callback,
76                              IOEventPriority priority = IOEventLowPriority);
77 
78   // Run a loop polling for Events. It only exits when ExitLoop() is called
79   // in a callback function of registered Events.
80   bool RunLoop();
81 
82   // Exit the loop started by RunLoop().
83   bool ExitLoop();
84 
85   // Disable an Event, which can be enabled later.
86   static bool DisableEvent(IOEventRef ref);
87   // Enable a disabled Event.
88   static bool EnableEvent(IOEventRef ref);
89 
90   // Unregister an Event.
91   static bool DelEvent(IOEventRef ref);
92 
93  private:
94   bool EnsureInit();
95   IOEventRef AddEvent(int fd_or_sig, int16_t events, timeval* timeout,
96                       const std::function<bool()>& callback,
97                       IOEventPriority priority = IOEventLowPriority);
98   static void EventCallbackFn(int, int16_t, void*);
99 
100   event_base* ebase_;
101   std::vector<std::unique_ptr<IOEvent>> events_;
102   bool has_error_;
103   bool in_loop_;
104 };
105 
106 }  // namespace simpleperf
107 
108 #endif  // SIMPLE_PERF_IOEVENT_LOOP_H_
109