Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) 2012 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_DISPATCH_BLOCK
25
26 # include <sys/iofunc.h>
27 # include <sys/dispatch.h>
28
29 typedef struct
30 {
31 dispatch_t *dpp;
32 dispatch_context_t *ctp;
33 } mowgli_qnx_eventloop_private_t;
34
35 static void
36 mowgli_qnx_eventloop_pollsetup(mowgli_eventloop_t *eventloop)
37 {
38 mowgli_qnx_eventloop_private_t *priv;
39
40 priv = mowgli_alloc(sizeof *priv);
41 eventloop->poller = priv;
42
43 priv->dpp = dispatch_create();
44
45 return;
46 }
47
48 static void
49 mowgli_qnx_eventloop_pollshutdown(mowgli_eventloop_t *eventloop)
50 {
51 mowgli_qnx_eventloop_private_t *priv;
52
53 return_if_fail(eventloop != NULL);
54
55 priv = eventloop->poller;
56
57 if (priv->ctp != NULL)
58 dispatch_context_free(priv->ctp);
59
60 dispatch_destroy(priv->dpp);
61 mowgli_free(priv);
62
63 return;
64 }
65
66 static void
67 mowgli_qnx_eventloop_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable)
68 {
69 mowgli_qnx_eventloop_private_t *priv;
70
71 return_if_fail(eventloop != NULL);
72 return_if_fail(pollable != NULL);
73
74 priv = eventloop->poller;
75
76 if (select_detach(priv->dpp, pollable->fd) != 0)
77 {
78 if (mowgli_eventloop_ignore_errno(errno))
79 return;
80
81 mowgli_log("mowgli_qnx_eventloop_destroy(): select_detach failed: %d (%s)", errno, strerror(errno));
82 }
83 }
84
85 static void
86 mowgli_qnx_eventloop_event_cb(select_context_t *ctp, mowgli_descriptor_t fd, unsigned int flags, void *userdata)
87 {
88 mowgli_eventloop_t *eventloop;
89 mowgli_eventloop_pollable_t *pollable;
90
91 return_if_fail(ctp != NULL);
92 return_if_fail(userdata != NULL);
93
94 pollable = userdata;
95 eventloop = pollable->eventloop;
96
97 return_if_fail(eventloop != NULL);
98
99 if (flags & (SELECT_FLAG_READ | SELECT_FLAG_EXCEPT) && !pollable->removed)
100 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_READ);
101
102 if (flags & (SELECT_FLAG_WRITE | SELECT_FLAG_EXCEPT) && !pollable->removed)
103 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_WRITE);
104 }
105
106 static void
107 mowgli_qnx_eventloop_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function)
108 {
109 mowgli_qnx_eventloop_private_t *priv;
110 select_attr_t attr = { };
111 unsigned int old_flags;
112
113 return_if_fail(eventloop != NULL);
114 return_if_fail(pollable != NULL);
115
116 priv = eventloop->poller;
117 old_flags = pollable->slot;
118
119 # ifdef DEBUG
120 mowgli_log("setselect %p fd %d func %p", pollable, pollable->fd, event_function);
121 # endif
122
123 switch (dir)
124 {
125 case MOWGLI_EVENTLOOP_IO_READ:
126 pollable->read_function = event_function;
127 pollable->slot |= SELECT_FLAG_READ;
128 break;
129 case MOWGLI_EVENTLOOP_IO_WRITE:
130 pollable->write_function = event_function;
131 pollable->slot |= SELECT_FLAG_WRITE;
132 break;
133 default:
134 mowgli_log("unhandled pollable direction %d", dir);
135 break;
136 }
137
138 # ifdef DEBUG
139 mowgli_log("%p -> read %p : write %p", pollable, pollable->read_function, pollable->write_function);
140 # endif
141
142 if (pollable->read_function == NULL)
143 pollable->slot &= ~SELECT_FLAG_READ;
144
145 if (pollable->write_function == NULL)
146 pollable->slot &= ~SELECT_FLAG_WRITE;
147
148 if ((old_flags == 0) && (pollable->slot == 0))
149 return;
150
151 if (old_flags)
152 select_detach(priv->dpp, pollable->fd);
153
154 if (pollable->slot)
155 if (select_attach(priv->dpp, &attr, pollable->fd, pollable->slot, mowgli_qnx_eventloop_event_cb, pollable) != 0)
156 {
157 if (mowgli_eventloop_ignore_errno(errno))
158 return;
159
160 mowgli_log("mowgli_qnx_eventloop_setselect(): select_attach failed: %d (%s)", errno, strerror(errno));
161 }
162
163 return;
164 }
165
166 static void
167 mowgli_qnx_eventloop_select(mowgli_eventloop_t *eventloop, int delay)
168 {
169 dispatch_context_t *new_ctp;
170 mowgli_qnx_eventloop_private_t *priv;
171
172 return_if_fail(eventloop != NULL);
173
174 priv = eventloop->poller;
175
176 /* set timeout if needed */
177 dispatch_timeout(priv->dpp, delay >= 0 ? &(struct timespec) { .tv_sec = delay / 1000, .tv_nsec = delay % 1000 * 1000000 } : NULL);
178
179 if (priv->ctp != NULL)
180 priv->ctp = dispatch_context_alloc(priv->dpp);
181
182 /* if dispatch_block returns non-NULL, priv->ctp may have been realloc()'d, NULL is error condition */
183 if ((new_ctp = dispatch_block(priv->ctp)) != NULL)
184 {
185 priv->ctp = new_ctp;
186 }
187 else
188 {
189 if (mowgli_eventloop_ignore_errno(errno))
190 return;
191
192 mowgli_log("mowgli_qnx_eventloop_select(): dispatch_block failed: %d (%s)", errno, strerror(errno));
193 }
194
195 mowgli_eventloop_synchronize(eventloop);
196 }
197
198 mowgli_eventloop_ops_t _mowgli_qnx_pollops =
199 {
200 .timeout_once = mowgli_simple_eventloop_timeout_once,
201 .run_once = mowgli_simple_eventloop_run_once,
202 .pollsetup = mowgli_qnx_eventloop_pollsetup,
203 .pollshutdown = mowgli_qnx_eventloop_pollshutdown,
204 .setselect = mowgli_qnx_eventloop_setselect,
205 .select = mowgli_qnx_eventloop_select,
206 .destroy = mowgli_qnx_eventloop_destroy,
207 };
208
209 #endif