Skip to content

Commit

Permalink
wait_for_fd reap events when wait_for_fd in 0 timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings committed Sep 11, 2024
1 parent dfd865d commit b0e32eb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
18 changes: 18 additions & 0 deletions io/epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,24 @@ ok: entry.interests |= eint;
if (ret < 0) LOG_ERROR_RETURN(0, -1, "failed to add event interest");
// if timeout is just simple 0, wait for a tiny little moment
// so that events can be collect.
if (timeout.expired()) {
ret = -1;
wait_for_events(
0,
[&](void* data) __INLINE__ {
if ((thread*)data == CURRENT) {
ret = 0;
} else {
thread_interrupt((thread*)data, EOK);
}
},
[&]() __INLINE__ { return true; });
if (ret < 0) {
rm_interest({fd, interest, 0});
errno = ETIMEDOUT;
}
return ret;
}
ret = thread_usleep(timeout.timeout() ? timeout : Timeout(10));
ERRNO err;
if (ret == -1 && err.no == EOK) {
Expand Down
59 changes: 41 additions & 18 deletions io/kqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,8 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine, public Res
return 0;
}

int wait_for_fd(int fd, uint32_t interests, Timeout timeout) override {
if (unlikely(interests == 0))
return 0;
short ev = (interests == EVENT_READ) ? EVFILT_READ : EVFILT_WRITE;
enqueue(fd, ev, EV_ADD | EV_ONESHOT, 0, CURRENT);
int ret = thread_usleep(timeout.timeout() ? timeout : Timeout(10));
ERRNO err;
if (ret == -1 && err.no == EOK) {
return 0; // event arrived
}

errno = (ret == 0) ? ETIMEDOUT : err.no;
return -1;
}

ssize_t wait_and_fire_events(uint64_t timeout) override {
template<typename EVCB>
ssize_t do_wait_and_fire_events(uint64_t timeout, EVCB&& event_callback) {
ssize_t nev = 0;
struct timespec tm;
tm.tv_sec = timeout / 1000 / 1000;
Expand All @@ -124,8 +110,7 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine, public Res
nev += ret;
for (int i = 0; i < ret; ++i) {
if (_events[i].filter == EVFILT_USER) continue;
auto th = (thread*) _events[i].udata;
if (th) thread_interrupt(th, EOK);
event_callback(_events[i].udata);
}
if (ret == (int) LEN(_events)) { // there may be more events
tm.tv_sec = tm.tv_nsec = 0;
Expand All @@ -134,6 +119,44 @@ class KQueue : public MasterEventEngine, public CascadingEventEngine, public Res
return nev;
}

int wait_for_fd(int fd, uint32_t interests, Timeout timeout) override {
if (unlikely(interests == 0))
return 0;
short ev = (interests == EVENT_READ) ? EVFILT_READ : EVFILT_WRITE;
enqueue(fd, ev, EV_ADD | EV_ONESHOT, 0, CURRENT, timeout.expired());
if (timeout.expired()) {
int ret = -1;
do_wait_and_fire_events(0, [&](void* data) {
auto th = (thread*)data;
if (th == CURRENT)
ret = 0;
else
thread_interrupt(th);
});
if (ret <0) {
enqueue(fd, ev, EV_DELETE, 0, CURRENT, true);
errno = ETIMEDOUT;
}
return ret;
}
int ret = thread_usleep(timeout);
ERRNO err;
if (ret == -1 && err.no == EOK) {
return 0; // event arrived
}

errno = (ret == 0) ? ETIMEDOUT : err.no;
enqueue(fd, ev, EV_DELETE, 0, CURRENT, true);
return -1;
}

ssize_t wait_and_fire_events(uint64_t timeout) override {
return do_wait_and_fire_events(timeout, [](void* data) {
auto th = (thread*)data;
thread_interrupt(th);
});
}

int cancel_wait() override {
enqueue(_kq, EVFILT_USER, EV_ONESHOT, NOTE_TRIGGER, nullptr, true);
return 0;
Expand Down

0 comments on commit b0e32eb

Please sign in to comment.