xref: /qemu/os-win32.c (revision 19113504)
1*19113504SJes Sorensen /*
2*19113504SJes Sorensen  * os-win32.c
3*19113504SJes Sorensen  *
4*19113504SJes Sorensen  * Copyright (c) 2003-2008 Fabrice Bellard
5*19113504SJes Sorensen  * Copyright (c) 2010 Red Hat, Inc.
6*19113504SJes Sorensen  *
7*19113504SJes Sorensen  * Permission is hereby granted, free of charge, to any person obtaining a copy
8*19113504SJes Sorensen  * of this software and associated documentation files (the "Software"), to deal
9*19113504SJes Sorensen  * in the Software without restriction, including without limitation the rights
10*19113504SJes Sorensen  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11*19113504SJes Sorensen  * copies of the Software, and to permit persons to whom the Software is
12*19113504SJes Sorensen  * furnished to do so, subject to the following conditions:
13*19113504SJes Sorensen  *
14*19113504SJes Sorensen  * The above copyright notice and this permission notice shall be included in
15*19113504SJes Sorensen  * all copies or substantial portions of the Software.
16*19113504SJes Sorensen  *
17*19113504SJes Sorensen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*19113504SJes Sorensen  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*19113504SJes Sorensen  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20*19113504SJes Sorensen  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*19113504SJes Sorensen  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*19113504SJes Sorensen  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23*19113504SJes Sorensen  * THE SOFTWARE.
24*19113504SJes Sorensen  */
25*19113504SJes Sorensen #include <windows.h>
26*19113504SJes Sorensen #include <unistd.h>
27*19113504SJes Sorensen #include <fcntl.h>
28*19113504SJes Sorensen #include <signal.h>
29*19113504SJes Sorensen #include <time.h>
30*19113504SJes Sorensen #include <errno.h>
31*19113504SJes Sorensen #include <sys/time.h>
32*19113504SJes Sorensen #include "config-host.h"
33*19113504SJes Sorensen #include "sysemu.h"
34*19113504SJes Sorensen 
35*19113504SJes Sorensen /***********************************************************/
36*19113504SJes Sorensen /* Polling handling */
37*19113504SJes Sorensen 
38*19113504SJes Sorensen typedef struct PollingEntry {
39*19113504SJes Sorensen     PollingFunc *func;
40*19113504SJes Sorensen     void *opaque;
41*19113504SJes Sorensen     struct PollingEntry *next;
42*19113504SJes Sorensen } PollingEntry;
43*19113504SJes Sorensen 
44*19113504SJes Sorensen static PollingEntry *first_polling_entry;
45*19113504SJes Sorensen 
46*19113504SJes Sorensen int qemu_add_polling_cb(PollingFunc *func, void *opaque)
47*19113504SJes Sorensen {
48*19113504SJes Sorensen     PollingEntry **ppe, *pe;
49*19113504SJes Sorensen     pe = qemu_mallocz(sizeof(PollingEntry));
50*19113504SJes Sorensen     pe->func = func;
51*19113504SJes Sorensen     pe->opaque = opaque;
52*19113504SJes Sorensen     for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
53*19113504SJes Sorensen     *ppe = pe;
54*19113504SJes Sorensen     return 0;
55*19113504SJes Sorensen }
56*19113504SJes Sorensen 
57*19113504SJes Sorensen void qemu_del_polling_cb(PollingFunc *func, void *opaque)
58*19113504SJes Sorensen {
59*19113504SJes Sorensen     PollingEntry **ppe, *pe;
60*19113504SJes Sorensen     for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
61*19113504SJes Sorensen         pe = *ppe;
62*19113504SJes Sorensen         if (pe->func == func && pe->opaque == opaque) {
63*19113504SJes Sorensen             *ppe = pe->next;
64*19113504SJes Sorensen             qemu_free(pe);
65*19113504SJes Sorensen             break;
66*19113504SJes Sorensen         }
67*19113504SJes Sorensen     }
68*19113504SJes Sorensen }
69*19113504SJes Sorensen 
70*19113504SJes Sorensen /***********************************************************/
71*19113504SJes Sorensen /* Wait objects support */
72*19113504SJes Sorensen typedef struct WaitObjects {
73*19113504SJes Sorensen     int num;
74*19113504SJes Sorensen     HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
75*19113504SJes Sorensen     WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
76*19113504SJes Sorensen     void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
77*19113504SJes Sorensen } WaitObjects;
78*19113504SJes Sorensen 
79*19113504SJes Sorensen static WaitObjects wait_objects = {0};
80*19113504SJes Sorensen 
81*19113504SJes Sorensen int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
82*19113504SJes Sorensen {
83*19113504SJes Sorensen     WaitObjects *w = &wait_objects;
84*19113504SJes Sorensen 
85*19113504SJes Sorensen     if (w->num >= MAXIMUM_WAIT_OBJECTS)
86*19113504SJes Sorensen         return -1;
87*19113504SJes Sorensen     w->events[w->num] = handle;
88*19113504SJes Sorensen     w->func[w->num] = func;
89*19113504SJes Sorensen     w->opaque[w->num] = opaque;
90*19113504SJes Sorensen     w->num++;
91*19113504SJes Sorensen     return 0;
92*19113504SJes Sorensen }
93*19113504SJes Sorensen 
94*19113504SJes Sorensen void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
95*19113504SJes Sorensen {
96*19113504SJes Sorensen     int i, found;
97*19113504SJes Sorensen     WaitObjects *w = &wait_objects;
98*19113504SJes Sorensen 
99*19113504SJes Sorensen     found = 0;
100*19113504SJes Sorensen     for (i = 0; i < w->num; i++) {
101*19113504SJes Sorensen         if (w->events[i] == handle)
102*19113504SJes Sorensen             found = 1;
103*19113504SJes Sorensen         if (found) {
104*19113504SJes Sorensen             w->events[i] = w->events[i + 1];
105*19113504SJes Sorensen             w->func[i] = w->func[i + 1];
106*19113504SJes Sorensen             w->opaque[i] = w->opaque[i + 1];
107*19113504SJes Sorensen         }
108*19113504SJes Sorensen     }
109*19113504SJes Sorensen     if (found)
110*19113504SJes Sorensen         w->num--;
111*19113504SJes Sorensen }
112