4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 #include "config-host.h"
35 /***********************************************************/
36 /* Polling handling */
38 typedef struct PollingEntry
{
41 struct PollingEntry
*next
;
44 static PollingEntry
*first_polling_entry
;
46 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
48 PollingEntry
**ppe
, *pe
;
49 pe
= qemu_mallocz(sizeof(PollingEntry
));
52 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
57 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
59 PollingEntry
**ppe
, *pe
;
60 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
62 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
70 /***********************************************************/
71 /* Wait objects support */
72 typedef struct WaitObjects
{
74 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
75 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
76 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
79 static WaitObjects wait_objects
= {0};
81 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
83 WaitObjects
*w
= &wait_objects
;
85 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
87 w
->events
[w
->num
] = handle
;
88 w
->func
[w
->num
] = func
;
89 w
->opaque
[w
->num
] = opaque
;
94 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
97 WaitObjects
*w
= &wait_objects
;
100 for (i
= 0; i
< w
->num
; i
++) {
101 if (w
->events
[i
] == handle
)
104 w
->events
[i
] = w
->events
[i
+ 1];
105 w
->func
[i
] = w
->func
[i
+ 1];
106 w
->opaque
[i
] = w
->opaque
[i
+ 1];
113 void os_host_main_loop_wait(int *timeout
)
118 /* XXX: need to suppress polling by better using win32 events */
120 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
121 ret
|= pe
->func(pe
->opaque
);
125 WaitObjects
*w
= &wait_objects
;
127 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
128 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
129 if (w
->func
[ret
- WAIT_OBJECT_0
])
130 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
132 /* Check for additional signaled events */
133 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
135 /* Check if event is signaled */
136 ret2
= WaitForSingleObject(w
->events
[i
], 0);
137 if(ret2
== WAIT_OBJECT_0
) {
139 w
->func
[i
](w
->opaque
[i
]);
140 } else if (ret2
== WAIT_TIMEOUT
) {
142 err
= GetLastError();
143 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
146 } else if (ret
== WAIT_TIMEOUT
) {
148 err
= GetLastError();
149 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);