mirror of
https://github.com/janet-lang/janet
synced 2024-11-17 22:24:49 +00:00
Quick blind take on getting kqueue similar to poll and poll.
This commit is contained in:
parent
0ff8f58be8
commit
a3b129845b
@ -1663,9 +1663,7 @@ static void timestamp2timespec(struct timespec *t, JanetTimestamp ts) {
|
|||||||
t->tv_nsec = ts == 0 ? 0 : (ts % 1000) * 1000000;
|
t->tv_nsec = ts == 0 ? 0 : (ts % 1000) * 1000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
JanetListenerState *janet_listen(JanetStream *stream, JanetListener behavior, int mask, size_t size, void *user) {
|
void janet_register_stream(JanetStream *stream) {
|
||||||
JanetListenerState *state = janet_listen_impl(stream, behavior, mask, size, user);
|
|
||||||
if (!(stream->flags & JANET_STREAM_REGISTERED)) {
|
|
||||||
struct kevent kev;
|
struct kevent kev;
|
||||||
EV_SETx(&kev, stream->handle, EVFILT_READ | EVFILT_WRITE, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, stream);
|
EV_SETx(&kev, stream->handle, EVFILT_READ | EVFILT_WRITE, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, stream);
|
||||||
int status;
|
int status;
|
||||||
@ -1675,13 +1673,6 @@ JanetListenerState *janet_listen(JanetStream *stream, JanetListener behavior, in
|
|||||||
if (status == -1) {
|
if (status == -1) {
|
||||||
janet_panicv(janet_ev_lasterr());
|
janet_panicv(janet_ev_lasterr());
|
||||||
}
|
}
|
||||||
stream->flags |= JANET_STREAM_REGISTERED;
|
|
||||||
}
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void janet_unlisten(JanetListenerState *state) {
|
|
||||||
janet_unlisten_impl(state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define JANET_KQUEUE_MAX_EVENTS 64
|
#define JANET_KQUEUE_MAX_EVENTS 64
|
||||||
@ -1721,29 +1712,27 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
|||||||
janet_ev_handle_selfpipe();
|
janet_ev_handle_selfpipe();
|
||||||
} else {
|
} else {
|
||||||
JanetStream *stream = p;
|
JanetStream *stream = p;
|
||||||
JanetListenerState *states[2] = {stream->read_state, stream->write_state};
|
int filt = events[i].filter;
|
||||||
for (int j = 0; j < 2; j++) {
|
int has_err = events[i].flags & EV_ERROR;
|
||||||
JanetListenerState *state = states[j];
|
int has_hup = events[i].flags & EV_EOF;
|
||||||
if (!state) continue;
|
JanetFiber *rf = stream->read_fiber;
|
||||||
state->event = events + i;
|
JanetFiber *wf = stream->write_fiber;
|
||||||
JanetAsyncStatus statuses[4];
|
if (rf) {
|
||||||
for (int i = 0; i < 4; i++)
|
if (rf->ev_callback && has_err) {
|
||||||
statuses[i] = JANET_ASYNC_STATUS_NOT_DONE;
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_ERR);
|
||||||
if (!(events[i].flags & EV_ERROR)) {
|
} else if (rf->ev_callback && (filt == EVFILT_READ)) {
|
||||||
if (events[i].filter == EVFILT_WRITE)
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_READ);
|
||||||
statuses[0] = state->machine(state, JANET_ASYNC_EVENT_WRITE);
|
} else if (rf->ev_callback && has_hup) {
|
||||||
if (events[i].filter == EVFILT_READ)
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_HUP);
|
||||||
statuses[1] = state->machine(state, JANET_ASYNC_EVENT_READ);
|
|
||||||
if ((events[i].flags & EV_EOF) && !(events[i].data > 0))
|
|
||||||
statuses[3] = state->machine(state, JANET_ASYNC_EVENT_HUP);
|
|
||||||
} else {
|
|
||||||
statuses[2] = state->machine(state, JANET_ASYNC_EVENT_ERR);
|
|
||||||
}
|
}
|
||||||
if (statuses[0] == JANET_ASYNC_STATUS_DONE ||
|
}
|
||||||
statuses[1] == JANET_ASYNC_STATUS_DONE ||
|
if (wf) {
|
||||||
statuses[2] == JANET_ASYNC_STATUS_DONE ||
|
if (wf->ev_callback && has_err) {
|
||||||
statuses[3] == JANET_ASYNC_STATUS_DONE) {
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_ERR);
|
||||||
janet_unlisten(state);
|
} else if (wf->ev_callback && (filt == EVFILT_WRITE)) {
|
||||||
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_WRITE);
|
||||||
|
} else if (wf->ev_callback && has_hup) {
|
||||||
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_HUP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
janet_stream_checktoclose(stream);
|
janet_stream_checktoclose(stream);
|
||||||
@ -1778,6 +1767,10 @@ void janet_ev_deinit(void) {
|
|||||||
|
|
||||||
#elif defined(JANET_EV_POLL)
|
#elif defined(JANET_EV_POLL)
|
||||||
|
|
||||||
|
/* Simple poll implementation. Efficiency is not the goal here, although the poll implementation should be farily efficient
|
||||||
|
* for low numbers of concurrent file descriptors. Rather, the code should be simple, portable, correct, and mirror the
|
||||||
|
* epoll and kqueue code. */
|
||||||
|
|
||||||
static JanetTimestamp ts_now(void) {
|
static JanetTimestamp ts_now(void) {
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
janet_assert(-1 != clock_gettime(CLOCK_REALTIME, &now), "failed to get time");
|
janet_assert(-1 != clock_gettime(CLOCK_REALTIME, &now), "failed to get time");
|
||||||
|
Loading…
Reference in New Issue
Block a user