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