summaryrefslogtreecommitdiffstats
path: root/src/io-epoll.c
blob: 32aa09081f75fd8f13d9805ef2146cdbee567e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* io-epoll.c - epoll(7) based file descriptor monitoring
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 */

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/epoll.h>

#include <libtf/io.h>
#include <libtf/scheduler.h>

struct tf_poll_data {
	int			epoll_fd;
	int			num_waiters;
};

static struct tf_poll_data *tf_epoll_get_data(void)
{
	struct tf_scheduler *sched = tf_scheduler_get_current();
	TF_BUILD_BUG_ON(sizeof(struct tf_poll_data) > sizeof(sched->poll_data));
	return (struct tf_poll_data *) &sched->poll_data;
}

static void tf_epoll_init(void)
{
	struct tf_poll_data *pd = tf_epoll_get_data();

	pd->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
	pd->num_waiters = 0;
	TF_BUG_ON(pd->epoll_fd < 0);
}

static void tf_epoll_close(void)
{
	struct tf_poll_data *pd = tf_epoll_get_data();

	close(pd->epoll_fd);
}
static int tf_epoll_poll(tf_mtime_diff_t timeout)
{
	struct tf_poll_data *pd = tf_epoll_get_data();
	struct epoll_event events[64];
	struct tf_fd *fd;
	int r, i, ret;

	if (timeout == 0 && pd->num_waiters == 0)
		return TF_WAKEUP_TIMEOUT;

	ret = TF_WAKEUP_TIMEOUT;
	do {
		r = epoll_wait(pd->epoll_fd, events, array_size(events), timeout);
		if (r == 0)
			break;

		for (i = 0; i < r; i++) {
			fd = (struct tf_fd *) events[i].data.ptr;
			if (likely(fd->events & events[i].events))
				__tf_fiber_wakeup(fd->waiting_fiber, TF_WAKEUP_FD);
		}
		ret = TF_WAKEUP_FD;
		timeout = 0;
	} while (unlikely(r == array_size(events)));

	return ret;
}

static int tf_epoll_fd_created(struct tf_fd *fd)
{
	struct tf_poll_data *pd = tf_epoll_get_data();
	struct epoll_event ev;
	int r;

	ev = (struct epoll_event) {
		.events = EPOLLIN | EPOLLOUT | EPOLLET,
		.data.ptr = fd,
	};
	r = epoll_ctl(pd->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev);
	if (unlikely(r < 0)) {
		TF_BUG_ON(errno == EEXIST);
		r = -errno;
		return r;
	}

	return 0;
}

static int tf_epoll_fd_destroyed(struct tf_fd *fd)
{
	struct tf_poll_data *pd = tf_epoll_get_data();

	if (fd->flags & TF_FD_AUTOCLOSE)
		return 0;

	epoll_ctl(pd->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
	return 0;
}

static void tf_epoll_fd_monitor(struct tf_fd *fd, int events)
{
	struct tf_poll_data *pd = tf_epoll_get_data();

	TF_BUG_ON(fd->waiting_fiber != NULL);
	fd->events = EPOLLERR | EPOLLHUP;
	if (events & TF_POLL_READ)
		fd->events |= EPOLLIN;
	if (events & TF_POLL_WRITE)
		fd->events |= EPOLLOUT;
	fd->waiting_fiber = tf_scheduler_get_current()->active_fiber;
	pd->num_waiters++;
}

static void tf_epoll_fd_unmonitor(struct tf_fd *fd)
{
	struct tf_poll_data *pd = tf_epoll_get_data();

	fd->waiting_fiber = NULL;
	fd->events = 0;
	pd->num_waiters--;
}

struct tf_poll_hooks tf_epoll_hooks = {
	.init = tf_epoll_init,
	.close = tf_epoll_close,
	.poll = tf_epoll_poll,
	.fd_created = tf_epoll_fd_created,
	.fd_destroyed = tf_epoll_fd_destroyed,
	.fd_monitor = tf_epoll_fd_monitor,
	.fd_unmonitor = tf_epoll_fd_unmonitor,
};