./configure: export xfs config via --{enable, disable}-xfsctl
[qemu/ar7.git] / main-loop.c
blob692381cb19d63aa6691c61a49e9ddc0641c5f178
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
25 #include "qemu-common.h"
26 #include "qemu-timer.h"
27 #include "slirp/slirp.h"
28 #include "main-loop.h"
30 #ifndef _WIN32
32 #include "compatfd.h"
34 static int io_thread_fd = -1;
36 void qemu_notify_event(void)
38 /* Write 8 bytes to be compatible with eventfd. */
39 static const uint64_t val = 1;
40 ssize_t ret;
42 if (io_thread_fd == -1) {
43 return;
45 do {
46 ret = write(io_thread_fd, &val, sizeof(val));
47 } while (ret < 0 && errno == EINTR);
49 /* EAGAIN is fine, a read must be pending. */
50 if (ret < 0 && errno != EAGAIN) {
51 fprintf(stderr, "qemu_notify_event: write() failed: %s\n",
52 strerror(errno));
53 exit(1);
57 static void qemu_event_read(void *opaque)
59 int fd = (intptr_t)opaque;
60 ssize_t len;
61 char buffer[512];
63 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
64 do {
65 len = read(fd, buffer, sizeof(buffer));
66 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
69 static int qemu_event_init(void)
71 int err;
72 int fds[2];
74 err = qemu_eventfd(fds);
75 if (err == -1) {
76 return -errno;
78 err = fcntl_setfl(fds[0], O_NONBLOCK);
79 if (err < 0) {
80 goto fail;
82 err = fcntl_setfl(fds[1], O_NONBLOCK);
83 if (err < 0) {
84 goto fail;
86 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
87 (void *)(intptr_t)fds[0]);
89 io_thread_fd = fds[1];
90 return 0;
92 fail:
93 close(fds[0]);
94 close(fds[1]);
95 return err;
98 /* If we have signalfd, we mask out the signals we want to handle and then
99 * use signalfd to listen for them. We rely on whatever the current signal
100 * handler is to dispatch the signals when we receive them.
102 static void sigfd_handler(void *opaque)
104 int fd = (intptr_t)opaque;
105 struct qemu_signalfd_siginfo info;
106 struct sigaction action;
107 ssize_t len;
109 while (1) {
110 do {
111 len = read(fd, &info, sizeof(info));
112 } while (len == -1 && errno == EINTR);
114 if (len == -1 && errno == EAGAIN) {
115 break;
118 if (len != sizeof(info)) {
119 printf("read from sigfd returned %zd: %m\n", len);
120 return;
123 sigaction(info.ssi_signo, NULL, &action);
124 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
125 action.sa_sigaction(info.ssi_signo,
126 (siginfo_t *)&info, NULL);
127 } else if (action.sa_handler) {
128 action.sa_handler(info.ssi_signo);
133 static int qemu_signal_init(void)
135 int sigfd;
136 sigset_t set;
139 * SIG_IPI must be blocked in the main thread and must not be caught
140 * by sigwait() in the signal thread. Otherwise, the cpu thread will
141 * not catch it reliably.
143 sigemptyset(&set);
144 sigaddset(&set, SIG_IPI);
145 sigaddset(&set, SIGIO);
146 sigaddset(&set, SIGALRM);
147 sigaddset(&set, SIGBUS);
148 pthread_sigmask(SIG_BLOCK, &set, NULL);
150 sigdelset(&set, SIG_IPI);
151 sigfd = qemu_signalfd(&set);
152 if (sigfd == -1) {
153 fprintf(stderr, "failed to create signalfd\n");
154 return -errno;
157 fcntl_setfl(sigfd, O_NONBLOCK);
159 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
160 (void *)(intptr_t)sigfd);
162 return 0;
165 #else /* _WIN32 */
167 HANDLE qemu_event_handle;
169 static void dummy_event_handler(void *opaque)
173 static int qemu_event_init(void)
175 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
176 if (!qemu_event_handle) {
177 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
178 return -1;
180 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
181 return 0;
184 void qemu_notify_event(void)
186 if (!SetEvent(qemu_event_handle)) {
187 fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n",
188 GetLastError());
189 exit(1);
193 static int qemu_signal_init(void)
195 return 0;
197 #endif
199 int qemu_init_main_loop(void)
201 int ret;
203 qemu_mutex_lock_iothread();
204 ret = qemu_signal_init();
205 if (ret) {
206 return ret;
209 /* Note eventfd must be drained before signalfd handlers run */
210 ret = qemu_event_init();
211 if (ret) {
212 return ret;
215 return 0;
219 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
220 static int n_poll_fds;
221 static int max_priority;
223 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
224 fd_set *xfds, struct timeval *tv)
226 GMainContext *context = g_main_context_default();
227 int i;
228 int timeout = 0, cur_timeout;
230 g_main_context_prepare(context, &max_priority);
232 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
233 poll_fds, ARRAY_SIZE(poll_fds));
234 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
236 for (i = 0; i < n_poll_fds; i++) {
237 GPollFD *p = &poll_fds[i];
239 if ((p->events & G_IO_IN)) {
240 FD_SET(p->fd, rfds);
241 *max_fd = MAX(*max_fd, p->fd);
243 if ((p->events & G_IO_OUT)) {
244 FD_SET(p->fd, wfds);
245 *max_fd = MAX(*max_fd, p->fd);
247 if ((p->events & G_IO_ERR)) {
248 FD_SET(p->fd, xfds);
249 *max_fd = MAX(*max_fd, p->fd);
253 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
254 if (timeout >= 0 && timeout < cur_timeout) {
255 tv->tv_sec = timeout / 1000;
256 tv->tv_usec = (timeout % 1000) * 1000;
260 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
261 bool err)
263 GMainContext *context = g_main_context_default();
265 if (!err) {
266 int i;
268 for (i = 0; i < n_poll_fds; i++) {
269 GPollFD *p = &poll_fds[i];
271 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
272 p->revents |= G_IO_IN;
274 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
275 p->revents |= G_IO_OUT;
277 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
278 p->revents |= G_IO_ERR;
283 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
284 g_main_context_dispatch(context);
288 #ifdef _WIN32
289 /***********************************************************/
290 /* Polling handling */
292 typedef struct PollingEntry {
293 PollingFunc *func;
294 void *opaque;
295 struct PollingEntry *next;
296 } PollingEntry;
298 static PollingEntry *first_polling_entry;
300 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
302 PollingEntry **ppe, *pe;
303 pe = g_malloc0(sizeof(PollingEntry));
304 pe->func = func;
305 pe->opaque = opaque;
306 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
307 *ppe = pe;
308 return 0;
311 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
313 PollingEntry **ppe, *pe;
314 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
315 pe = *ppe;
316 if (pe->func == func && pe->opaque == opaque) {
317 *ppe = pe->next;
318 g_free(pe);
319 break;
324 /***********************************************************/
325 /* Wait objects support */
326 typedef struct WaitObjects {
327 int num;
328 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
329 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
330 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
331 } WaitObjects;
333 static WaitObjects wait_objects = {0};
335 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
337 WaitObjects *w = &wait_objects;
338 if (w->num >= MAXIMUM_WAIT_OBJECTS) {
339 return -1;
341 w->events[w->num] = handle;
342 w->func[w->num] = func;
343 w->opaque[w->num] = opaque;
344 w->num++;
345 return 0;
348 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
350 int i, found;
351 WaitObjects *w = &wait_objects;
353 found = 0;
354 for (i = 0; i < w->num; i++) {
355 if (w->events[i] == handle) {
356 found = 1;
358 if (found) {
359 w->events[i] = w->events[i + 1];
360 w->func[i] = w->func[i + 1];
361 w->opaque[i] = w->opaque[i + 1];
364 if (found) {
365 w->num--;
369 static void os_host_main_loop_wait(int *timeout)
371 int ret, ret2, i;
372 PollingEntry *pe;
374 /* XXX: need to suppress polling by better using win32 events */
375 ret = 0;
376 for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
377 ret |= pe->func(pe->opaque);
379 if (ret == 0) {
380 int err;
381 WaitObjects *w = &wait_objects;
383 qemu_mutex_unlock_iothread();
384 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
385 qemu_mutex_lock_iothread();
386 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
387 if (w->func[ret - WAIT_OBJECT_0]) {
388 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
391 /* Check for additional signaled events */
392 for (i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
393 /* Check if event is signaled */
394 ret2 = WaitForSingleObject(w->events[i], 0);
395 if (ret2 == WAIT_OBJECT_0) {
396 if (w->func[i]) {
397 w->func[i](w->opaque[i]);
399 } else if (ret2 != WAIT_TIMEOUT) {
400 err = GetLastError();
401 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
404 } else if (ret != WAIT_TIMEOUT) {
405 err = GetLastError();
406 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
410 *timeout = 0;
412 #else
413 static inline void os_host_main_loop_wait(int *timeout)
416 #endif
418 int main_loop_wait(int nonblocking)
420 fd_set rfds, wfds, xfds;
421 int ret, nfds;
422 struct timeval tv;
423 int timeout;
425 if (nonblocking) {
426 timeout = 0;
427 } else {
428 timeout = qemu_calculate_timeout();
429 qemu_bh_update_timeout(&timeout);
432 os_host_main_loop_wait(&timeout);
434 tv.tv_sec = timeout / 1000;
435 tv.tv_usec = (timeout % 1000) * 1000;
437 /* poll any events */
438 /* XXX: separate device handlers from system ones */
439 nfds = -1;
440 FD_ZERO(&rfds);
441 FD_ZERO(&wfds);
442 FD_ZERO(&xfds);
444 #ifdef CONFIG_SLIRP
445 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
446 #endif
447 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
448 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
450 if (timeout > 0) {
451 qemu_mutex_unlock_iothread();
454 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
456 if (timeout > 0) {
457 qemu_mutex_lock_iothread();
460 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
461 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
462 #ifdef CONFIG_SLIRP
463 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
464 #endif
466 qemu_run_all_timers();
468 /* Check bottom-halves last in case any of the earlier events triggered
469 them. */
470 qemu_bh_poll();
472 return ret;