summaryrefslogtreecommitdiff
path: root/epoll.c
blob: a464cc7d6bc189bc1b668286b265b9bd761dfb5e (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "epoll.h"
#include <sys/poll.h>

typedef struct FdInfo FdInfo;

struct FdInfo
{
    /* FIXME: we might want to compress this struct at some point.
     * Though, if we have 100000 fd's, it's only 1.6 MB, which is
     * almost nothing for that many clients, Still, cache use
     * would improve.
     */
    gboolean		valid;
    gboolean		disabled;
    EPollEventType	mask;
    gpointer		data;
};

struct EPoll
{
    int		n_fds;
    gsize	n_fd_infos;
    FdInfo *	fd_info;
};

EPoll *
epoll_new (void)
{
    EPoll *epoll = g_new0 (EPoll, 1);
    
    epoll->n_fds = 0;
    epoll->n_fd_infos = 1;
    epoll->fd_info = g_new0 (FdInfo, 1);
    
    return epoll;
}

void
epoll_add_fd (EPoll   *epoll,
	      int      fd,
	      EPollEventType      mask,
	      gpointer data)
{
    g_return_if_fail (!epoll_has_fd (epoll, fd));

    while (fd >= epoll->n_fd_infos)
	epoll->n_fd_infos *= 2;
    
    epoll->fd_info = g_renew (FdInfo, epoll->fd_info, epoll->n_fd_infos);
    
    epoll->n_fds++;
    
    epoll->fd_info[fd].valid = TRUE;
    epoll->fd_info[fd].mask = mask;
    epoll->fd_info[fd].data = data;
}

gboolean
epoll_has_fd (EPoll *epoll,
	      int fd)
{
    g_return_val_if_fail (epoll != NULL, FALSE);
    
    return (fd < epoll->n_fd_infos && epoll->fd_info[fd].valid);
}

gint
epoll_get_n_fds (EPoll *epoll)
{
    return epoll->n_fds;
}

void
epoll_remove_fd (EPoll  *epoll,
		 int     fd)
{
    g_return_if_fail (epoll_has_fd (epoll, fd));
    
    epoll->fd_info[fd].valid = FALSE;
}

gpointer
epoll_get_fd_data (EPoll *epoll,
		   int    fd)
{
    g_return_val_if_fail (epoll != NULL, NULL);
    g_return_val_if_fail (epoll_has_fd (epoll, fd), NULL);

    return epoll->fd_info[fd].data;
}

/* epoll_wait() is a one-shot polling mechanism, so this function
 * must be called to reenable fd's after they have been returned
 * by epoll_wait().
 */
void
epoll_reenable_fd (EPoll *epoll,
		   int    fd)
{
    g_return_if_fail (epoll_has_fd (epoll, fd));
    
    epoll->fd_info[fd].disabled = FALSE;
}

EPollEvent *
epoll_wait (EPoll      *epoll,
	    int	       *n_events,
	    int         timeout)
{
    struct pollfd *fds;
    int i, j;
    int n_poll_fds;
    int n_ready;
    EPollEvent *events;

    fds = g_new (struct pollfd, epoll->n_fds);

    j = 0;
    for (i = 0; i < epoll->n_fd_infos; ++i)
    {
	if (epoll->fd_info[i].valid		&&
	    !epoll->fd_info[i].disabled		&&
	    epoll->fd_info[i].mask != 0)
	{
	    fds[j].events = 0;
    
	    if (epoll->fd_info[i].mask & EPOLL_READ)
		fds[j].events |= POLLIN;

	    if (epoll->fd_info[i].mask & EPOLL_WRITE)
		fds[j].events |= POLLOUT;
	    
	    if (epoll->fd_info[i].mask & EPOLL_HANGUP)
		fds[j].events |= POLLHUP;
	    
	    if (epoll->fd_info[i].mask & EPOLL_PRIORITY)
		fds[j].events |= POLLPRI;
	    
	    if (epoll->fd_info[i].mask & EPOLL_ERROR)
		fds[j].events |= POLLERR;
	    
	    fds[j].fd = i;

	    j++;
	}
    }

    n_poll_fds = j;

    events = NULL;
    
    n_ready = poll (fds, n_poll_fds, timeout);

    if (n_ready)
    {
	events = g_new0 (EPollEvent, n_ready);

	j = 0;
	for (i = 0; i < n_poll_fds; ++i)
	{
	    if (fds[i].revents)
	    {
		events[j].events = 0;
		
		if (fds[i].revents & POLLIN)
		    events[j].events |= EPOLL_READ;
		
		if (fds[i].revents & POLLOUT)
		    events[j].events |= EPOLL_WRITE;
		
		if (fds[i].revents & POLLHUP)
		    events[j].events |= EPOLL_HANGUP;
		
		if (fds[i].revents & POLLERR)
		    events[j].events |= EPOLL_ERROR;
		
		if (fds[i].revents & POLLPRI)
		    events[j].events |= EPOLL_PRIORITY;
		
		events[j].fd = fds[i].fd;
		epoll->fd_info[fds[i].fd].disabled = TRUE;
	    }
	}
    }

    g_free (fds);

    if (n_events)
	*n_events = n_ready;
    
    return events;
}