summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/lib/common/socket/evloop
diff options
context:
space:
mode:
Diffstat (limited to 'web/server/h2o/libh2o/lib/common/socket/evloop')
-rw-r--r--web/server/h2o/libh2o/lib/common/socket/evloop/epoll.c.h203
-rw-r--r--web/server/h2o/libh2o/lib/common/socket/evloop/kqueue.c.h186
-rw-r--r--web/server/h2o/libh2o/lib/common/socket/evloop/poll.c.h178
3 files changed, 567 insertions, 0 deletions
diff --git a/web/server/h2o/libh2o/lib/common/socket/evloop/epoll.c.h b/web/server/h2o/libh2o/lib/common/socket/evloop/epoll.c.h
new file mode 100644
index 00000000..247dac89
--- /dev/null
+++ b/web/server/h2o/libh2o/lib/common/socket/evloop/epoll.c.h
@@ -0,0 +1,203 @@
+/*
+ * Copyright (c) 2014 DeNA Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include <assert.h>
+#include <limits.h>
+#include <stdio.h>
+#include <sys/epoll.h>
+
+#if 0
+#define DEBUG_LOG(...) fprintf(stderr, __VA_ARGS__)
+#else
+#define DEBUG_LOG(...)
+#endif
+
+struct st_h2o_evloop_epoll_t {
+ h2o_evloop_t super;
+ int ep;
+};
+
+static int update_status(struct st_h2o_evloop_epoll_t *loop)
+{
+ while (loop->super._statechanged.head != NULL) {
+ /* detach the top */
+ struct st_h2o_evloop_socket_t *sock = loop->super._statechanged.head;
+ loop->super._statechanged.head = sock->_next_statechanged;
+ sock->_next_statechanged = sock;
+ /* update the state */
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_DISPOSED) != 0) {
+ free(sock);
+ } else {
+ int changed = 0, op, ret;
+ struct epoll_event ev;
+ ev.events = 0;
+ if (h2o_socket_is_reading(&sock->super)) {
+ ev.events |= EPOLLIN;
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_READ) == 0) {
+ sock->_flags |= H2O_SOCKET_FLAG_IS_POLLED_FOR_READ;
+ changed = 1;
+ }
+ } else {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_READ) != 0) {
+ sock->_flags &= ~H2O_SOCKET_FLAG_IS_POLLED_FOR_READ;
+ changed = 1;
+ }
+ }
+ if (h2o_socket_is_writing(&sock->super)) {
+ ev.events |= EPOLLOUT;
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE) == 0) {
+ sock->_flags |= H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE;
+ changed = 1;
+ }
+ } else {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE) != 0) {
+ sock->_flags &= ~H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE;
+ changed = 1;
+ }
+ }
+ if (changed) {
+ if ((sock->_flags & H2O_SOCKET_FLAG__EPOLL_IS_REGISTERED) != 0) {
+ if (ev.events != 0)
+ op = EPOLL_CTL_MOD;
+ else
+ op = EPOLL_CTL_DEL;
+ } else {
+ assert(ev.events != 0);
+ op = EPOLL_CTL_ADD;
+ }
+ ev.data.ptr = sock;
+ while ((ret = epoll_ctl(loop->ep, op, sock->fd, &ev)) != 0 && errno == EINTR)
+ ;
+ if (ret != 0)
+ return -1;
+ if (op == EPOLL_CTL_DEL)
+ sock->_flags &= ~H2O_SOCKET_FLAG__EPOLL_IS_REGISTERED;
+ else
+ sock->_flags |= H2O_SOCKET_FLAG__EPOLL_IS_REGISTERED;
+ }
+ }
+ }
+ loop->super._statechanged.tail_ref = &loop->super._statechanged.head;
+
+ return 0;
+}
+
+int evloop_do_proceed(h2o_evloop_t *_loop, int32_t max_wait)
+{
+ struct st_h2o_evloop_epoll_t *loop = (struct st_h2o_evloop_epoll_t *)_loop;
+ struct epoll_event events[256];
+ int nevents, i;
+
+ /* collect (and update) status */
+ if (update_status(loop) != 0)
+ return -1;
+
+ /* poll */
+ max_wait = adjust_max_wait(&loop->super, max_wait);
+ nevents = epoll_wait(loop->ep, events, sizeof(events) / sizeof(events[0]), max_wait);
+ update_now(&loop->super);
+ if (nevents == -1)
+ return -1;
+
+ if (nevents != 0)
+ h2o_sliding_counter_start(&loop->super.exec_time_counter, loop->super._now);
+
+ /* update readable flags, perform writes */
+ for (i = 0; i != nevents; ++i) {
+ struct st_h2o_evloop_socket_t *sock = events[i].data.ptr;
+ int notified = 0;
+ if ((events[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR)) != 0) {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_READ) != 0) {
+ sock->_flags |= H2O_SOCKET_FLAG_IS_READ_READY;
+ link_to_pending(sock);
+ notified = 1;
+ }
+ }
+ if ((events[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR)) != 0) {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE) != 0) {
+ write_pending(sock);
+ notified = 1;
+ }
+ }
+ if (!notified) {
+ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+ static time_t last_reported = 0;
+ time_t now = time(NULL);
+ pthread_mutex_lock(&lock);
+ if (last_reported + 60 < now) {
+ last_reported = now;
+ fprintf(stderr, "ignoring epoll event (fd:%d,event:%x)\n", sock->fd, (int)events[i].events);
+ }
+ pthread_mutex_unlock(&lock);
+ }
+ }
+
+ return 0;
+}
+
+static void evloop_do_on_socket_create(struct st_h2o_evloop_socket_t *sock)
+{
+}
+
+static void evloop_do_on_socket_close(struct st_h2o_evloop_socket_t *sock)
+{
+ struct st_h2o_evloop_epoll_t *loop = (void *)sock->loop;
+ int ret;
+
+ if (sock->fd == -1)
+ return;
+ if ((sock->_flags & H2O_SOCKET_FLAG__EPOLL_IS_REGISTERED) == 0)
+ return;
+ while ((ret = epoll_ctl(loop->ep, EPOLL_CTL_DEL, sock->fd, NULL)) != 0 && errno == EINTR)
+ ;
+ if (ret != 0)
+ fprintf(stderr, "socket_close: epoll(DEL) returned error %d (fd=%d)\n", errno, sock->fd);
+}
+
+static void evloop_do_on_socket_export(struct st_h2o_evloop_socket_t *sock)
+{
+ struct st_h2o_evloop_epoll_t *loop = (void *)sock->loop;
+ int ret;
+
+ if ((sock->_flags & H2O_SOCKET_FLAG__EPOLL_IS_REGISTERED) == 0)
+ return;
+ while ((ret = epoll_ctl(loop->ep, EPOLL_CTL_DEL, sock->fd, NULL)) != 0 && errno == EINTR)
+ ;
+ if (ret != 0)
+ fprintf(stderr, "socket_export: epoll(DEL) returned error %d (fd=%d)\n", errno, sock->fd);
+}
+
+h2o_evloop_t *h2o_evloop_create(void)
+{
+ struct st_h2o_evloop_epoll_t *loop = (struct st_h2o_evloop_epoll_t *)create_evloop(sizeof(*loop));
+
+ pthread_mutex_lock(&cloexec_mutex);
+ loop->ep = epoll_create(10);
+ while (fcntl(loop->ep, F_SETFD, FD_CLOEXEC) == -1) {
+ if (errno != EAGAIN) {
+ fprintf(stderr, "h2o_evloop_create: failed to set FD_CLOEXEC to the epoll fd (errno=%d)\n", errno);
+ abort();
+ }
+ }
+ pthread_mutex_unlock(&cloexec_mutex);
+
+ return &loop->super;
+}
diff --git a/web/server/h2o/libh2o/lib/common/socket/evloop/kqueue.c.h b/web/server/h2o/libh2o/lib/common/socket/evloop/kqueue.c.h
new file mode 100644
index 00000000..21288ed7
--- /dev/null
+++ b/web/server/h2o/libh2o/lib/common/socket/evloop/kqueue.c.h
@@ -0,0 +1,186 @@
+/*
+ * Copyright (c) 2014 DeNA Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include <assert.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+#if 0
+#define DEBUG_LOG(...) fprintf(stderr, __VA_ARGS__)
+#else
+#define DEBUG_LOG(...)
+#endif
+
+struct st_h2o_socket_loop_kqueue_t {
+ h2o_evloop_t super;
+ int kq;
+};
+
+static void ev_set(struct kevent *ev, int fd, int filter, int flags, struct st_h2o_evloop_socket_t *sock)
+{
+#ifdef __NetBSD__
+ EV_SET(ev, fd, filter, flags, 0, 0, (intptr_t)sock);
+#else
+ EV_SET(ev, fd, filter, flags, 0, 0, sock);
+#endif
+}
+
+static int collect_status(struct st_h2o_socket_loop_kqueue_t *loop, struct kevent *changelist, int changelist_capacity)
+{
+ int change_index = 0;
+
+#define SET_AND_UPDATE(filter, flags) \
+ do { \
+ ev_set(changelist + change_index++, sock->fd, filter, flags, sock); \
+ if (change_index == changelist_capacity) { \
+ int ret; \
+ while ((ret = kevent(loop->kq, changelist, change_index, NULL, 0, NULL)) != 0 && errno == EINTR) \
+ ; \
+ if (ret == -1) \
+ return -1; \
+ change_index = 0; \
+ } \
+ } while (0)
+
+ while (loop->super._statechanged.head != NULL) {
+ /* detach the top */
+ struct st_h2o_evloop_socket_t *sock = loop->super._statechanged.head;
+ loop->super._statechanged.head = sock->_next_statechanged;
+ sock->_next_statechanged = sock;
+ /* update the state */
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_DISPOSED) != 0) {
+ free(sock);
+ } else {
+ if (h2o_socket_is_reading(&sock->super)) {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_READ) == 0) {
+ sock->_flags |= H2O_SOCKET_FLAG_IS_POLLED_FOR_READ;
+ SET_AND_UPDATE(EVFILT_READ, EV_ADD);
+ }
+ } else {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_READ) != 0) {
+ sock->_flags &= ~H2O_SOCKET_FLAG_IS_POLLED_FOR_READ;
+ SET_AND_UPDATE(EVFILT_READ, EV_DELETE);
+ }
+ }
+ if (h2o_socket_is_writing(&sock->super)) {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE) == 0) {
+ sock->_flags |= H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE;
+ SET_AND_UPDATE(EVFILT_WRITE, EV_ADD);
+ }
+ } else {
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE) != 0) {
+ sock->_flags &= ~H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE;
+ SET_AND_UPDATE(EVFILT_WRITE, EV_DELETE);
+ }
+ }
+ }
+ }
+ loop->super._statechanged.tail_ref = &loop->super._statechanged.head;
+
+ return change_index;
+
+#undef SET_AND_UPDATE
+}
+
+int evloop_do_proceed(h2o_evloop_t *_loop, int32_t max_wait)
+{
+ struct st_h2o_socket_loop_kqueue_t *loop = (struct st_h2o_socket_loop_kqueue_t *)_loop;
+ struct kevent changelist[64], events[128];
+ int nchanges, nevents, i;
+ struct timespec ts;
+
+ /* collect (and update) status */
+ if ((nchanges = collect_status(loop, changelist, sizeof(changelist) / sizeof(changelist[0]))) == -1)
+ return -1;
+
+ /* poll */
+ max_wait = adjust_max_wait(&loop->super, max_wait);
+ ts.tv_sec = max_wait / 1000;
+ ts.tv_nsec = max_wait % 1000 * 1000 * 1000;
+ nevents = kevent(loop->kq, changelist, nchanges, events, sizeof(events) / sizeof(events[0]), &ts);
+
+ update_now(&loop->super);
+ if (nevents == -1)
+ return -1;
+
+ if (nevents != 0)
+ h2o_sliding_counter_start(&loop->super.exec_time_counter, loop->super._now);
+
+ /* update readable flags, perform writes */
+ for (i = 0; i != nevents; ++i) {
+ struct st_h2o_evloop_socket_t *sock = (void *)events[i].udata;
+ assert(sock->fd == events[i].ident);
+ switch (events[i].filter) {
+ case EVFILT_READ:
+ if (sock->_flags != H2O_SOCKET_FLAG_IS_DISPOSED) {
+ sock->_flags |= H2O_SOCKET_FLAG_IS_READ_READY;
+ link_to_pending(sock);
+ }
+ break;
+ case EVFILT_WRITE:
+ if (sock->_flags != H2O_SOCKET_FLAG_IS_DISPOSED) {
+ write_pending(sock);
+ }
+ break;
+ default:
+ break; /* ??? */
+ }
+ }
+
+ return 0;
+}
+
+static void evloop_do_on_socket_create(struct st_h2o_evloop_socket_t *sock)
+{
+}
+
+static void evloop_do_on_socket_close(struct st_h2o_evloop_socket_t *sock)
+{
+}
+
+static void evloop_do_on_socket_export(struct st_h2o_evloop_socket_t *sock)
+{
+ struct st_h2o_socket_loop_kqueue_t *loop = (void *)sock->loop;
+ struct kevent changelist[2];
+ int change_index = 0, ret;
+
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_READ) != 0)
+ ev_set(changelist + change_index++, sock->fd, EVFILT_READ, EV_DELETE, 0);
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE) != 0)
+ ev_set(changelist + change_index++, sock->fd, EVFILT_WRITE, EV_DELETE, 0);
+ if (change_index == 0)
+ return;
+ while ((ret = kevent(loop->kq, changelist, change_index, NULL, 0, NULL)) != 0 && errno == EINTR)
+ ;
+ if (ret == -1)
+ fprintf(stderr, "kevent returned error %d (fd=%d)", errno, sock->fd);
+}
+
+h2o_evloop_t *h2o_evloop_create(void)
+{
+ struct st_h2o_socket_loop_kqueue_t *loop = (struct st_h2o_socket_loop_kqueue_t *)create_evloop(sizeof(*loop));
+
+ loop->kq = kqueue();
+
+ return &loop->super;
+}
diff --git a/web/server/h2o/libh2o/lib/common/socket/evloop/poll.c.h b/web/server/h2o/libh2o/lib/common/socket/evloop/poll.c.h
new file mode 100644
index 00000000..8b3f3d14
--- /dev/null
+++ b/web/server/h2o/libh2o/lib/common/socket/evloop/poll.c.h
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2014,2015 DeNA Co., Ltd., Kazuho Oku
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+#include <stdio.h>
+#include <poll.h>
+
+#if 0
+#define DEBUG_LOG(...) fprintf(stderr, __VA_ARGS__)
+#else
+#define DEBUG_LOG(...)
+#endif
+
+struct st_h2o_evloop_poll_t {
+ h2o_evloop_t super;
+ H2O_VECTOR(struct st_h2o_evloop_socket_t *) socks;
+};
+
+static void update_socks(struct st_h2o_evloop_poll_t *loop)
+{
+ /* update loop->socks */
+ while (loop->super._statechanged.head != NULL) {
+ /* detach the top */
+ struct st_h2o_evloop_socket_t *sock = loop->super._statechanged.head;
+ loop->super._statechanged.head = sock->_next_statechanged;
+ sock->_next_statechanged = sock;
+ /* update the state */
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_DISPOSED) != 0) {
+ assert(sock->fd == -1);
+ free(sock);
+ } else {
+ assert(sock->fd < loop->socks.size);
+ if (loop->socks.entries[sock->fd] == NULL) {
+ loop->socks.entries[sock->fd] = sock;
+ } else {
+ assert(loop->socks.entries[sock->fd] == sock);
+ }
+ if (h2o_socket_is_reading(&sock->super)) {
+ DEBUG_LOG("setting READ for fd: %d\n", sock->fd);
+ sock->_flags |= H2O_SOCKET_FLAG_IS_POLLED_FOR_READ;
+ } else {
+ DEBUG_LOG("clearing READ for fd: %d\n", sock->fd);
+ sock->_flags &= ~H2O_SOCKET_FLAG_IS_POLLED_FOR_READ;
+ }
+ if (h2o_socket_is_writing(&sock->super)) {
+ DEBUG_LOG("setting WRITE for fd: %d\n", sock->fd);
+ sock->_flags |= H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE;
+ } else {
+ DEBUG_LOG("clearing WRITE for fd: %d\n", sock->fd);
+ sock->_flags &= ~H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE;
+ }
+ }
+ }
+ loop->super._statechanged.tail_ref = &loop->super._statechanged.head;
+}
+
+int evloop_do_proceed(h2o_evloop_t *_loop, int32_t max_wait)
+{
+ struct st_h2o_evloop_poll_t *loop = (struct st_h2o_evloop_poll_t *)_loop;
+ H2O_VECTOR(struct pollfd) pollfds = {NULL};
+ int fd, ret;
+
+ /* update status */
+ update_socks(loop);
+
+ /* build list of fds to be polled */
+ for (fd = 0; fd != loop->socks.size; ++fd) {
+ struct st_h2o_evloop_socket_t *sock = loop->socks.entries[fd];
+ if (sock == NULL)
+ continue;
+ assert(fd == sock->fd);
+ if ((sock->_flags & (H2O_SOCKET_FLAG_IS_POLLED_FOR_READ | H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE)) != 0) {
+ h2o_vector_reserve(NULL, &pollfds, pollfds.size + 1);
+ struct pollfd *slot = pollfds.entries + pollfds.size++;
+ slot->fd = fd;
+ slot->events = 0;
+ slot->revents = 0;
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_READ) != 0)
+ slot->events |= POLLIN;
+ if ((sock->_flags & H2O_SOCKET_FLAG_IS_POLLED_FOR_WRITE) != 0)
+ slot->events |= POLLOUT;
+ }
+ }
+
+ /* call */
+ max_wait = adjust_max_wait(&loop->super, max_wait);
+ ret = poll(pollfds.entries, (nfds_t)pollfds.size, max_wait);
+ update_now(&loop->super);
+ if (ret == -1)
+ goto Exit;
+ DEBUG_LOG("poll returned: %d\n", ret);
+
+ /* update readable flags, perform writes */
+ if (ret > 0) {
+ size_t i;
+ h2o_sliding_counter_start(&loop->super.exec_time_counter, loop->super._now);
+ for (i = 0; i != pollfds.size; ++i) {
+ /* set read_ready flag before calling the write cb, since app. code invoked by the latter may close the socket, clearing
+ * the former flag */
+ if ((pollfds.entries[i].revents & POLLIN) != 0) {
+ struct st_h2o_evloop_socket_t *sock = loop->socks.entries[pollfds.entries[i].fd];
+ assert(sock != NULL);
+ assert(sock->fd == pollfds.entries[i].fd);
+ if (sock->_flags != H2O_SOCKET_FLAG_IS_DISPOSED) {
+ sock->_flags |= H2O_SOCKET_FLAG_IS_READ_READY;
+ link_to_pending(sock);
+ DEBUG_LOG("added fd %d as read_ready\n", sock->fd);
+ }
+ }
+ if ((pollfds.entries[i].revents & POLLOUT) != 0) {
+ struct st_h2o_evloop_socket_t *sock = loop->socks.entries[pollfds.entries[i].fd];
+ assert(sock != NULL);
+ assert(sock->fd == pollfds.entries[i].fd);
+ if (sock->_flags != H2O_SOCKET_FLAG_IS_DISPOSED) {
+ DEBUG_LOG("handling pending writes on fd %d\n", fd);
+ write_pending(sock);
+ }
+ }
+ }
+ ret = 0;
+ }
+
+Exit:
+ free(pollfds.entries);
+ return ret;
+}
+
+static void evloop_do_on_socket_create(struct st_h2o_evloop_socket_t *sock)
+{
+ struct st_h2o_evloop_poll_t *loop = (struct st_h2o_evloop_poll_t *)sock->loop;
+
+ if (sock->fd >= loop->socks.size) {
+ h2o_vector_reserve(NULL, &loop->socks, sock->fd + 1);
+ memset(loop->socks.entries + loop->socks.size, 0, (sock->fd + 1 - loop->socks.size) * sizeof(loop->socks.entries[0]));
+ loop->socks.size = sock->fd + 1;
+ }
+
+ if (loop->socks.entries[sock->fd] != NULL)
+ assert(loop->socks.entries[sock->fd]->_flags == H2O_SOCKET_FLAG_IS_DISPOSED);
+}
+
+static void evloop_do_on_socket_close(struct st_h2o_evloop_socket_t *sock)
+{
+ struct st_h2o_evloop_poll_t *loop = (struct st_h2o_evloop_poll_t *)sock->loop;
+
+ if (sock->fd != -1)
+ loop->socks.entries[sock->fd] = NULL;
+}
+
+static void evloop_do_on_socket_export(struct st_h2o_evloop_socket_t *sock)
+{
+ struct st_h2o_evloop_poll_t *loop = (struct st_h2o_evloop_poll_t *)sock->loop;
+ evloop_do_on_socket_close(sock);
+ loop->socks.entries[sock->fd] = NULL;
+}
+
+h2o_evloop_t *h2o_evloop_create(void)
+{
+ struct st_h2o_evloop_poll_t *loop = (struct st_h2o_evloop_poll_t *)create_evloop(sizeof(*loop));
+ return &loop->super;
+}