2 * QEMU I/O channels watch helper APIs
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io/channel-watch.h"
24 typedef struct QIOChannelFDSource QIOChannelFDSource;
25 struct QIOChannelFDSource {
29 GIOCondition condition;
33 typedef struct QIOChannelFDPairSource QIOChannelFDPairSource;
34 struct QIOChannelFDPairSource {
39 GIOCondition condition;
44 qio_channel_fd_source_prepare(GSource *source G_GNUC_UNUSED,
54 qio_channel_fd_source_check(GSource *source)
56 QIOChannelFDSource *ssource = (QIOChannelFDSource *)source;
58 return ssource->fd.revents & ssource->condition;
63 qio_channel_fd_source_dispatch(GSource *source,
67 QIOChannelFunc func = (QIOChannelFunc)callback;
68 QIOChannelFDSource *ssource = (QIOChannelFDSource *)source;
70 return (*func)(ssource->ioc,
71 ssource->fd.revents & ssource->condition,
77 qio_channel_fd_source_finalize(GSource *source)
79 QIOChannelFDSource *ssource = (QIOChannelFDSource *)source;
81 object_unref(OBJECT(ssource->ioc));
86 qio_channel_fd_pair_source_prepare(GSource *source G_GNUC_UNUSED,
96 qio_channel_fd_pair_source_check(GSource *source)
98 QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
99 GIOCondition poll_condition = ssource->fdread.revents |
100 ssource->fdwrite.revents;
102 return poll_condition & ssource->condition;
107 qio_channel_fd_pair_source_dispatch(GSource *source,
108 GSourceFunc callback,
111 QIOChannelFunc func = (QIOChannelFunc)callback;
112 QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
113 GIOCondition poll_condition = ssource->fdread.revents |
114 ssource->fdwrite.revents;
116 return (*func)(ssource->ioc,
117 poll_condition & ssource->condition,
123 qio_channel_fd_pair_source_finalize(GSource *source)
125 QIOChannelFDPairSource *ssource = (QIOChannelFDPairSource *)source;
127 object_unref(OBJECT(ssource->ioc));
131 GSourceFuncs qio_channel_fd_source_funcs = {
132 qio_channel_fd_source_prepare,
133 qio_channel_fd_source_check,
134 qio_channel_fd_source_dispatch,
135 qio_channel_fd_source_finalize
139 GSourceFuncs qio_channel_fd_pair_source_funcs = {
140 qio_channel_fd_pair_source_prepare,
141 qio_channel_fd_pair_source_check,
142 qio_channel_fd_pair_source_dispatch,
143 qio_channel_fd_pair_source_finalize
147 GSource *qio_channel_create_fd_watch(QIOChannel *ioc,
149 GIOCondition condition)
152 QIOChannelFDSource *ssource;
154 source = g_source_new(&qio_channel_fd_source_funcs,
155 sizeof(QIOChannelFDSource));
156 ssource = (QIOChannelFDSource *)source;
159 object_ref(OBJECT(ioc));
161 ssource->condition = condition;
164 ssource->fd.fd = (gint64)_get_osfhandle(fd);
168 ssource->fd.events = condition;
170 g_source_add_poll(source, &ssource->fd);
176 GSource *qio_channel_create_fd_pair_watch(QIOChannel *ioc,
179 GIOCondition condition)
182 QIOChannelFDPairSource *ssource;
184 source = g_source_new(&qio_channel_fd_pair_source_funcs,
185 sizeof(QIOChannelFDPairSource));
186 ssource = (QIOChannelFDPairSource *)source;
189 object_ref(OBJECT(ioc));
191 ssource->condition = condition;
194 ssource->fdread.fd = (gint64)_get_osfhandle(fdread);
195 ssource->fdwrite.fd = (gint64)_get_osfhandle(fdwrite);
197 ssource->fdread.fd = fdread;
198 ssource->fdwrite.fd = fdwrite;
201 ssource->fdread.events = condition & G_IO_IN;
202 ssource->fdwrite.events = condition & G_IO_OUT;
204 g_source_add_poll(source, &ssource->fdread);
205 g_source_add_poll(source, &ssource->fdwrite);