diff options
Diffstat (limited to 'simpleperf/IOEventLoop.h')
-rw-r--r-- | simpleperf/IOEventLoop.h | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/simpleperf/IOEventLoop.h b/simpleperf/IOEventLoop.h index f0192969..1578a4d0 100644 --- a/simpleperf/IOEventLoop.h +++ b/simpleperf/IOEventLoop.h @@ -18,6 +18,7 @@ #define SIMPLE_PERF_IOEVENT_LOOP_H_ #include <stdint.h> +#include <sys/time.h> #include <time.h> #include <functional> @@ -31,6 +32,12 @@ namespace simpleperf { struct IOEvent; typedef IOEvent* IOEventRef; +enum IOEventPriority { + // Lower value means higher priority. + IOEventHighPriority = 0, + IOEventLowPriority = 1, +}; + // IOEventLoop is a class wrapper of libevent, it monitors events happened, // and calls the corresponding callbacks. Possible events are: file ready to // read, file ready to write, signal happens, periodic timer timeout. @@ -45,22 +52,27 @@ class IOEventLoop { // Register a read Event, so [callback] is called when [fd] can be read // without blocking. If registered successfully, return the reference // to control the Event, otherwise return nullptr. - IOEventRef AddReadEvent(int fd, const std::function<bool()>& callback); + IOEventRef AddReadEvent(int fd, const std::function<bool()>& callback, + IOEventPriority priority = IOEventLowPriority); // Register a write Event, so [callback] is called when [fd] can be written // without blocking. - IOEventRef AddWriteEvent(int fd, const std::function<bool()>& callback); + IOEventRef AddWriteEvent(int fd, const std::function<bool()>& callback, + IOEventPriority priority = IOEventLowPriority); // Register a signal Event, so [callback] is called each time signal [sig] // happens. - bool AddSignalEvent(int sig, const std::function<bool()>& callback); + bool AddSignalEvent(int sig, const std::function<bool()>& callback, + IOEventPriority priority = IOEventLowPriority); // Register a vector of signal Events. - bool AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback); + bool AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback, + IOEventPriority priority = IOEventLowPriority); // Register a periodic Event, so [callback] is called periodically every // [duration]. - IOEventRef AddPeriodicEvent(timeval duration, const std::function<bool()>& callback); + IOEventRef AddPeriodicEvent(timeval duration, const std::function<bool()>& callback, + IOEventPriority priority = IOEventLowPriority); // Run a loop polling for Events. It only exits when ExitLoop() is called // in a callback function of registered Events. @@ -80,7 +92,8 @@ class IOEventLoop { private: bool EnsureInit(); IOEventRef AddEvent(int fd_or_sig, int16_t events, timeval* timeout, - const std::function<bool()>& callback); + const std::function<bool()>& callback, + IOEventPriority priority = IOEventLowPriority); static void EventCallbackFn(int, int16_t, void*); event_base* ebase_; |