Debian Codegraph @

libmowgli-2/src/libmowgli/eventloop/poll_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_POLL_H
25
26 # include <poll.h>
27
28 # ifndef POLLRDNORM
29 # define POLLRDNORM POLLIN
30 # endif
31 # ifndef POLLWRNORM
32 # define POLLWRNORM POLLOUT
33 # endif
34
35 typedef struct
36 {
37 struct pollfd pollfds[FD_SETSIZE];
38
39 nfds_t nfds;
40 mowgli_list_t pollable_list;
41 } mowgli_poll_eventloop_private_t;
42
43 static nfds_t
44 update_poll_fds(mowgli_eventloop_t *eventloop)
45 {
46 mowgli_node_t *n, *tn;
47 mowgli_poll_eventloop_private_t *priv;
48 nfds_t slot = 0;
49
50 return_val_if_fail(eventloop != NULL, 0);
51
52 priv = eventloop->poller;
53
54 memset(priv->pollfds, '\0', sizeof(priv->pollfds));
55
56 MOWGLI_ITER_FOREACH_SAFE(n, tn, priv->pollable_list.head)
57 {
58 mowgli_eventloop_pollable_t *pollable = n->data;
59
60 # ifdef DEBUG
61 mowgli_log("considering fd %d pollable %p count %d", pollable->fd, pollable, priv->pollable_list.count);
62 # endif
63
64 if (pollable->read_function || pollable->write_function)
65 {
66 priv->pollfds[slot].fd = pollable->fd;
67
68 if (pollable->read_function)
69 priv->pollfds[slot].events |= POLLRDNORM;
70
71 if (pollable->write_function)
72 priv->pollfds[slot].events |= POLLWRNORM;
73
74 priv->pollfds[slot].revents = 0;
75 pollable->slot = slot;
76 slot++;
77 }
78 else
79 {
80 pollable->slot = -1;
81 }
82 }
83
84 return slot;
85 }
86
87 static void
88 mowgli_poll_eventloop_pollsetup(mowgli_eventloop_t *eventloop)
89 {
90 mowgli_poll_eventloop_private_t *priv;
91
92 priv = mowgli_alloc(sizeof *priv);
93 eventloop->poller = priv;
94
95 return;
96 }
97
98 static void
99 mowgli_poll_eventloop_pollshutdown(mowgli_eventloop_t *eventloop)
100 {
101 mowgli_node_t *n, *tn;
102 mowgli_poll_eventloop_private_t *priv;
103
104 return_if_fail(eventloop != NULL);
105
106 priv = eventloop->poller;
107
108 MOWGLI_ITER_FOREACH_SAFE(n, tn, priv->pollable_list.head)
109 {
110 mowgli_node_delete(n, &priv->pollable_list);
111 }
112
113 mowgli_free(priv);
114 return;
115 }
116
117 static void
118 mowgli_poll_eventloop_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable)
119 {
120 mowgli_poll_eventloop_private_t *priv;
121
122 return_if_fail(eventloop != NULL);
123 return_if_fail(pollable != NULL);
124
125 priv = eventloop->poller;
126
127 mowgli_node_delete(&pollable->node, &priv->pollable_list);
128 }
129
130 static void
131 mowgli_poll_eventloop_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function)
132 {
133 mowgli_poll_eventloop_private_t *priv;
134
135 return_if_fail(eventloop != NULL);
136 return_if_fail(pollable != NULL);
137
138 priv = eventloop->poller;
139
140 # ifdef DEBUG
141 mowgli_log("setselect %p fd %d func %p", pollable, pollable->fd, event_function);
142 # endif
143
144 if (pollable->read_function || pollable->write_function)
145 mowgli_node_delete(&pollable->node, &priv->pollable_list);
146
147 switch (dir)
148 {
149 case MOWGLI_EVENTLOOP_IO_READ:
150 pollable->read_function = event_function;
151 break;
152 case MOWGLI_EVENTLOOP_IO_WRITE:
153 pollable->write_function = event_function;
154 break;
155 default:
156 mowgli_log("unhandled pollable direction %d", dir);
157 break;
158 }
159
160 # ifdef DEBUG
161 mowgli_log("%p -> read %p : write %p", pollable, pollable->read_function, pollable->write_function);
162 # endif
163
164 if (pollable->read_function || pollable->write_function)
165 mowgli_node_add(pollable, &pollable->node, &priv->pollable_list);
166
167 return;
168 }
169
170 static void
171 mowgli_poll_eventloop_select(mowgli_eventloop_t *eventloop, int time)
172 {
173 mowgli_node_t *n, *tn;
174 nfds_t nfds;
175 mowgli_eventloop_pollable_t *pollable;
176 mowgli_poll_eventloop_private_t *priv;
177 int slot;
178
179 return_if_fail(eventloop != NULL);
180
181 priv = eventloop->poller;
182
183 nfds = update_poll_fds(eventloop);
184
185 if (poll(priv->pollfds, nfds, time) > 0)
186 {
187 mowgli_eventloop_synchronize(eventloop);
188
189 MOWGLI_ITER_FOREACH_SAFE(n, tn, priv->pollable_list.head)
190 {
191 pollable = n->data;
192 slot = pollable->slot;
193
194 if ((slot == -1) || (priv->pollfds[slot].revents == 0))
195 continue;
196
197 if (priv->pollfds[slot].revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR) && pollable->read_function && !pollable->removed)
198 {
199 # ifdef DEBUG
200 mowgli_log("run %p(%p, %p, MOWGLI_EVENTLOOP_IO_READ, %p)\n", pollable->read_function, eventloop, pollable, pollable->userdata);
201 # endif
202
203 priv->pollfds[slot].events &= ~(POLLRDNORM | POLLIN);
204 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_READ);
205 }
206
207 if (priv->pollfds[slot].revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR) && pollable->write_function && !pollable->removed)
208 {
209 # ifdef DEBUG
210 mowgli_log("run %p(%p, %p, MOWGLI_EVENTLOOP_IO_WRITE, %p)\n", pollable->write_function, eventloop, pollable, pollable->userdata);
211 # endif
212
213 priv->pollfds[slot].events &= ~(POLLWRNORM | POLLOUT);
214 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_WRITE);
215 }
216 }
217 }
218 }
219
220 mowgli_eventloop_ops_t _mowgli_poll_pollops =
221 {
222 .timeout_once = mowgli_simple_eventloop_timeout_once,
223 .run_once = mowgli_simple_eventloop_run_once,
224 .pollsetup = mowgli_poll_eventloop_pollsetup,
225 .pollshutdown = mowgli_poll_eventloop_pollshutdown,
226 .setselect = mowgli_poll_eventloop_setselect,
227 .select = mowgli_poll_eventloop_select,
228 .destroy = mowgli_poll_eventloop_destroy,
229 };
230
231 #endif