blob: 9b9662958a04b090a823fa30f181667771c396b5 [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
20#include <time.h>
21
22#include <functional>
23#include <memory>
24#include <vector>
25
26struct IOEvent;
Yabin Cui825e56b2016-08-26 18:25:21 -070027typedef IOEvent* IOEventRef;
Yabin Cui3e4c5952016-07-26 15:03:27 -070028struct event_base;
29
30// IOEventLoop is a class wrapper of libevent, it monitors events happened,
31// and calls the corresponding callbacks. Possible events are: file ready to
32// read, file ready to write, signal happens, periodic timer timeout.
33class IOEventLoop {
34 public:
35 IOEventLoop();
36 ~IOEventLoop();
37
Yabin Cuidbbda302016-07-28 12:55:41 -070038 // Register a read Event, so [callback] is called when [fd] can be read
Yabin Cui825e56b2016-08-26 18:25:21 -070039 // without blocking. If registered successfully, return the reference
40 // to control the Event, otherwise return nullptr.
41 IOEventRef AddReadEvent(int fd, const std::function<bool()>& callback);
Yabin Cuidbbda302016-07-28 12:55:41 -070042
Yabin Cuia383c112016-10-19 11:04:56 -070043 // Register a write Event, so [callback] is called when [fd] can be written
44 // without blocking.
45 IOEventRef AddWriteEvent(int fd, const std::function<bool()>& callback);
46
Yabin Cui3e4c5952016-07-26 15:03:27 -070047 // Register a signal Event, so [callback] is called each time signal [sig]
48 // happens.
49 bool AddSignalEvent(int sig, const std::function<bool()>& callback);
50
51 // Register a vector of signal Events.
52 bool AddSignalEvents(std::vector<int> sigs,
53 const std::function<bool()>& callback);
54
55 // Register a periodic Event, so [callback] is called periodically every
56 // [duration].
57 bool AddPeriodicEvent(timeval duration,
58 const std::function<bool()>& callback);
59
60 // Run a loop polling for Events. It only exits when ExitLoop() is called
61 // in a callback function of registered Events.
62 bool RunLoop();
63
64 // Exit the loop started by RunLoop().
65 bool ExitLoop();
66
Yabin Cuia383c112016-10-19 11:04:56 -070067 // Disable an Event, which can be enabled later.
68 static bool DisableEvent(IOEventRef ref);
69 // Enable a disabled Event.
70 static bool EnableEvent(IOEventRef ref);
71
Yabin Cui825e56b2016-08-26 18:25:21 -070072 // Unregister an Event.
73 static bool DelEvent(IOEventRef ref);
74
Yabin Cui3e4c5952016-07-26 15:03:27 -070075 private:
76 bool EnsureInit();
Yabin Cui825e56b2016-08-26 18:25:21 -070077 IOEventRef AddEvent(int fd_or_sig, short events, timeval* timeout,
78 const std::function<bool()>& callback);
Yabin Cui3e4c5952016-07-26 15:03:27 -070079 static void EventCallbackFn(int, short, void*);
80
81 event_base* ebase_;
82 std::vector<std::unique_ptr<IOEvent>> events_;
83 bool has_error_;
84};
85
86#endif // SIMPLE_PERF_IOEVENT_LOOP_H_