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