| 1561 | } |
| 1562 | |
| 1563 | void Poller::wait() noexcept { |
| 1564 | // Clean up ZOMBIE events (Events we used to want, but don't anymore) |
| 1565 | for (const auto &[NodeFd, FdData] : OldFdDatas) { |
| 1566 | auto Iter = FdDatas.find(NodeFd); |
| 1567 | |
| 1568 | // Case 1: The FD is completely gone. |
| 1569 | if (Iter == FdDatas.end()) { |
| 1570 | if (FdData.ReadEvent) { |
| 1571 | struct kevent KEvent; |
| 1572 | EV_SET(&KEvent, NodeFd, EVFILT_READ, EV_DELETE, 0, 0, nullptr); |
| 1573 | ::kevent(Fd, &KEvent, 1, nullptr, 0, nullptr); |
| 1574 | } |
| 1575 | if (FdData.WriteEvent) { |
| 1576 | struct kevent KEvent; |
| 1577 | EV_SET(&KEvent, NodeFd, EVFILT_WRITE, EV_DELETE, 0, 0, nullptr); |
| 1578 | ::kevent(Fd, &KEvent, 1, nullptr, 0, nullptr); |
| 1579 | } |
| 1580 | } |
| 1581 | // Case 2: The FD is still here, but interests might have changed. |
| 1582 | else { |
| 1583 | if (FdData.WriteEvent && !Iter->second.WriteEvent) { |
| 1584 | struct kevent KEvent; |
| 1585 | EV_SET(&KEvent, NodeFd, EVFILT_WRITE, EV_DELETE, 0, 0, nullptr); |
| 1586 | ::kevent(Fd, &KEvent, 1, nullptr, 0, nullptr); |
| 1587 | } |
| 1588 | if (FdData.ReadEvent && !Iter->second.ReadEvent) { |
| 1589 | struct kevent KEvent; |
| 1590 | EV_SET(&KEvent, NodeFd, EVFILT_READ, EV_DELETE, 0, 0, nullptr); |
| 1591 | ::kevent(Fd, &KEvent, 1, nullptr, 0, nullptr); |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | KEvents.resize(Events.size()); |
| 1597 | const int Count = |
| 1598 | ::kevent(Fd, nullptr, 0, KEvents.data(), KEvents.size(), nullptr); |
| 1599 | if (unlikely(Count < 0)) { |
| 1600 | const auto Error = fromErrNo(errno); |
| 1601 | for (auto &Event : Events) { |
| 1602 | Event.Valid = true; |
| 1603 | Event.error = Error; |
| 1604 | } |
| 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | for (int I = 0; I < Count; ++I) { |
| 1609 | auto &KEvent = KEvents[I]; |
| 1610 | auto &Event = *reinterpret_cast<OptionalEvent *>(KEvent.udata); |
| 1611 | Event.Valid = true; |
| 1612 | Event.error = __WASI_ERRNO_SUCCESS; |
| 1613 | switch (Event.type) { |
| 1614 | case __WASI_EVENTTYPE_CLOCK: |
| 1615 | break; |
| 1616 | case __WASI_EVENTTYPE_FD_READ: { |
| 1617 | Event.fd_readwrite.flags = static_cast<__wasi_eventrwflags_t>(0); |
| 1618 | if (KEvent.flags & EV_EOF) { |
| 1619 | Event.fd_readwrite.flags |= __WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP; |
| 1620 | } |