Revision 44a9b356

b/async.c
24 24

  
25 25
#include "qemu-common.h"
26 26
#include "qemu-aio.h"
27
#include "main-loop.h"
27 28

  
28 29
/* Anchor of the list of Bottom Halves belonging to the context */
29 30
static struct QEMUBH *first_bh;
b/cpus.c
33 33

  
34 34
#include "qemu-thread.h"
35 35
#include "cpus.h"
36
#include "main-loop.h"
36 37

  
37 38
#ifndef _WIN32
38 39
#include "compatfd.h"
39 40
#endif
40 41

  
41
#ifdef SIGRTMIN
42
#define SIG_IPI (SIGRTMIN+4)
43
#else
44
#define SIG_IPI SIGUSR1
45
#endif
46

  
47 42
#ifdef CONFIG_LINUX
48 43

  
49 44
#include <sys/prctl.h>
b/cpus.h
2 2
#define QEMU_CPUS_H
3 3

  
4 4
/* cpus.c */
5
int qemu_init_main_loop(void);
6 5
void qemu_main_loop_start(void);
7 6
void resume_all_vcpus(void);
8 7
void pause_all_vcpus(void);
b/iohandler.c
26 26
#include "qemu-common.h"
27 27
#include "qemu-char.h"
28 28
#include "qemu-queue.h"
29
#include "main-loop.h"
29 30

  
30 31
#ifndef _WIN32
31 32
#include <sys/wait.h>
b/main-loop.h
1
/*
2
 * QEMU System Emulator
3
 *
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 *
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:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
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.
23
 */
24

  
25
#ifndef QEMU_MAIN_LOOP_H
26
#define QEMU_MAIN_LOOP_H 1
27

  
28
#ifdef SIGRTMIN
29
#define SIG_IPI (SIGRTMIN+4)
30
#else
31
#define SIG_IPI SIGUSR1
32
#endif
33

  
34
/**
35
 * qemu_init_main_loop: Set up the process so that it can run the main loop.
36
 *
37
 * This includes setting up signal handlers.  It should be called before
38
 * any other threads are created.  In addition, threads other than the
39
 * main one should block signals that are trapped by the main loop.
40
 * For simplicity, you can consider these signals to be safe: SIGUSR1,
41
 * SIGUSR2, thread signals (SIGFPE, SIGILL, SIGSEGV, SIGBUS) and real-time
42
 * signals if available.  Remember that Windows in practice does not have
43
 * signals, though.
44
 */
45
int qemu_init_main_loop(void);
46

  
47
/**
48
 * main_loop_wait: Run one iteration of the main loop.
49
 *
50
 * If @nonblocking is true, poll for events, otherwise suspend until
51
 * one actually occurs.  The main loop usually consists of a loop that
52
 * repeatedly calls main_loop_wait(false).
53
 *
54
 * Main loop services include file descriptor callbacks, bottom halves
55
 * and timers (defined in qemu-timer.h).  Bottom halves are similar to timers
56
 * that execute immediately, but have a lower overhead and scheduling them
57
 * is wait-free, thread-safe and signal-safe.
58
 *
59
 * It is sometimes useful to put a whole program in a coroutine.  In this
60
 * case, the coroutine actually should be started from within the main loop,
61
 * so that the main loop can run whenever the coroutine yields.  To do this,
62
 * you can use a bottom half to enter the coroutine as soon as the main loop
63
 * starts:
64
 *
65
 *     void enter_co_bh(void *opaque) {
66
 *         QEMUCoroutine *co = opaque;
67
 *         qemu_coroutine_enter(co, NULL);
68
 *     }
69
 *
70
 *     ...
71
 *     QEMUCoroutine *co = qemu_coroutine_create(coroutine_entry);
72
 *     QEMUBH *start_bh = qemu_bh_new(enter_co_bh, co);
73
 *     qemu_bh_schedule(start_bh);
74
 *     while (...) {
75
 *         main_loop_wait(false);
76
 *     }
77
 *
78
 * (In the future we may provide a wrapper for this).
79
 *
80
 * @nonblocking: Whether the caller should block until an event occurs.
81
 */
82
int main_loop_wait(int nonblocking);
83

  
84
/**
85
 * qemu_notify_event: Force processing of pending events.
86
 *
87
 * Similar to signaling a condition variable, qemu_notify_event forces
88
 * main_loop_wait to look at pending events and exit.  The caller of
89
 * main_loop_wait will usually call it again very soon, so qemu_notify_event
90
 * also has the side effect of recalculating the sets of file descriptors
91
 * that the main loop waits for.
92
 *
93
 * Calling qemu_notify_event is rarely necessary, because main loop
94
 * services (bottom halves and timers) call it themselves.  One notable
95
 * exception occurs when using qemu_set_fd_handler2 (see below).
96
 */
97
void qemu_notify_event(void);
98

  
99
#ifdef _WIN32
100
/* return TRUE if no sleep should be done afterwards */
101
typedef int PollingFunc(void *opaque);
102

  
103
/**
104
 * qemu_add_polling_cb: Register a Windows-specific polling callback
105
 *
106
 * Currently, under Windows some events are polled rather than waited for.
107
 * Polling callbacks do not ensure that @func is called timely, because
108
 * the main loop might wait for an arbitrarily long time.  If possible,
109
 * you should instead create a separate thread that does a blocking poll
110
 * and set a Win32 event object.  The event can then be passed to
111
 * qemu_add_wait_object.
112
 *
113
 * Polling callbacks really have nothing Windows specific in them, but
114
 * as they are a hack and are currenly not necessary under POSIX systems,
115
 * they are only available when QEMU is running under Windows.
116
 *
117
 * @func: The function that does the polling, and returns 1 to force
118
 * immediate completion of main_loop_wait.
119
 * @opaque: A pointer-size value that is passed to @func.
120
 */
121
int qemu_add_polling_cb(PollingFunc *func, void *opaque);
122

  
123
/**
124
 * qemu_del_polling_cb: Unregister a Windows-specific polling callback
125
 *
126
 * This function removes a callback that was registered with
127
 * qemu_add_polling_cb.
128
 *
129
 * @func: The function that was passed to qemu_add_polling_cb.
130
 * @opaque: A pointer-size value that was passed to qemu_add_polling_cb.
131
 */
132
void qemu_del_polling_cb(PollingFunc *func, void *opaque);
133

  
134
/* Wait objects handling */
135
typedef void WaitObjectFunc(void *opaque);
136

  
137
/**
138
 * qemu_add_wait_object: Register a callback for a Windows handle
139
 *
140
 * Under Windows, the iohandler mechanism can only be used with sockets.
141
 * QEMU must use the WaitForMultipleObjects API to wait on other handles.
142
 * This function registers a #HANDLE with QEMU, so that it will be included
143
 * in the main loop's calls to WaitForMultipleObjects.  When the handle
144
 * is in a signaled state, QEMU will call @func.
145
 *
146
 * @handle: The Windows handle to be observed.
147
 * @func: A function to be called when @handle is in a signaled state.
148
 * @opaque: A pointer-size value that is passed to @func.
149
 */
150
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
151

  
152
/**
153
 * qemu_del_wait_object: Unregister a callback for a Windows handle
154
 *
155
 * This function removes a callback that was registered with
156
 * qemu_add_wait_object.
157
 *
158
 * @func: The function that was passed to qemu_add_wait_object.
159
 * @opaque: A pointer-size value that was passed to qemu_add_wait_object.
160
 */
161
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
162
#endif
163

  
164
/* async I/O support */
165

  
166
typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
167
typedef int IOCanReadHandler(void *opaque);
168
typedef void IOHandler(void *opaque);
169

  
170
/**
171
 * qemu_set_fd_handler2: Register a file descriptor with the main loop
172
 *
173
 * This function tells the main loop to wake up whenever one of the
174
 * following conditions is true:
175
 *
176
 * 1) if @fd_write is not %NULL, when the file descriptor is writable;
177
 *
178
 * 2) if @fd_read is not %NULL, when the file descriptor is readable.
179
 *
180
 * @fd_read_poll can be used to disable the @fd_read callback temporarily.
181
 * This is useful to avoid calling qemu_set_fd_handler2 every time the
182
 * client becomes interested in reading (or dually, stops being interested).
183
 * A typical example is when @fd is a listening socket and you want to bound
184
 * the number of active clients.  Remember to call qemu_notify_event whenever
185
 * the condition may change from %false to %true.
186
 *
187
 * The callbacks that are set up by qemu_set_fd_handler2 are level-triggered.
188
 * If @fd_read does not read from @fd, or @fd_write does not write to @fd
189
 * until its buffers are full, they will be called again on the next
190
 * iteration.
191
 *
192
 * @fd: The file descriptor to be observed.  Under Windows it must be
193
 * a #SOCKET.
194
 *
195
 * @fd_read_poll: A function that returns 1 if the @fd_read callback
196
 * should be fired.  If the function returns 0, the main loop will not
197
 * end its iteration even if @fd becomes readable.
198
 *
199
 * @fd_read: A level-triggered callback that is fired if @fd is readable
200
 * at the beginning of a main loop iteration, or if it becomes readable
201
 * during one.
202
 *
203
 * @fd_write: A level-triggered callback that is fired when @fd is writable
204
 * at the beginning of a main loop iteration, or if it becomes writable
205
 * during one.
206
 *
207
 * @opaque: A pointer-sized value that is passed to @fd_read_poll,
208
 * @fd_read and @fd_write.
209
 */
210
int qemu_set_fd_handler2(int fd,
211
                         IOCanReadHandler *fd_read_poll,
212
                         IOHandler *fd_read,
213
                         IOHandler *fd_write,
214
                         void *opaque);
215

  
216
/**
217
 * qemu_set_fd_handler: Register a file descriptor with the main loop
218
 *
219
 * This function tells the main loop to wake up whenever one of the
220
 * following conditions is true:
221
 *
222
 * 1) if @fd_write is not %NULL, when the file descriptor is writable;
223
 *
224
 * 2) if @fd_read is not %NULL, when the file descriptor is readable.
225
 *
226
 * The callbacks that are set up by qemu_set_fd_handler are level-triggered.
227
 * If @fd_read does not read from @fd, or @fd_write does not write to @fd
228
 * until its buffers are full, they will be called again on the next
229
 * iteration.
230
 *
231
 * @fd: The file descriptor to be observed.  Under Windows it must be
232
 * a #SOCKET.
233
 *
234
 * @fd_read: A level-triggered callback that is fired if @fd is readable
235
 * at the beginning of a main loop iteration, or if it becomes readable
236
 * during one.
237
 *
238
 * @fd_write: A level-triggered callback that is fired when @fd is writable
239
 * at the beginning of a main loop iteration, or if it becomes writable
240
 * during one.
241
 *
242
 * @opaque: A pointer-sized value that is passed to @fd_read and @fd_write.
243
 */
244
int qemu_set_fd_handler(int fd,
245
                        IOHandler *fd_read,
246
                        IOHandler *fd_write,
247
                        void *opaque);
248

  
249
typedef struct QEMUBH QEMUBH;
250
typedef void QEMUBHFunc(void *opaque);
251

  
252
/**
253
 * qemu_bh_new: Allocate a new bottom half structure.
254
 *
255
 * Bottom halves are lightweight callbacks whose invocation is guaranteed
256
 * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
257
 * is opaque and must be allocated prior to its use.
258
 */
259
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
260

  
261
/**
262
 * qemu_bh_schedule: Schedule a bottom half.
263
 *
264
 * Scheduling a bottom half interrupts the main loop and causes the
265
 * execution of the callback that was passed to qemu_bh_new.
266
 *
267
 * Bottom halves that are scheduled from a bottom half handler are instantly
268
 * invoked.  This can create an infinite loop if a bottom half handler
269
 * schedules itself.
270
 *
271
 * @bh: The bottom half to be scheduled.
272
 */
273
void qemu_bh_schedule(QEMUBH *bh);
274

  
275
/**
276
 * qemu_bh_cancel: Cancel execution of a bottom half.
277
 *
278
 * Canceling execution of a bottom half undoes the effect of calls to
279
 * qemu_bh_schedule without freeing its resources yet.  While cancellation
280
 * itself is also wait-free and thread-safe, it can of course race with the
281
 * loop that executes bottom halves unless you are holding the iothread
282
 * mutex.  This makes it mostly useless if you are not holding the mutex.
283
 *
284
 * @bh: The bottom half to be canceled.
285
 */
286
void qemu_bh_cancel(QEMUBH *bh);
287

  
288
/**
289
 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
290
 *
291
 * Deleting a bottom half frees the memory that was allocated for it by
292
 * qemu_bh_new.  It also implies canceling the bottom half if it was
293
 * scheduled.
294
 *
295
 * @bh: The bottom half to be deleted.
296
 */
297
void qemu_bh_delete(QEMUBH *bh);
298

  
299
#ifdef CONFIG_POSIX
300
/**
301
 * qemu_add_child_watch: Register a child process for reaping.
302
 *
303
 * Under POSIX systems, a parent process must read the exit status of
304
 * its child processes using waitpid, or the operating system will not
305
 * free some of the resources attached to that process.
306
 *
307
 * This function directs the QEMU main loop to observe a child process
308
 * and call waitpid as soon as it exits; the watch is then removed
309
 * automatically.  It is useful whenever QEMU forks a child process
310
 * but will find out about its termination by other means such as a
311
 * "broken pipe".
312
 *
313
 * @pid: The pid that QEMU should observe.
314
 */
315
int qemu_add_child_watch(pid_t pid);
316
#endif
317

  
318
/* internal interfaces */
319

  
320
void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
321
void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
322

  
323
void qemu_bh_schedule_idle(QEMUBH *bh);
324
int qemu_bh_poll(void);
325
void qemu_bh_update_timeout(int *timeout);
326

  
327
#endif
b/qemu-char.h
7 7
#include "qemu-config.h"
8 8
#include "qobject.h"
9 9
#include "qstring.h"
10
#include "main-loop.h"
10 11

  
11 12
/* character device */
12 13

  
......
237 238
QString *qemu_chr_mem_to_qs(CharDriverState *chr);
238 239
size_t qemu_chr_mem_osize(const CharDriverState *chr);
239 240

  
240
/* async I/O support */
241

  
242
int qemu_set_fd_handler2(int fd,
243
                         IOCanReadHandler *fd_read_poll,
244
                         IOHandler *fd_read,
245
                         IOHandler *fd_write,
246
                         void *opaque);
247
int qemu_set_fd_handler(int fd,
248
                        IOHandler *fd_read,
249
                        IOHandler *fd_write,
250
                        void *opaque);
251 241
#endif
b/qemu-common.h
13 13

  
14 14
typedef struct QEMUTimer QEMUTimer;
15 15
typedef struct QEMUFile QEMUFile;
16
typedef struct QEMUBH QEMUBH;
17 16
typedef struct DeviceState DeviceState;
18 17

  
19 18
struct Monitor;
......
117 116
int qemu_main(int argc, char **argv, char **envp);
118 117
#endif
119 118

  
120
/* bottom halves */
121
typedef void QEMUBHFunc(void *opaque);
122

  
123
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
124
void qemu_bh_schedule(QEMUBH *bh);
125
/* Bottom halfs that are scheduled from a bottom half handler are instantly
126
 * invoked.  This can create an infinite loop if a bottom half handler
127
 * schedules itself.  qemu_bh_schedule_idle() avoids this infinite loop by
128
 * ensuring that the bottom half isn't executed until the next main loop
129
 * iteration.
130
 */
131
void qemu_bh_schedule_idle(QEMUBH *bh);
132
void qemu_bh_cancel(QEMUBH *bh);
133
void qemu_bh_delete(QEMUBH *bh);
134
int qemu_bh_poll(void);
135
void qemu_bh_update_timeout(int *timeout);
136

  
137 119
void qemu_get_timedate(struct tm *tm, int offset);
138 120
int qemu_timedate_diff(struct tm *tm);
139 121

  
......
196 178
void qemu_set_cloexec(int fd);
197 179

  
198 180
#ifndef _WIN32
199
int qemu_add_child_watch(pid_t pid);
200 181
int qemu_eventfd(int pipefd[2]);
201 182
int qemu_pipe(int pipefd[2]);
202 183
#endif
......
211 192

  
212 193
void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
213 194

  
214
/* IO callbacks.  */
215
typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
216
typedef int IOCanReadHandler(void *opaque);
217
typedef void IOHandler(void *opaque);
218

  
219
void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
220
void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
221

  
222 195
struct ParallelIOArg {
223 196
    void *buffer;
224 197
    int count;
......
280 253
void cpu_save(QEMUFile *f, void *opaque);
281 254
int cpu_load(QEMUFile *f, void *opaque, int version_id);
282 255

  
283
/* Force QEMU to process pending events */
284
void qemu_notify_event(void);
285

  
286 256
/* Unblock cpu */
287 257
void qemu_cpu_kick(void *env);
288 258
void qemu_cpu_kick_self(void);
b/qemu-coroutine-lock.c
26 26
#include "qemu-coroutine.h"
27 27
#include "qemu-coroutine-int.h"
28 28
#include "qemu-queue.h"
29
#include "main-loop.h"
29 30
#include "trace.h"
30 31

  
31 32
static QTAILQ_HEAD(, Coroutine) unlock_bh_queue =
b/qemu-os-win32.h
28 28

  
29 29
#include <windows.h>
30 30
#include <winsock2.h>
31
#include "main-loop.h"
31 32

  
32 33
/* Declaration of ffs() is missing in MinGW's strings.h. */
33 34
int ffs(int i);
34 35

  
35
/* Polling handling */
36

  
37
/* return TRUE if no sleep should be done afterwards */
38
typedef int PollingFunc(void *opaque);
39

  
40
int qemu_add_polling_cb(PollingFunc *func, void *opaque);
41
void qemu_del_polling_cb(PollingFunc *func, void *opaque);
42

  
43
/* Wait objects handling */
44
typedef void WaitObjectFunc(void *opaque);
45

  
46
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
47
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
48

  
49 36
void os_host_main_loop_wait(int *timeout);
50 37

  
51 38
static inline void os_setup_signal_handling(void) {}
b/qemu-timer.h
2 2
#define QEMU_TIMER_H
3 3

  
4 4
#include "qemu-common.h"
5
#include "main-loop.h"
5 6
#include "notify.h"
6 7
#include <time.h>
7 8
#include <sys/time.h>
b/sysemu.h
8 8
#include "qemu-timer.h"
9 9
#include "qapi-types.h"
10 10
#include "notify.h"
11
#include "main-loop.h"
11 12

  
12 13
/* vl.c */
13 14

  
......
64 65

  
65 66
void qemu_announce_self(void);
66 67

  
67
int main_loop_wait(int nonblocking);
68

  
69 68
bool qemu_savevm_state_blocked(Monitor *mon);
70 69
int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
71 70
                            int shared);
b/vl.c
148 148
#include "qemu-objects.h"
149 149
#include "qemu-options.h"
150 150
#include "qmp-commands.h"
151
#include "main-loop.h"
151 152
#ifdef CONFIG_VIRTFS
152 153
#include "fsdev/qemu-fsdev.h"
153 154
#endif

Also available in: Unified diff