blob: 1578a4d087c20a634dad0cfb28f2e106fa010d2f [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>
Colin Crossc13a62b2021-12-15 14:56:47 -080021#include <sys/time.h>
Yabin Cui3e4c5952016-07-26 15:03:27 -070022#include <time.h>
23
24#include <functional>
25#include <memory>
26#include <vector>
27
Yabin Cuifaa7b922021-01-11 17:35:57 -080028struct event_base;
29
30namespace simpleperf {
31
Yabin Cui3e4c5952016-07-26 15:03:27 -070032struct IOEvent;
Yabin Cui825e56b2016-08-26 18:25:21 -070033typedef IOEvent* IOEventRef;
Yabin Cui3e4c5952016-07-26 15:03:27 -070034
Yabin Cuic3cc93b2022-03-31 16:46:16 -070035enum IOEventPriority {
36 // Lower value means higher priority.
37 IOEventHighPriority = 0,
38 IOEventLowPriority = 1,
39};
40
Yabin Cui3e4c5952016-07-26 15:03:27 -070041// 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.
44class IOEventLoop {
45 public:
46 IOEventLoop();
47 ~IOEventLoop();
48
Yabin Cui76624282017-10-23 12:02:28 -070049 // Use precise timer for periodic events which want precision in ms.
50 bool UsePreciseTimer();
51
Yabin Cuidbbda302016-07-28 12:55:41 -070052 // Register a read Event, so [callback] is called when [fd] can be read
Yabin Cui825e56b2016-08-26 18:25:21 -070053 // without blocking. If registered successfully, return the reference
54 // to control the Event, otherwise return nullptr.
Yabin Cuic3cc93b2022-03-31 16:46:16 -070055 IOEventRef AddReadEvent(int fd, const std::function<bool()>& callback,
56 IOEventPriority priority = IOEventLowPriority);
Yabin Cuidbbda302016-07-28 12:55:41 -070057
Yabin Cuia383c112016-10-19 11:04:56 -070058 // Register a write Event, so [callback] is called when [fd] can be written
59 // without blocking.
Yabin Cuic3cc93b2022-03-31 16:46:16 -070060 IOEventRef AddWriteEvent(int fd, const std::function<bool()>& callback,
61 IOEventPriority priority = IOEventLowPriority);
Yabin Cuia383c112016-10-19 11:04:56 -070062
Yabin Cui3e4c5952016-07-26 15:03:27 -070063 // Register a signal Event, so [callback] is called each time signal [sig]
64 // happens.
Yabin Cuic3cc93b2022-03-31 16:46:16 -070065 bool AddSignalEvent(int sig, const std::function<bool()>& callback,
66 IOEventPriority priority = IOEventLowPriority);
Yabin Cui3e4c5952016-07-26 15:03:27 -070067
68 // Register a vector of signal Events.
Yabin Cuic3cc93b2022-03-31 16:46:16 -070069 bool AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback,
70 IOEventPriority priority = IOEventLowPriority);
Yabin Cui3e4c5952016-07-26 15:03:27 -070071
72 // Register a periodic Event, so [callback] is called periodically every
73 // [duration].
Yabin Cuic3cc93b2022-03-31 16:46:16 -070074 IOEventRef AddPeriodicEvent(timeval duration, const std::function<bool()>& callback,
75 IOEventPriority priority = IOEventLowPriority);
Yabin Cui3e4c5952016-07-26 15:03:27 -070076
77 // Run a loop polling for Events. It only exits when ExitLoop() is called
78 // in a callback function of registered Events.
79 bool RunLoop();
80
81 // Exit the loop started by RunLoop().
82 bool ExitLoop();
83
Yabin Cuia383c112016-10-19 11:04:56 -070084 // Disable an Event, which can be enabled later.
85 static bool DisableEvent(IOEventRef ref);
86 // Enable a disabled Event.
87 static bool EnableEvent(IOEventRef ref);
88
Yabin Cui825e56b2016-08-26 18:25:21 -070089 // Unregister an Event.
90 static bool DelEvent(IOEventRef ref);
91
Yabin Cui3e4c5952016-07-26 15:03:27 -070092 private:
93 bool EnsureInit();
Chih-Hung Hsieh84280792018-08-13 10:31:02 -070094 IOEventRef AddEvent(int fd_or_sig, int16_t events, timeval* timeout,
Yabin Cuic3cc93b2022-03-31 16:46:16 -070095 const std::function<bool()>& callback,
96 IOEventPriority priority = IOEventLowPriority);
Chih-Hung Hsieh84280792018-08-13 10:31:02 -070097 static void EventCallbackFn(int, int16_t, void*);
Yabin Cui3e4c5952016-07-26 15:03:27 -070098
99 event_base* ebase_;
100 std::vector<std::unique_ptr<IOEvent>> events_;
101 bool has_error_;
Yabin Cui76624282017-10-23 12:02:28 -0700102 bool use_precise_timer_;
Yabin Cuiacf04b22018-04-18 13:10:40 -0700103 bool in_loop_;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700104};
105
Yabin Cuifaa7b922021-01-11 17:35:57 -0800106} // namespace simpleperf
107
Yabin Cui3e4c5952016-07-26 15:03:27 -0700108#endif // SIMPLE_PERF_IOEVENT_LOOP_H_