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
|
/* 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 <sys/socket.h>
#include <libtf/io.h>
#include <libtf/fiber.h>
static int tf_fd_created(struct tf_fd *fd)
{
struct tf_poll_data *pd = &tf_get_scheduler()->poll_data;
struct epoll_event ev;
int r;
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.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_fd_destroyed(struct tf_fd *fd)
{
struct tf_poll_data *pd = &tf_get_scheduler()->poll_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_fd_monitor(struct tf_fd *fd, int events)
{
struct tf_poll_data *pd = &tf_get_scheduler()->poll_data;
TF_BUG_ON(fd->waiting_fiber != NULL);
fd->events = events | EPOLLERR | EPOLLHUP;
fd->waiting_fiber = tf_get_fiber();
pd->num_waiters++;
}
static void tf_fd_unmonitor(struct tf_fd *fd)
{
struct tf_poll_data *pd = &tf_get_scheduler()->poll_data;
fd->waiting_fiber = NULL;
fd->events = 0;
pd->num_waiters--;
}
void tf_poll_init(void)
{
struct tf_poll_data *pd = &tf_get_scheduler()->poll_data;
pd->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
pd->num_waiters = 0;
TF_BUG_ON(pd->epoll_fd < 0);
}
int tf_poll(tf_mtime_diff_t timeout)
{
struct tf_poll_data *pd = &tf_get_scheduler()->poll_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_wakeup(fd->waiting_fiber, TF_WAKEUP_FD);
}
ret = TF_WAKEUP_FD;
timeout = 0;
} while (unlikely(r == array_size(events)));
return ret;
}
void tf_poll_close(void)
{
struct tf_poll_data *pd = &tf_get_scheduler()->poll_data;
close(pd->epoll_fd);
}
#include "io-unix.c"
|