blob: 746ba8b7e1f952d8622a0de8fdff3241400ea811 [file] [log] [blame]
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001/*
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
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/sysemu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010026#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010028
29#include "hw/hw.h"
30
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/timer.h"
Anthony Liguori30ea8332012-11-02 16:12:53 -050032#ifdef CONFIG_POSIX
33#include <pthread.h>
34#endif
Stefan Weilbff9f8b2012-04-20 10:27:06 +020035
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010036#ifdef _WIN32
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010037#include <mmsystem.h>
38#endif
39
Alex Bligh4e0c6522013-08-21 16:02:43 +010040#ifdef CONFIG_PPOLL
41#include <poll.h>
42#endif
43
Alex Blighcd758dd2013-08-21 16:02:44 +010044#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
45#include <sys/prctl.h>
46#endif
47
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010048/***********************************************************/
49/* timers */
50
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010051struct QEMUClock {
Alex Blighff83c662013-08-21 16:02:46 +010052 QEMUTimerList *main_loop_timerlist;
53 QLIST_HEAD(, QEMUTimerList) timerlists;
Jan Kiszka691a0c92011-06-20 14:06:27 +020054
55 NotifierList reset_notifiers;
56 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020057
Alex Blighff83c662013-08-21 16:02:46 +010058 QEMUClockType type;
Stefan Weil9a14b292012-04-20 11:51:58 +020059 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010060};
61
Alex Bligh754d6a52013-08-21 16:02:48 +010062QEMUTimerListGroup main_loop_tlg;
Alex Blighff83c662013-08-21 16:02:46 +010063QEMUClock *qemu_clocks[QEMU_CLOCK_MAX];
64
65/* A QEMUTimerList is a list of timers attached to a clock. More
66 * than one QEMUTimerList can be attached to each clock, for instance
67 * used by different AioContexts / threads. Each clock also has
68 * a list of the QEMUTimerLists associated with it, in order that
69 * reenabling the clock can call all the notifiers.
70 */
71
72struct QEMUTimerList {
Stefan Weil9a14b292012-04-20 11:51:58 +020073 QEMUClock *clock;
Alex Blighff83c662013-08-21 16:02:46 +010074 QEMUTimer *active_timers;
75 QLIST_ENTRY(QEMUTimerList) list;
Alex Blighd5541d82013-08-21 16:02:50 +010076 QEMUTimerListNotifyCB *notify_cb;
77 void *notify_opaque;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010078};
79
80struct qemu_alarm_timer {
81 char const *name;
82 int (*start)(struct qemu_alarm_timer *t);
83 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010084 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020085#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020086 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020087 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020088#elif defined(_WIN32)
89 HANDLE timer;
90#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020091 bool expired;
92 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010093};
94
95static struct qemu_alarm_timer *alarm_timer;
96
Alex Blighe93379b2013-08-21 16:02:39 +010097static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010098{
99 return timer_head && (timer_head->expire_time <= current_time);
100}
101
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100102static int64_t qemu_next_alarm_deadline(void)
103{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100104 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100105 int64_t rtdelta;
Alex Blighff83c662013-08-21 16:02:46 +0100106 int64_t hdelta;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100107
Alex Blighff83c662013-08-21 16:02:46 +0100108 if (!use_icount && vm_clock->enabled &&
109 vm_clock->main_loop_timerlist->active_timers) {
110 delta = vm_clock->main_loop_timerlist->active_timers->expire_time -
111 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100112 }
Alex Blighff83c662013-08-21 16:02:46 +0100113 if (host_clock->enabled &&
114 host_clock->main_loop_timerlist->active_timers) {
115 hdelta = host_clock->main_loop_timerlist->active_timers->expire_time -
116 qemu_get_clock_ns(host_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100117 if (hdelta < delta) {
118 delta = hdelta;
119 }
120 }
Alex Blighff83c662013-08-21 16:02:46 +0100121 if (rt_clock->enabled &&
122 rt_clock->main_loop_timerlist->active_timers) {
123 rtdelta = (rt_clock->main_loop_timerlist->active_timers->expire_time -
124 qemu_get_clock_ns(rt_clock));
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100125 if (rtdelta < delta) {
126 delta = rtdelta;
127 }
128 }
129
130 return delta;
131}
132
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100133static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
134{
Stefano Stabellini82274212012-05-29 03:35:24 +0000135 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
136 if (nearest_delta_ns < INT64_MAX) {
137 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100138 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100139}
140
Paolo Bonzini9c132462011-02-03 14:48:59 +0100141/* TODO: MIN_TIMER_REARM_NS should be optimized */
142#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100143
144#ifdef _WIN32
145
Stefan Weil2f9cba02011-04-05 18:34:21 +0200146static int mm_start_timer(struct qemu_alarm_timer *t);
147static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100148static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200149
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100150static int win32_start_timer(struct qemu_alarm_timer *t);
151static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100152static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100153
154#else
155
156static int unix_start_timer(struct qemu_alarm_timer *t);
157static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100158static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100159
160#ifdef __linux__
161
162static int dynticks_start_timer(struct qemu_alarm_timer *t);
163static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100164static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100165
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100166#endif /* __linux__ */
167
168#endif /* _WIN32 */
169
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100170static struct qemu_alarm_timer alarm_timers[] = {
171#ifndef _WIN32
172#ifdef __linux__
173 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200174 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100175#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200176 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100177#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100178 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200179 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100180#endif
181 {NULL, }
182};
183
184static void show_available_alarms(void)
185{
186 int i;
187
188 printf("Available alarm timers, in order of precedence:\n");
189 for (i = 0; alarm_timers[i].name; i++)
190 printf("%s\n", alarm_timers[i].name);
191}
192
193void configure_alarms(char const *opt)
194{
195 int i;
196 int cur = 0;
197 int count = ARRAY_SIZE(alarm_timers) - 1;
198 char *arg;
199 char *name;
200 struct qemu_alarm_timer tmp;
201
Peter Maydellc8057f92012-08-02 13:45:54 +0100202 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100203 show_available_alarms();
204 exit(0);
205 }
206
Anthony Liguori7267c092011-08-20 22:09:37 -0500207 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100208
209 /* Reorder the array */
210 name = strtok(arg, ",");
211 while (name) {
212 for (i = 0; i < count && alarm_timers[i].name; i++) {
213 if (!strcmp(alarm_timers[i].name, name))
214 break;
215 }
216
217 if (i == count) {
218 fprintf(stderr, "Unknown clock %s\n", name);
219 goto next;
220 }
221
222 if (i < cur)
223 /* Ignore */
224 goto next;
225
226 /* Swap */
227 tmp = alarm_timers[i];
228 alarm_timers[i] = alarm_timers[cur];
229 alarm_timers[cur] = tmp;
230
231 cur++;
232next:
233 name = strtok(NULL, ",");
234 }
235
Anthony Liguori7267c092011-08-20 22:09:37 -0500236 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100237
238 if (cur) {
239 /* Disable remaining timers */
240 for (i = cur; i < count; i++)
241 alarm_timers[i].name = NULL;
242 } else {
243 show_available_alarms();
244 exit(1);
245 }
246}
247
Alex Blighd5541d82013-08-21 16:02:50 +0100248static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock,
249 QEMUTimerListNotifyCB *cb,
250 void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +0100251{
252 QEMUTimerList *timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100253
Alex Blighff83c662013-08-21 16:02:46 +0100254 /* Assert if we do not have a clock. If you see this
255 * assertion in means that the clocks have not been
256 * initialised before a timerlist is needed. This
257 * normally happens if an AioContext is used before
258 * init_clocks() is called within main().
259 */
260 assert(clock);
261
262 timer_list = g_malloc0(sizeof(QEMUTimerList));
263 timer_list->clock = clock;
Alex Blighd5541d82013-08-21 16:02:50 +0100264 timer_list->notify_cb = cb;
265 timer_list->notify_opaque = opaque;
Alex Blighff83c662013-08-21 16:02:46 +0100266 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
267 return timer_list;
268}
269
Alex Blighd5541d82013-08-21 16:02:50 +0100270QEMUTimerList *timerlist_new(QEMUClockType type,
271 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +0100272{
Alex Blighd5541d82013-08-21 16:02:50 +0100273 return timerlist_new_from_clock(qemu_clock_ptr(type), cb, opaque);
Alex Blighff83c662013-08-21 16:02:46 +0100274}
275
276void timerlist_free(QEMUTimerList *timer_list)
277{
278 assert(!timerlist_has_timers(timer_list));
279 if (timer_list->clock) {
280 QLIST_REMOVE(timer_list, list);
281 if (timer_list->clock->main_loop_timerlist == timer_list) {
282 timer_list->clock->main_loop_timerlist = NULL;
283 }
284 }
285 g_free(timer_list);
286}
287
288static QEMUClock *qemu_clock_new(QEMUClockType type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100289{
290 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200291
Anthony Liguori7267c092011-08-20 22:09:37 -0500292 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100293 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200294 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200295 clock->last = INT64_MIN;
Alex Blighff83c662013-08-21 16:02:46 +0100296 QLIST_INIT(&clock->timerlists);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200297 notifier_list_init(&clock->reset_notifiers);
Alex Blighd5541d82013-08-21 16:02:50 +0100298 clock->main_loop_timerlist = timerlist_new_from_clock(clock, NULL, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100299 return clock;
300}
301
Alex Blighff83c662013-08-21 16:02:46 +0100302bool qemu_clock_use_for_deadline(QEMUClock *clock)
303{
304 return !(use_icount && (clock->type == QEMU_CLOCK_VIRTUAL));
305}
306
Alex Blighb1bbfe72013-08-21 16:02:55 +0100307void qemu_clock_notify(QEMUClock *clock)
308{
309 QEMUTimerList *timer_list;
310 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
311 timerlist_notify(timer_list);
312 }
313}
314
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200315void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100316{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200317 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100318 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200319 if (enabled && !old) {
Alex Blighb1bbfe72013-08-21 16:02:55 +0100320 qemu_clock_notify(clock);
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200321 qemu_rearm_alarm_timer(alarm_timer);
322 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100323}
324
Alex Blighff83c662013-08-21 16:02:46 +0100325bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200326{
Alex Blighff83c662013-08-21 16:02:46 +0100327 return !!timer_list->active_timers;
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200328}
329
Alex Blighff83c662013-08-21 16:02:46 +0100330bool qemu_clock_has_timers(QEMUClock *clock)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200331{
Alex Blighff83c662013-08-21 16:02:46 +0100332 return timerlist_has_timers(clock->main_loop_timerlist);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200333}
334
Alex Blighff83c662013-08-21 16:02:46 +0100335bool timerlist_expired(QEMUTimerList *timer_list)
336{
337 return (timer_list->active_timers &&
338 timer_list->active_timers->expire_time <
339 qemu_get_clock_ns(timer_list->clock));
340}
341
342bool qemu_clock_expired(QEMUClock *clock)
343{
344 return timerlist_expired(clock->main_loop_timerlist);
345}
346
347int64_t timerlist_deadline(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200348{
349 /* To avoid problems with overflow limit this to 2^32. */
350 int64_t delta = INT32_MAX;
351
Alex Blighff83c662013-08-21 16:02:46 +0100352 if (timer_list->clock->enabled && timer_list->active_timers) {
353 delta = timer_list->active_timers->expire_time -
354 qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200355 }
356 if (delta < 0) {
357 delta = 0;
358 }
359 return delta;
360}
361
Alex Blighff83c662013-08-21 16:02:46 +0100362int64_t qemu_clock_deadline(QEMUClock *clock)
363{
364 return timerlist_deadline(clock->main_loop_timerlist);
365}
366
Alex Bligh02a03a92013-08-21 16:02:41 +0100367/*
368 * As above, but return -1 for no deadline, and do not cap to 2^32
369 * as we know the result is always positive.
370 */
371
Alex Blighff83c662013-08-21 16:02:46 +0100372int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
Alex Bligh02a03a92013-08-21 16:02:41 +0100373{
374 int64_t delta;
375
Alex Blighff83c662013-08-21 16:02:46 +0100376 if (!timer_list->clock->enabled || !timer_list->active_timers) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100377 return -1;
378 }
379
Alex Blighff83c662013-08-21 16:02:46 +0100380 delta = timer_list->active_timers->expire_time -
381 qemu_get_clock_ns(timer_list->clock);
Alex Bligh02a03a92013-08-21 16:02:41 +0100382
383 if (delta <= 0) {
384 return 0;
385 }
386
387 return delta;
388}
389
Alex Blighff83c662013-08-21 16:02:46 +0100390int64_t qemu_clock_deadline_ns(QEMUClock *clock)
391{
392 return timerlist_deadline_ns(clock->main_loop_timerlist);
393}
394
395QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list)
396{
397 return timer_list->clock;
398}
399
400QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock)
401{
402 return clock->main_loop_timerlist;
403}
404
Alex Blighd5541d82013-08-21 16:02:50 +0100405void timerlist_notify(QEMUTimerList *timer_list)
406{
407 if (timer_list->notify_cb) {
408 timer_list->notify_cb(timer_list->notify_opaque);
409 } else {
410 qemu_notify_event();
411 }
412}
413
Alex Bligh02a03a92013-08-21 16:02:41 +0100414/* Transition function to convert a nanosecond timeout to ms
415 * This is used where a system does not support ppoll
416 */
417int qemu_timeout_ns_to_ms(int64_t ns)
418{
419 int64_t ms;
420 if (ns < 0) {
421 return -1;
422 }
423
424 if (!ns) {
425 return 0;
426 }
427
428 /* Always round up, because it's better to wait too long than to wait too
429 * little and effectively busy-wait
430 */
431 ms = (ns + SCALE_MS - 1) / SCALE_MS;
432
433 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
434 if (ms > (int64_t) INT32_MAX) {
435 ms = INT32_MAX;
436 }
437
438 return (int) ms;
439}
440
441
Alex Bligh4e0c6522013-08-21 16:02:43 +0100442/* qemu implementation of g_poll which uses a nanosecond timeout but is
443 * otherwise identical to g_poll
444 */
445int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
446{
447#ifdef CONFIG_PPOLL
448 if (timeout < 0) {
449 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
450 } else {
451 struct timespec ts;
452 ts.tv_sec = timeout / 1000000000LL;
453 ts.tv_nsec = timeout % 1000000000LL;
454 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
455 }
456#else
457 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
458#endif
459}
460
461
Alex Blighff83c662013-08-21 16:02:46 +0100462void timer_init(QEMUTimer *ts,
463 QEMUTimerList *timer_list, int scale,
464 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100465{
Alex Blighff83c662013-08-21 16:02:46 +0100466 ts->timer_list = timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100467 ts->cb = cb;
468 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100469 ts->scale = scale;
Alex Blighff83c662013-08-21 16:02:46 +0100470}
471
472QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
473 QEMUTimerCB *cb, void *opaque)
474{
475 return timer_new_tl(clock->main_loop_timerlist,
476 scale, cb, opaque);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100477}
478
479void qemu_free_timer(QEMUTimer *ts)
480{
Anthony Liguori7267c092011-08-20 22:09:37 -0500481 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100482}
483
484/* stop a timer, but do not dealloc it */
485void qemu_del_timer(QEMUTimer *ts)
486{
487 QEMUTimer **pt, *t;
488
489 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100490 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100491 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100492 for(;;) {
493 t = *pt;
494 if (!t)
495 break;
496 if (t == ts) {
497 *pt = t->next;
498 break;
499 }
500 pt = &t->next;
501 }
502}
503
504/* modify the current timer so that it will be fired when current_time
505 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200506void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100507{
508 QEMUTimer **pt, *t;
509
510 qemu_del_timer(ts);
511
512 /* add the timer in the sorted list */
513 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100514 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100515 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100516 for(;;) {
517 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100518 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100519 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100520 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100521 pt = &t->next;
522 }
523 ts->expire_time = expire_time;
524 ts->next = *pt;
525 *pt = ts;
526
527 /* Rearm if necessary */
Alex Blighff83c662013-08-21 16:02:46 +0100528 if (pt == &ts->timer_list->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100529 if (!alarm_timer->pending) {
530 qemu_rearm_alarm_timer(alarm_timer);
531 }
532 /* Interrupt execution to force deadline recalculation. */
Alex Blighff83c662013-08-21 16:02:46 +0100533 qemu_clock_warp(ts->timer_list->clock);
Alex Blighb1bbfe72013-08-21 16:02:55 +0100534 timerlist_notify(ts->timer_list);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100535 }
536}
537
Paolo Bonzini4a998742011-03-11 16:33:58 +0100538void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
539{
540 qemu_mod_timer_ns(ts, expire_time * ts->scale);
541}
542
Alex Blighe93379b2013-08-21 16:02:39 +0100543bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100544{
545 QEMUTimer *t;
Alex Blighff83c662013-08-21 16:02:46 +0100546 for (t = ts->timer_list->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200547 if (t == ts) {
548 return true;
549 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100550 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200551 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100552}
553
Alex Blighe93379b2013-08-21 16:02:39 +0100554bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100555{
Alex Blighe93379b2013-08-21 16:02:39 +0100556 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100557}
558
Alex Blighff83c662013-08-21 16:02:46 +0100559bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100560{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200561 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100562 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100563 bool progress = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100564
Alex Blighff83c662013-08-21 16:02:46 +0100565 if (!timer_list->clock->enabled) {
Alex Blighf9a976b2013-08-21 16:02:45 +0100566 return progress;
Alex Blighff83c662013-08-21 16:02:46 +0100567 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100568
Alex Blighff83c662013-08-21 16:02:46 +0100569 current_time = qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100570 for(;;) {
Alex Blighff83c662013-08-21 16:02:46 +0100571 ts = timer_list->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100572 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100573 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100574 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100575 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100576 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100577 ts->next = NULL;
578
579 /* run the callback (the timer list can be modified) */
580 ts->cb(ts->opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100581 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100582 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100583 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100584}
585
Alex Blighff83c662013-08-21 16:02:46 +0100586bool qemu_run_timers(QEMUClock *clock)
587{
588 return timerlist_run_timers(clock->main_loop_timerlist);
589}
590
Alex Blighd5541d82013-08-21 16:02:50 +0100591void timerlistgroup_init(QEMUTimerListGroup *tlg,
592 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Bligh754d6a52013-08-21 16:02:48 +0100593{
594 QEMUClockType type;
595 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Blighd5541d82013-08-21 16:02:50 +0100596 tlg->tl[type] = timerlist_new(type, cb, opaque);
Alex Bligh754d6a52013-08-21 16:02:48 +0100597 }
598}
599
600void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
601{
602 QEMUClockType type;
603 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
604 timerlist_free(tlg->tl[type]);
605 }
606}
607
608bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
609{
610 QEMUClockType type;
611 bool progress = false;
612 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
613 progress |= timerlist_run_timers(tlg->tl[type]);
614 }
615 return progress;
616}
617
618int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
619{
620 int64_t deadline = -1;
621 QEMUClockType type;
622 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
623 if (qemu_clock_use_for_deadline(tlg->tl[type]->clock)) {
624 deadline = qemu_soonest_timeout(deadline,
625 timerlist_deadline_ns(
626 tlg->tl[type]));
627 }
628 }
629 return deadline;
630}
631
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100632int64_t qemu_get_clock_ns(QEMUClock *clock)
633{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200634 int64_t now, last;
635
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100636 switch(clock->type) {
637 case QEMU_CLOCK_REALTIME:
638 return get_clock();
639 default:
640 case QEMU_CLOCK_VIRTUAL:
641 if (use_icount) {
642 return cpu_get_icount();
643 } else {
644 return cpu_get_clock();
645 }
646 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200647 now = get_clock_realtime();
648 last = clock->last;
649 clock->last = now;
650 if (now < last) {
651 notifier_list_notify(&clock->reset_notifiers, &now);
652 }
653 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100654 }
655}
656
Jan Kiszka691a0c92011-06-20 14:06:27 +0200657void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
658{
659 notifier_list_add(&clock->reset_notifiers, notifier);
660}
661
662void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
663{
Paolo Bonzini31552522012-01-13 17:34:01 +0100664 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200665}
666
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100667void init_clocks(void)
668{
Alex Blighff83c662013-08-21 16:02:46 +0100669 QEMUClockType type;
670 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
671 if (!qemu_clocks[type]) {
672 qemu_clocks[type] = qemu_clock_new(type);
Alex Bligh754d6a52013-08-21 16:02:48 +0100673 main_loop_tlg.tl[type] = qemu_clocks[type]->main_loop_timerlist;
Alex Blighff83c662013-08-21 16:02:46 +0100674 }
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100675 }
Alex Blighff83c662013-08-21 16:02:46 +0100676
Alex Blighcd758dd2013-08-21 16:02:44 +0100677#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
678 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
679#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100680}
681
Alex Blighe93379b2013-08-21 16:02:39 +0100682uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100683{
Alex Blighe93379b2013-08-21 16:02:39 +0100684 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100685}
686
Alex Blighf9a976b2013-08-21 16:02:45 +0100687bool qemu_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100688{
Alex Blighf9a976b2013-08-21 16:02:45 +0100689 bool progress = false;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200690 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100691
Peter Portante158fd3c2012-04-05 11:00:45 -0400692 /* vm time timers */
Alex Blighff83c662013-08-21 16:02:46 +0100693 QEMUClockType type;
694 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
695 progress |= qemu_run_timers(qemu_clock_ptr(type));
696 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400697
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100698 /* rearm timer, if not periodic */
699 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200700 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100701 qemu_rearm_alarm_timer(alarm_timer);
702 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100703
704 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100705}
706
707#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100708static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100709#else
710static void host_alarm_handler(int host_signum)
711#endif
712{
713 struct qemu_alarm_timer *t = alarm_timer;
714 if (!t)
715 return;
716
Stefan Weil82051992012-04-20 11:27:24 +0200717 t->expired = true;
718 t->pending = true;
719 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100720}
721
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100722#if defined(__linux__)
723
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100724#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200725
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100726static int dynticks_start_timer(struct qemu_alarm_timer *t)
727{
728 struct sigevent ev;
729 timer_t host_timer;
730 struct sigaction act;
731
732 sigfillset(&act.sa_mask);
733 act.sa_flags = 0;
734 act.sa_handler = host_alarm_handler;
735
736 sigaction(SIGALRM, &act, NULL);
737
738 /*
739 * Initialize ev struct to 0 to avoid valgrind complaining
740 * about uninitialized data in timer_create call
741 */
742 memset(&ev, 0, sizeof(ev));
743 ev.sigev_value.sival_int = 0;
744 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000745#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200746 if (qemu_signalfd_available()) {
747 ev.sigev_notify = SIGEV_THREAD_ID;
748 ev._sigev_un._tid = qemu_get_thread_id();
749 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000750#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100751 ev.sigev_signo = SIGALRM;
752
753 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
754 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100755 return -1;
756 }
757
Stefan Weilcd0544e2011-04-10 20:15:09 +0200758 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100759
760 return 0;
761}
762
763static void dynticks_stop_timer(struct qemu_alarm_timer *t)
764{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200765 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100766
767 timer_delete(host_timer);
768}
769
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100770static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
771 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100772{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200773 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100774 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100775 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100776
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100777 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
778 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100779
780 /* check whether a timer is already running */
781 if (timer_gettime(host_timer, &timeout)) {
782 perror("gettime");
783 fprintf(stderr, "Internal timer error: aborting\n");
784 exit(1);
785 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100786 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
787 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100788 return;
789
790 timeout.it_interval.tv_sec = 0;
791 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100792 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
793 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100794 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
795 perror("settime");
796 fprintf(stderr, "Internal timer error: aborting\n");
797 exit(1);
798 }
799}
800
801#endif /* defined(__linux__) */
802
Stefan Weilf26e5a52011-02-04 22:01:32 +0100803#if !defined(_WIN32)
804
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100805static int unix_start_timer(struct qemu_alarm_timer *t)
806{
807 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100808
809 /* timer signal */
810 sigfillset(&act.sa_mask);
811 act.sa_flags = 0;
812 act.sa_handler = host_alarm_handler;
813
814 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200815 return 0;
816}
817
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100818static void unix_rearm_timer(struct qemu_alarm_timer *t,
819 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200820{
821 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200822 int err;
823
Paolo Bonzini84682832011-06-09 13:10:25 +0200824 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
825 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100826
827 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200828 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
829 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
830 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100831 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200832 if (err) {
833 perror("setitimer");
834 fprintf(stderr, "Internal timer error: aborting\n");
835 exit(1);
836 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100837}
838
839static void unix_stop_timer(struct qemu_alarm_timer *t)
840{
841 struct itimerval itv;
842
843 memset(&itv, 0, sizeof(itv));
844 setitimer(ITIMER_REAL, &itv, NULL);
845}
846
847#endif /* !defined(_WIN32) */
848
849
850#ifdef _WIN32
851
Stefan Weil2f9cba02011-04-05 18:34:21 +0200852static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000853static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200854
855static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
856 DWORD_PTR dwUser, DWORD_PTR dw1,
857 DWORD_PTR dw2)
858{
859 struct qemu_alarm_timer *t = alarm_timer;
860 if (!t) {
861 return;
862 }
Stefan Weil82051992012-04-20 11:27:24 +0200863 t->expired = true;
864 t->pending = true;
865 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200866}
867
868static int mm_start_timer(struct qemu_alarm_timer *t)
869{
Stefan Weil40f08e82012-04-27 05:34:40 +0000870 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200871 return 0;
872}
873
874static void mm_stop_timer(struct qemu_alarm_timer *t)
875{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100876 if (mm_timer) {
877 timeKillEvent(mm_timer);
878 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200879}
880
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100881static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200882{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100883 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000884 if (nearest_delta_ms < mm_tc.wPeriodMin) {
885 nearest_delta_ms = mm_tc.wPeriodMin;
886 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
887 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100888 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100889
Paolo Bonzini0727b862013-02-20 14:43:31 +0100890 if (mm_timer) {
891 timeKillEvent(mm_timer);
892 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000893 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
894 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200895 mm_alarm_handler,
896 (DWORD_PTR)t,
897 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
898
899 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200900 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000901 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200902 exit(1);
903 }
904}
905
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100906static int win32_start_timer(struct qemu_alarm_timer *t)
907{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100908 HANDLE hTimer;
909 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100910
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100911 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
912 is zero) that has already expired, the timer is not updated. Since
913 creating a new timer is relatively expensive, set a bogus one-hour
914 interval in the dynticks case. */
915 success = CreateTimerQueueTimer(&hTimer,
916 NULL,
917 host_alarm_handler,
918 t,
919 1,
Stefan Weil82051992012-04-20 11:27:24 +0200920 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100921 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100922
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100923 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100924 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
925 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100926 return -1;
927 }
928
Stefan Weilcd0544e2011-04-10 20:15:09 +0200929 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100930 return 0;
931}
932
933static void win32_stop_timer(struct qemu_alarm_timer *t)
934{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200935 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100936
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100937 if (hTimer) {
938 DeleteTimerQueueTimer(NULL, hTimer, NULL);
939 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100940}
941
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100942static void win32_rearm_timer(struct qemu_alarm_timer *t,
943 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100944{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200945 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100946 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100947 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100948
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100949 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100950 if (nearest_delta_ms < 1) {
951 nearest_delta_ms = 1;
952 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100953 /* ULONG_MAX can be 32 bit */
954 if (nearest_delta_ms > ULONG_MAX) {
955 nearest_delta_ms = ULONG_MAX;
956 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100957 success = ChangeTimerQueueTimer(NULL,
958 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100959 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100960 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100961
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100962 if (!success) {
963 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100964 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100965 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100966 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100967
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100968}
969
970#endif /* _WIN32 */
971
Paolo Bonzini4260a732011-09-19 10:18:51 +0200972static void quit_timers(void)
973{
974 struct qemu_alarm_timer *t = alarm_timer;
975 alarm_timer = NULL;
976 t->stop(t);
977}
978
Stefan Weil253ecf82012-11-04 21:42:08 +0100979#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100980static void reinit_timers(void)
981{
982 struct qemu_alarm_timer *t = alarm_timer;
983 t->stop(t);
984 if (t->start(t)) {
985 fprintf(stderr, "Internal timer error: aborting\n");
986 exit(1);
987 }
988 qemu_rearm_alarm_timer(t);
989}
Stefan Weil253ecf82012-11-04 21:42:08 +0100990#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100991
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100992int init_timer_alarm(void)
993{
994 struct qemu_alarm_timer *t = NULL;
995 int i, err = -1;
996
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100997 if (alarm_timer) {
998 return 0;
999 }
1000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001001 for (i = 0; alarm_timers[i].name; i++) {
1002 t = &alarm_timers[i];
1003
1004 err = t->start(t);
1005 if (!err)
1006 break;
1007 }
1008
1009 if (err) {
1010 err = -ENOENT;
1011 goto fail;
1012 }
1013
Paolo Bonzini4260a732011-09-19 10:18:51 +02001014 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +01001015#ifdef CONFIG_POSIX
1016 pthread_atfork(NULL, NULL, reinit_timers);
1017#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001018 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001019 return 0;
1020
1021fail:
1022 return err;
1023}
1024