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 "io/channel-watch.h"
23 typedef struct QIOChannelFDSource QIOChannelFDSource
;
24 struct QIOChannelFDSource
{
28 GIOCondition condition
;
32 typedef struct QIOChannelFDPairSource QIOChannelFDPairSource
;
33 struct QIOChannelFDPairSource
{
38 GIOCondition condition
;
43 qio_channel_fd_source_prepare(GSource
*source G_GNUC_UNUSED
,
53 qio_channel_fd_source_check(GSource
*source
)
55 QIOChannelFDSource
*ssource
= (QIOChannelFDSource
*)source
;
57 return ssource
->fd
.revents
& ssource
->condition
;
62 qio_channel_fd_source_dispatch(GSource
*source
,
66 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
67 QIOChannelFDSource
*ssource
= (QIOChannelFDSource
*)source
;
69 return (*func
)(ssource
->ioc
,
70 ssource
->fd
.revents
& ssource
->condition
,
76 qio_channel_fd_source_finalize(GSource
*source
)
78 QIOChannelFDSource
*ssource
= (QIOChannelFDSource
*)source
;
80 object_unref(OBJECT(ssource
->ioc
));
85 qio_channel_fd_pair_source_prepare(GSource
*source G_GNUC_UNUSED
,
95 qio_channel_fd_pair_source_check(GSource
*source
)
97 QIOChannelFDPairSource
*ssource
= (QIOChannelFDPairSource
*)source
;
98 GIOCondition poll_condition
= ssource
->fdread
.revents
|
99 ssource
->fdwrite
.revents
;
101 return poll_condition
& ssource
->condition
;
106 qio_channel_fd_pair_source_dispatch(GSource
*source
,
107 GSourceFunc callback
,
110 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
111 QIOChannelFDPairSource
*ssource
= (QIOChannelFDPairSource
*)source
;
112 GIOCondition poll_condition
= ssource
->fdread
.revents
|
113 ssource
->fdwrite
.revents
;
115 return (*func
)(ssource
->ioc
,
116 poll_condition
& ssource
->condition
,
122 qio_channel_fd_pair_source_finalize(GSource
*source
)
124 QIOChannelFDPairSource
*ssource
= (QIOChannelFDPairSource
*)source
;
126 object_unref(OBJECT(ssource
->ioc
));
130 GSourceFuncs qio_channel_fd_source_funcs
= {
131 qio_channel_fd_source_prepare
,
132 qio_channel_fd_source_check
,
133 qio_channel_fd_source_dispatch
,
134 qio_channel_fd_source_finalize
138 GSourceFuncs qio_channel_fd_pair_source_funcs
= {
139 qio_channel_fd_pair_source_prepare
,
140 qio_channel_fd_pair_source_check
,
141 qio_channel_fd_pair_source_dispatch
,
142 qio_channel_fd_pair_source_finalize
146 GSource
*qio_channel_create_fd_watch(QIOChannel
*ioc
,
148 GIOCondition condition
)
151 QIOChannelFDSource
*ssource
;
153 source
= g_source_new(&qio_channel_fd_source_funcs
,
154 sizeof(QIOChannelFDSource
));
155 ssource
= (QIOChannelFDSource
*)source
;
158 object_ref(OBJECT(ioc
));
160 ssource
->condition
= condition
;
163 ssource
->fd
.events
= condition
;
165 g_source_add_poll(source
, &ssource
->fd
);
171 GSource
*qio_channel_create_fd_pair_watch(QIOChannel
*ioc
,
174 GIOCondition condition
)
177 QIOChannelFDPairSource
*ssource
;
179 source
= g_source_new(&qio_channel_fd_pair_source_funcs
,
180 sizeof(QIOChannelFDPairSource
));
181 ssource
= (QIOChannelFDPairSource
*)source
;
184 object_ref(OBJECT(ioc
));
186 ssource
->condition
= condition
;
188 ssource
->fdread
.fd
= fdread
;
189 ssource
->fdread
.events
= condition
& G_IO_IN
;
191 ssource
->fdwrite
.fd
= fdwrite
;
192 ssource
->fdwrite
.events
= condition
& G_IO_OUT
;
194 g_source_add_poll(source
, &ssource
->fdread
);
195 g_source_add_poll(source
, &ssource
->fdwrite
);