1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/* Copyright (C) 2010 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
/* Implement the Looper interface on top of the QEMU main event loop */
#include <android/looper.h>
#include <android/utils/panic.h>
#include "qemu-common.h"
#include "qemu-timer.h"
#include "qemu-char.h"
#include "sockets.h" /* for socket_set_nonblock() */
/**********************************************************************
**********************************************************************
*****
***** T I M E R S
*****
**********************************************************************
**********************************************************************/
/* Model a timer simple as a QEMUTimer for the host_clock */
static void
qlooptimer_startRelative(void* impl, Duration timeout_ms)
{
QEMUTimer* tt = impl;
if (timeout_ms == DURATION_INFINITE)
qemu_del_timer(tt);
else
qemu_mod_timer(tt, qemu_get_clock_ns(host_clock) + timeout_ms*1000000);
}
static void
qlooptimer_startAbsolute(void* impl, Duration deadline_ms)
{
QEMUTimer* tt = impl;
if (deadline_ms == DURATION_INFINITE)
qemu_del_timer(tt);
else
qemu_mod_timer(tt, deadline_ms*1000000);
}
static void
qlooptimer_stop(void* impl)
{
QEMUTimer* tt = impl;
qemu_del_timer(tt);
}
static int
qlooptimer_isActive(void* impl)
{
QEMUTimer* tt = impl;
return qemu_timer_pending(tt);
}
static void
qlooptimer_free(void* impl)
{
QEMUTimer* tt = impl;
qemu_free_timer(tt);
}
static const LoopTimerClass qlooptimer_class = {
qlooptimer_startRelative,
qlooptimer_startAbsolute,
qlooptimer_stop,
qlooptimer_isActive,
qlooptimer_free
};
static void
qlooper_timer_init(Looper* looper,
LoopTimer* timer,
LoopTimerFunc callback,
void* opaque)
{
timer->clazz = (LoopTimerClass*) &qlooptimer_class;
timer->impl = qemu_new_timer(host_clock, callback, opaque);
}
/**********************************************************************
**********************************************************************
*****
***** F I L E D E S C R I P T O R S
*****
**********************************************************************
**********************************************************************/
/* Modeling the LoopIo is a bit more complex because the main event loop
* will call different functions for read and write readiness, while our
* users expect a single call with a mask of ready events.
*
* Since the QEMU main event loop looks like the following:
*
* 1/ perform select()
* 2/ for each file descriptor:
* if readReady:
* call readHandler()
* if writeReady:
* call writeHandler()
* 3/ run timers
* 4/ run bottom-half handlers
*
* We're going to provide simple read and write handlers that only mark
* the file descriptor for readiness, and put it on a "pending list".
*
* Then, we're going to schedule a bottom-half handler when such a pending
* i/o event occurs, in order to call the user callback with the correct
* flags.
*/
typedef struct QLoopIo QLoopIo;
typedef struct QLooper QLooper;
struct QLoopIo {
int fd;
LoopIoFunc user_callback;
void* user_opaque;
unsigned wanted;
unsigned ready;
QLooper* looper;
QLoopIo* pendingNext;
QLoopIo* next;
};
static void qlooper_addIo(QLooper* looper, QLoopIo* io);
static void qlooper_delIo(QLooper* looper, QLoopIo* io);
static QLoopIo*
qloopio_new(int fd, LoopIoFunc callback, void* opaque, QLooper* qlooper)
{
QLoopIo* io = qemu_malloc(sizeof(*io));
io->fd = fd;
io->user_callback = callback;
io->user_opaque = opaque;
io->wanted = 0;
io->ready = 0;
io->looper = qlooper;
io->pendingNext = NULL;
qlooper_addIo(qlooper, io);
return io;
}
static void qlooper_addPendingIo(QLooper* qlooper, QLoopIo* io);
static void qlooper_delPendingIo(QLooper* qlooper, QLoopIo* io);
static void
qloopio_removePending(QLoopIo* io)
{
if (io->ready != 0) {
qlooper_delPendingIo(io->looper, io);
io->ready = 0;
}
}
static void
qloopio_setReady(QLoopIo* io, unsigned flag)
{
if (io->ready == 0) {
qlooper_addPendingIo(io->looper, io);
}
io->ready |= flag;
}
static void
qloopio_handleRead(void* opaque)
{
QLoopIo* io = opaque;
qloopio_setReady(io, LOOP_IO_READ);
}
static void
qloopio_handleWrite(void* opaque)
{
QLoopIo* io = opaque;
qloopio_setReady(io, LOOP_IO_WRITE);
}
static void
qloopio_modify(QLoopIo* io, unsigned wanted)
{
/* no change, don't bother */
if (wanted == io->wanted)
return;
/* if we're pending, but the new mask doesn't care about
* out state, remove from pending list */
if (io->ready && (io->ready & wanted) == 0) {
qloopio_removePending(io);
}
/* recompute read/write handlers for QEMU */
IOHandler* fd_read = (wanted & LOOP_IO_READ) ? qloopio_handleRead : NULL;
IOHandler* fd_write = (wanted & LOOP_IO_WRITE) ? qloopio_handleWrite : NULL;
qemu_set_fd_handler(io->fd, fd_read, fd_write, io);
io->wanted = wanted;
}
static void
qloopio_wantRead(void* impl)
{
QLoopIo* io = impl;
qloopio_modify(io, io->wanted | LOOP_IO_READ);
}
static void
qloopio_wantWrite(void* impl)
{
QLoopIo* io = impl;
qloopio_modify(io, io->wanted | LOOP_IO_WRITE);
}
static void
qloopio_dontWantRead(void* impl)
{
QLoopIo* io = impl;
qloopio_modify(io, io->wanted & ~LOOP_IO_READ);
}
static void
qloopio_dontWantWrite(void* impl)
{
QLoopIo* io = impl;
qloopio_modify(io, io->wanted & ~LOOP_IO_WRITE);
}
static void
qloopio_free(void* impl)
{
QLoopIo* io = impl;
if (io->ready)
qloopio_removePending(io);
/* remove from global list */
qlooper_delIo(io->looper, io);
/* make QEMU forget about this fd */
qemu_set_fd_handler(io->fd, NULL, NULL, NULL);
io->fd = -1;
qemu_free(io);
}
static const LoopIoClass qlooper_io_class = {
qloopio_wantRead,
qloopio_wantWrite,
qloopio_dontWantRead,
qloopio_dontWantWrite,
qloopio_free
};
static void
qlooper_io_init(Looper* looper,
LoopIo* loopio,
int fd,
LoopIoFunc callback,
void* opaque)
{
QLoopIo* io = qloopio_new(fd, callback, opaque, (QLooper*)looper);
socket_set_nonblock(fd);
loopio->clazz = (LoopIoClass*) &qlooper_io_class;
loopio->impl = io;
}
struct QLooper {
Looper looper;
QLoopIo* io_list;
QLoopIo* io_pending;
QEMUBH* io_bh;
};
static void
qlooper_addIo(QLooper* looper, QLoopIo* io)
{
io->next = looper->io_list;
looper->io_list = io;
}
static void
qlooper_delIo(QLooper* looper, QLoopIo* io)
{
QLoopIo** pnode = &looper->io_list;
for (;;) {
if (*pnode == NULL)
break;
if (*pnode == io) {
*pnode = io->next;
io->next = NULL;
break;
}
pnode = &(*pnode)->next;
}
}
static void
qlooper_addPendingIo(QLooper* looper, QLoopIo* io)
{
if (looper->io_pending == NULL) {
qemu_bh_schedule(looper->io_bh);
}
io->pendingNext = looper->io_pending;
looper->io_pending = io;
}
static void
qlooper_delPendingIo(QLooper* looper, QLoopIo* io)
{
QLoopIo** pnode = &looper->io_pending;
for (;;) {
if (*pnode == NULL)
break;
if (*pnode == io) {
*pnode = io->pendingNext;
break;
}
pnode = &(*pnode)->pendingNext;
}
io->pendingNext = NULL;
}
/* This function is called by the main event loop when pending i/o
* events were registered with qlooper_addPendingIo(). It will parse
* the list of pending QLoopIo and call the user callback with the
* appropriate flags.
*/
static void
qlooper_handle_io_bh(void* opaque)
{
QLooper* looper = opaque;
QLoopIo* io;
while ((io = looper->io_pending) != NULL) {
unsigned ready;
/* extract from list */
looper->io_pending = io->pendingNext;
io->pendingNext = NULL;
/* call the user callback, clear io->ready before to
* indicate that the item is not on the pending list
* anymore.
*/
ready = io->ready;
io->ready = 0;
io->user_callback(io->user_opaque, io->fd, ready);
}
}
static Duration
qlooper_now(Looper* ll)
{
return qemu_get_clock_ns(host_clock)/1000000;
}
extern void qemu_system_shutdown_request(void);
static void
qlooper_forceQuit(Looper* ll)
{
qemu_system_shutdown_request();
}
/* The user cannot call looper_run on the core event loop, because it
* is started by qemu_main() explicitely instead, so just panic. */
int
qlooper_run(Looper* ll, Duration deadline_ms)
{
APANIC("Trying to run the QEMU main event loop explicitely!");
return EINVAL;
}
static void
qlooper_destroy(Looper* ll)
{
QLooper* looper = (QLooper*)ll;
QLoopIo* io;
while ((io = looper->io_list) != NULL)
qloopio_free(io);
qemu_bh_delete(looper->io_bh);
qemu_free(looper);
}
Looper*
looper_newCore(void)
{
QLooper* looper = qemu_mallocz(sizeof(*looper));
looper->io_list = NULL;
looper->io_pending = NULL;
looper->io_bh = qemu_bh_new(qlooper_handle_io_bh, looper);
looper->looper.now = qlooper_now;
looper->looper.timer_init = qlooper_timer_init;
looper->looper.io_init = qlooper_io_init;
looper->looper.run = qlooper_run;
looper->looper.forceQuit = qlooper_forceQuit;
looper->looper.destroy = qlooper_destroy;
return &looper->looper;
}
|