EpollEventLoop constructor

EpollEventLoop(
  1. LibC libc
)

Implementation

factory EpollEventLoop(LibC libc) {
  final epollFd = libc.epoll_create1(EPOLL_CLOEXEC);
  if (epollFd < 0) {
    throw LinuxError('Could not create epoll fd.', 'epoll_create1', libc.errno);
  }

  try {
    final eventFd = libc.eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
    if (eventFd < 0) {
      throw LinuxError('Could not create event fd.', 'eventfd', libc.errno);
    }

    try {
      final myReceivePort = ReceivePort();
      final otherSendPort = myReceivePort.sendPort;

      final isolateFuture = Isolate.spawn(
        EpollIsolate.entry,
        Tuple3(otherSendPort, epollFd, eventFd),
      );

      return EpollEventLoop._construct(
        libc: libc,
        epollFd: epollFd,
        eventFd: eventFd,
        receivePort: myReceivePort,
        isolateFuture: isolateFuture,
      );
    } on Object {
      libc.close(eventFd);
      rethrow;
    }
  } on Object {
    libc.close(epollFd);
    rethrow;
  }
}