Debian Codegraph @

libmowgli-2/src/libmowgli/eventloop/epoll_pollops.c

raw
1 /*
2 * Copyright (c) 2011 William Pitcock <[email protected]>.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "mowgli.h"
22 #include "eventloop/eventloop_internal.h"
23
24 #ifdef HAVE_SYS_EPOLL_H
25
26 # include <sys/epoll.h>
27
28 typedef struct
29 {
30 int epoll_fd;
31 int pfd_size;
32 struct epoll_event *pfd;
33 } mowgli_epoll_eventloop_private_t;
34
35 static void
36 mowgli_epoll_eventloop_pollsetup(mowgli_eventloop_t *eventloop)
37 {
38 mowgli_epoll_eventloop_private_t *priv;
39
40 priv = mowgli_alloc(sizeof *priv);
41 eventloop->poller = priv;
42
43 priv->pfd_size = getdtablesize();
44 priv->epoll_fd = epoll_create1(EPOLL_CLOEXEC); /* Linux 2.6.27+ */
45 priv->pfd = mowgli_alloc(sizeof(struct epoll_event) * priv->pfd_size);
46
47 return;
48 }
49
50 static void
51 mowgli_epoll_eventloop_pollshutdown(mowgli_eventloop_t *eventloop)
52 {
53 mowgli_epoll_eventloop_private_t *priv;
54
55 return_if_fail(eventloop != NULL);
56
57 priv = eventloop->poller;
58
59 close(priv->epoll_fd);
60
61 mowgli_free(priv->pfd);
62 mowgli_free(priv);
63 return;
64 }
65
66 static void
67 mowgli_epoll_eventloop_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable)
68 {
69 mowgli_epoll_eventloop_private_t *priv;
70 struct epoll_event ep_event;
71
72 return_if_fail(eventloop != NULL);
73 return_if_fail(pollable != NULL);
74
75 priv = eventloop->poller;
76 pollable->slot = 0;
77
78 (void) memset(&ep_event, 0x00, sizeof ep_event);
79 ep_event.events = pollable->slot;
80 ep_event.data.ptr = pollable;
81
82 if (epoll_ctl(priv->epoll_fd, EPOLL_CTL_DEL, pollable->fd, &ep_event) != 0)
83 {
84 if (mowgli_eventloop_ignore_errno(errno))
85 return;
86
87 mowgli_log("mowgli_epoll_eventloop_destroy(): epoll_ctl failed: %d (%s)", errno, strerror(errno));
88 }
89 }
90
91 static void
92 mowgli_epoll_eventloop_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function)
93 {
94 mowgli_epoll_eventloop_private_t *priv;
95 struct epoll_event ep_event;
96
97 int op = -1;
98 unsigned int old_flags;
99
100 return_if_fail(eventloop != NULL);
101 return_if_fail(pollable != NULL);
102
103 priv = eventloop->poller;
104 old_flags = pollable->slot;
105
106 # ifdef DEBUG
107 mowgli_log("setselect %p fd %d func %p", pollable, pollable->fd, event_function);
108 # endif
109
110 switch (dir)
111 {
112 case MOWGLI_EVENTLOOP_IO_READ:
113 pollable->read_function = event_function;
114 pollable->slot |= EPOLLIN;
115 break;
116 case MOWGLI_EVENTLOOP_IO_WRITE:
117 pollable->write_function = event_function;
118 pollable->slot |= EPOLLOUT;
119 break;
120 default:
121 mowgli_log("unhandled pollable direction %d", dir);
122 break;
123 }
124
125 # ifdef DEBUG
126 mowgli_log("%p -> read %p : write %p", pollable, pollable->read_function, pollable->write_function);
127 # endif
128
129 if (pollable->read_function == NULL)
130 pollable->slot &= ~EPOLLIN;
131
132 if (pollable->write_function == NULL)
133 pollable->slot &= ~EPOLLOUT;
134
135 if ((old_flags == 0) && (pollable->slot == 0))
136 return;
137 else if (pollable->slot <= 0)
138 op = EPOLL_CTL_DEL;
139 else if ((old_flags == 0) && (pollable->slot != 0))
140 op = EPOLL_CTL_ADD;
141 else if (pollable->slot != old_flags)
142 op = EPOLL_CTL_MOD;
143
144 if (op == -1)
145 return;
146
147 (void) memset(&ep_event, 0x00, sizeof ep_event);
148 ep_event.events = pollable->slot;
149 ep_event.data.ptr = pollable;
150
151 if (epoll_ctl(priv->epoll_fd, op, pollable->fd, &ep_event) != 0)
152 {
153 if (mowgli_eventloop_ignore_errno(errno))
154 return;
155
156 mowgli_log("mowgli_epoll_eventloop_setselect(): epoll_ctl failed: %d (%s)", errno, strerror(errno));
157 }
158
159 return;
160 }
161
162 static void
163 mowgli_epoll_eventloop_select(mowgli_eventloop_t *eventloop, int delay)
164 {
165 mowgli_epoll_eventloop_private_t *priv;
166 int i, num, o_errno;
167
168 return_if_fail(eventloop != NULL);
169
170 priv = eventloop->poller;
171
172 num = epoll_wait(priv->epoll_fd, priv->pfd, priv->pfd_size, delay);
173
174 o_errno = errno;
175 mowgli_eventloop_synchronize(eventloop);
176
177 if (num < 0)
178 {
179 if (mowgli_eventloop_ignore_errno(errno))
180 return;
181
182 mowgli_log("mowgli_epoll_eventloop_select(): epoll_wait failed: %d (%s)", o_errno, strerror(o_errno));
183 return;
184 }
185
186 for (i = 0; i < num; i++)
187 {
188 mowgli_eventloop_pollable_t *pollable = priv->pfd[i].data.ptr;
189
190 if (priv->pfd[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR) && !pollable->removed)
191 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_READ);
192
193 if (priv->pfd[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR) && !pollable->removed)
194 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_WRITE);
195 }
196 }
197
198 mowgli_eventloop_ops_t _mowgli_epoll_pollops =
199 {
200 .timeout_once = mowgli_simple_eventloop_timeout_once,
201 .run_once = mowgli_simple_eventloop_run_once,
202 .pollsetup = mowgli_epoll_eventloop_pollsetup,
203 .pollshutdown = mowgli_epoll_eventloop_pollshutdown,
204 .setselect = mowgli_epoll_eventloop_setselect,
205 .select = mowgli_epoll_eventloop_select,
206 .destroy = mowgli_epoll_eventloop_destroy,
207 };
208
209 #endif