EVQ_API int evq_init (struct event_queue *evq) { evq->kqueue_fd = kqueue(); if (evq->kqueue_fd == -1) return -1; pthread_mutex_init(&evq->cs, NULL); { fd_t *sig_fd = evq->sig_fd; struct kevent kev; memset(&kev, 0, sizeof(struct kevent)); kev.filter = EVFILT_READ; kev.flags = EV_ADD; sig_fd[0] = sig_fd[1] = (fd_t) -1; if (pipe(sig_fd) || fcntl(sig_fd[0], F_SETFL, O_NONBLOCK)) goto err; kev.ident = sig_fd[0]; if (kevent(evq->kqueue_fd, &kev, 1, NULL, 0, NULL)) goto err; } evq->now = sys_milliseconds(); return 0; err: evq_done(evq); return -1; }
EVQ_API int evq_init (struct event_queue *evq) { pthread_mutex_init(&evq->sig_cs, NULL); { fd_t *sig_fd = evq->sig_fd; unsigned int fd; sig_fd[0] = sig_fd[1] = (fd_t) -1; if (pipe(sig_fd) || fcntl(sig_fd[0], F_SETFL, O_NONBLOCK)) goto err; fd = (unsigned int) sig_fd[0]; FD_SET(fd, &evq->readset); evq->max_fd = fd; evq->npolls++; } evq->now = sys_milliseconds(); return 0; err: evq_done(evq); return -1; }
EVQ_API int evq_init (struct event_queue *evq) { evq->events = malloc(NEVENT * sizeof(void *)); if (!evq->events) return -1; evq->fdset = malloc(NEVENT * sizeof(struct pollfd)); if (!evq->fdset) { free(evq->events); return -1; } pthread_mutex_init(&evq->cs, NULL); { fd_t *sig_fd = evq->sig_fd; struct pollfd *fdp; sig_fd[0] = sig_fd[1] = (fd_t) -1; if (pipe(sig_fd) || fcntl(sig_fd[0], F_SETFL, O_NONBLOCK)) goto err; fdp = &evq->fdset[0]; fdp->fd = sig_fd[0]; fdp->events = POLLIN; fdp->revents = 0; } evq->npolls++; evq->max_polls = NEVENT; evq->now = get_milliseconds(); return 0; err: evq_done(evq); return -1; }
/* * Arguments: evq_udata */ static int levq_done (lua_State *L) { struct event_queue *evq = checkudata(L, 1, EVQ_TYPENAME); struct event *buffers[EVQ_ENV_BUF_MAX + 1]; /* cache */ lua_State *NL = evq->L; memset(buffers, 0, sizeof(buffers)); #undef ARG_LAST #define ARG_LAST 1 /* delete object events */ lua_pushnil(NL); while (lua_next(NL, EVQ_CORO_UDATA)) { const int ev_id = lua_tointeger(NL, -2); const int buf_idx = getmaxbit( (ev_id | ((1 << EVQ_ENV_BUF_IDX) - 1)) + 1); const int nmax = (1 << buf_idx); struct event *ev = buffers[buf_idx]; if (!ev) { lua_rawgeti(NL, EVQ_CORO_ENV, buf_idx - EVQ_ENV_BUF_IDX + 1); ev = lua_touserdata(NL, -1); lua_pop(NL, 1); /* pop events buffer */ buffers[buf_idx] = ev; } ev += ev_id - ((nmax - 1) & ~((1 << EVQ_ENV_BUF_IDX) - 1)); if (!event_deleted(ev)) evq_del(ev, 0); lua_pop(NL, 1); /* pop value */ } evq_done(evq); return 0; }