blob: ee9014aec9558b848ac4d8f6712d328feedc1751 [file] [log] [blame]
Yabin Cui3e4c5952016-07-26 15:03:27 -07001/*
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
Chih-Hung Hsieh84280792018-08-13 10:31:02 -070020#include <stdint.h>
Yabin Cui3e4c5952016-07-26 15:03:27 -070021#include <time.h>
22
23#include <functional>
24#include <memory>
25#include <vector>
26
27struct IOEvent;
Yabin Cui825e56b2016-08-26 18:25:21 -070028typedef IOEvent* IOEventRef;
Yabin Cui3e4c5952016-07-26 15:03:27 -070029struct 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.
34class IOEventLoop {
35 public:
36 IOEventLoop();
37 ~IOEventLoop();
38
Yabin Cui76624282017-10-23 12:02:28 -070039 // Use precise timer for periodic events which want precision in ms.
40 bool UsePreciseTimer();
41
Yabin Cuidbbda302016-07-28 12:55:41 -070042 // Register a read Event, so [callback] is called when [fd] can be read
Yabin Cui825e56b2016-08-26 18:25:21 -070043 // 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);
Yabin Cuidbbda302016-07-28 12:55:41 -070046
Yabin Cuia383c112016-10-19 11:04:56 -070047 // 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
Yabin Cui3e4c5952016-07-26 15:03:27 -070051 // 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].
Yabin Cuic8571d42018-06-06 11:20:39 -070061 IOEventRef AddPeriodicEvent(timeval duration, const std::function<bool()>& callback);
Yabin Cui3e4c5952016-07-26 15:03:27 -070062
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
Yabin Cuia383c112016-10-19 11:04:56 -070070 // 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
Yabin Cui825e56b2016-08-26 18:25:21 -070075 // Unregister an Event.
76 static bool DelEvent(IOEventRef ref);
77
Yabin Cui3e4c5952016-07-26 15:03:27 -070078 private:
79 bool EnsureInit();
Chih-Hung Hsieh84280792018-08-13 10:31:02 -070080 IOEventRef AddEvent(int fd_or_sig, int16_t events, timeval* timeout,
Yabin Cui825e56b2016-08-26 18:25:21 -070081 const std::function<bool()>& callback);
Chih-Hung Hsieh84280792018-08-13 10:31:02 -070082 static void EventCallbackFn(int, int16_t, void*);
Yabin Cui3e4c5952016-07-26 15:03:27 -070083
84 event_base* ebase_;
85 std::vector<std::unique_ptr<IOEvent>> events_;
86 bool has_error_;
Yabin Cui76624282017-10-23 12:02:28 -070087 bool use_precise_timer_;
Yabin Cuiacf04b22018-04-18 13:10:40 -070088 bool in_loop_;
Yabin Cui3e4c5952016-07-26 15:03:27 -070089};
90
91#endif // SIMPLE_PERF_IOEVENT_LOOP_H_