4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 /* XXX: fd_read_poll should be suppressed, but an API change is
28 necessary in the character devices to suppress fd_can_read(). */
29 int qemu_set_fd_handler3(void *ioh_record_list
,
31 IOCanReadHandler
*fd_read_poll
,
36 QLIST_HEAD(, IOHandlerRecord
) *io_handlers_ptr
= ioh_record_list
;
39 if (!fd_read
&& !fd_write
) {
40 QLIST_FOREACH(ioh
, io_handlers_ptr
, next
) {
47 QLIST_FOREACH(ioh
, io_handlers_ptr
, next
) {
51 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
52 QLIST_INSERT_HEAD(io_handlers_ptr
, ioh
, next
);
55 ioh
->fd_read_poll
= fd_read_poll
;
56 ioh
->fd_read
= fd_read
;
57 ioh
->fd_write
= fd_write
;
64 /* add entries from ioh record list to fd sets. nfds and fd sets
65 * should be cleared/reset by caller if desired. set a particular
66 * fdset to NULL to ignore fd events of that type
68 void qemu_get_fdset2(void *ioh_record_list
, int *nfds
, fd_set
*rfds
,
69 fd_set
*wfds
, fd_set
*xfds
)
71 QLIST_HEAD(, IOHandlerRecord
) *io_handlers
= ioh_record_list
;
74 QLIST_FOREACH(ioh
, io_handlers
, next
) {
77 if ((rfds
!= NULL
&& ioh
->fd_read
) &&
78 (!ioh
->fd_read_poll
||
79 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
80 FD_SET(ioh
->fd
, rfds
);
84 if (wfds
!= NULL
&& ioh
->fd_write
) {
85 FD_SET(ioh
->fd
, wfds
);
92 /* execute registered handlers for r/w events in the provided fdsets. unset
93 * handlers are cleaned up here as well
95 void qemu_process_fd_handlers2(void *ioh_record_list
, const fd_set
*rfds
,
96 const fd_set
*wfds
, const fd_set
*xfds
)
98 QLIST_HEAD(, IOHandlerRecord
) *io_handlers
= ioh_record_list
;
99 IOHandlerRecord
*ioh
, *pioh
;
101 QLIST_FOREACH_SAFE(ioh
, io_handlers
, next
, pioh
) {
102 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, rfds
)) {
103 ioh
->fd_read(ioh
->opaque
);
105 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, wfds
)) {
106 ioh
->fd_write(ioh
->opaque
);
109 /* Do this last in case read/write handlers marked it for deletion */
111 QLIST_REMOVE(ioh
, next
);