Class EpollEventLoop

Nested Relationships

Nested Types

Inheritance Relationships

Base Type

Class Documentation

class EpollEventLoop : public netio3::BaseEventLoop

The AsioEventLoop class represents an event loop using epoll_wait.

This class uses a native linux epoll_wait instance as the event loop driver. File descriptors are registered with epoll_ctl.

Public Functions

explicit EpollEventLoop(std::function<void()> cb_init = nullptr)
~EpollEventLoop() override
EpollEventLoop(const EpollEventLoop &other) = delete
EpollEventLoop &operator=(const EpollEventLoop &other) = delete
EpollEventLoop(EpollEventLoop &&other) = delete
EpollEventLoop &operator=(EpollEventLoop &&other) = delete
virtual void run() override

Runs the event loop.

The event loop is executed in the caling thread. The function blocks until the event loop is stopped.

The on_init callback is executed before the event loop starts.

Closes all remaining fd when terminating. While stopping waits up to 5 iterations to finish callbacks before force stopping.

void run_for(uint64_t t_sec) override

Runs the event loop for a given time.

The event loop is executed in the calling thread. The function blocks until the event loop is stopped or the given time has passed.

Parameters:

sec – The time to run the event loop in seconds

virtual void register_fd(const EventContext &ctx) override

Registers an event with the event loop.

This function is used to register a file descriptor with the event loop. The registered file descriptor will be monitored for read events.

The file descriptor is not registered immediately but queued and a signal is fired to actually register it. This procedure is used to have a lock free but thread safe registration of file descriptors.

Parameters:

ctx – The event context to register

virtual void remove_fd(int fd, bool close_fd = false, bool wait = false) override

Removes an event from the event loop.

This function is used to remove a file descriptor from the event loop. The file descriptor will no longer be monitored for events.

If close_fd is true, the file descriptor is closed. Otherwise, it is only removed from the event loop.

The file descriptor is not removed immediately but queued and a signal is fired to actually remove it. This procedure is used to have a lock free but thread safe removal of file descriptors.

Parameters:
  • fd – The file descriptor to remove

  • close_fd – Whether to close the file descriptor

  • wait – Whether to wait for outstanding events to finish

virtual void stop() override

Stops the event loop.

The event loop is stopped by setting a flag. The actual stopping is done in the event loop. This allows outstanding events to be processed before stopping.

virtual bool is_running() const override

Checks if the event loop is running.

Returns:

True if the event loop is running, false otherwise