]> Git Repo - qemu.git/blame - qemu-timer.c
qemu-timer: make qemu_timer_mod_ns() and qemu_timer_del() thread-safe
[qemu.git] / qemu-timer.c
CommitLineData
db1a4972
PB
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
9c17d615 25#include "sysemu/sysemu.h"
83c9089e 26#include "monitor/monitor.h"
28ecbaee 27#include "ui/console.h"
db1a4972
PB
28
29#include "hw/hw.h"
30
1de7afc9 31#include "qemu/timer.h"
30ea8339
AL
32#ifdef CONFIG_POSIX
33#include <pthread.h>
34#endif
bff9f8bf 35
4e0c6529
AB
36#ifdef CONFIG_PPOLL
37#include <poll.h>
38#endif
39
cd758dd0
AB
40#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
41#include <sys/prctl.h>
42#endif
43
db1a4972
PB
44/***********************************************************/
45/* timers */
46
b4049b74 47typedef struct QEMUClock {
ff83c66e 48 QLIST_HEAD(, QEMUTimerList) timerlists;
691a0c9c
JK
49
50 NotifierList reset_notifiers;
51 int64_t last;
9a14b298 52
ff83c66e 53 QEMUClockType type;
9a14b298 54 bool enabled;
b4049b74 55} QEMUClock;
db1a4972 56
754d6a54 57QEMUTimerListGroup main_loop_tlg;
7bf8fbde 58QEMUClock qemu_clocks[QEMU_CLOCK_MAX];
ff83c66e
AB
59
60/* A QEMUTimerList is a list of timers attached to a clock. More
61 * than one QEMUTimerList can be attached to each clock, for instance
62 * used by different AioContexts / threads. Each clock also has
63 * a list of the QEMUTimerLists associated with it, in order that
64 * reenabling the clock can call all the notifiers.
65 */
66
67struct QEMUTimerList {
9a14b298 68 QEMUClock *clock;
978f2205 69 QemuMutex active_timers_lock;
ff83c66e
AB
70 QEMUTimer *active_timers;
71 QLIST_ENTRY(QEMUTimerList) list;
d5541d86
AB
72 QEMUTimerListNotifyCB *notify_cb;
73 void *notify_opaque;
db1a4972
PB
74};
75
7bf8fbde
AB
76/**
77 * qemu_clock_ptr:
78 * @type: type of clock
79 *
80 * Translate a clock type into a pointer to QEMUClock object.
81 *
82 * Returns: a pointer to the QEMUClock object
83 */
b4049b74 84static inline QEMUClock *qemu_clock_ptr(QEMUClockType type)
7bf8fbde
AB
85{
86 return &qemu_clocks[type];
87}
88
e93379b0 89static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
45c7b37f
SW
90{
91 return timer_head && (timer_head->expire_time <= current_time);
92}
93
7bf8fbde
AB
94QEMUTimerList *timerlist_new(QEMUClockType type,
95 QEMUTimerListNotifyCB *cb,
96 void *opaque)
ff83c66e
AB
97{
98 QEMUTimerList *timer_list;
7bf8fbde 99 QEMUClock *clock = qemu_clock_ptr(type);
ff83c66e
AB
100
101 timer_list = g_malloc0(sizeof(QEMUTimerList));
102 timer_list->clock = clock;
d5541d86
AB
103 timer_list->notify_cb = cb;
104 timer_list->notify_opaque = opaque;
978f2205 105 qemu_mutex_init(&timer_list->active_timers_lock);
ff83c66e
AB
106 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
107 return timer_list;
108}
109
ff83c66e
AB
110void timerlist_free(QEMUTimerList *timer_list)
111{
112 assert(!timerlist_has_timers(timer_list));
113 if (timer_list->clock) {
114 QLIST_REMOVE(timer_list, list);
ff83c66e 115 }
978f2205 116 qemu_mutex_destroy(&timer_list->active_timers_lock);
ff83c66e
AB
117 g_free(timer_list);
118}
119
7bf8fbde 120static void qemu_clock_init(QEMUClockType type)
db1a4972 121{
7bf8fbde 122 QEMUClock *clock = qemu_clock_ptr(type);
691a0c9c 123
db1a4972 124 clock->type = type;
5e1ec7b2 125 clock->enabled = true;
2ff68d07 126 clock->last = INT64_MIN;
ff83c66e 127 QLIST_INIT(&clock->timerlists);
691a0c9c 128 notifier_list_init(&clock->reset_notifiers);
7bf8fbde 129 main_loop_tlg.tl[type] = timerlist_new(type, NULL, NULL);
db1a4972
PB
130}
131
40daca54 132bool qemu_clock_use_for_deadline(QEMUClockType type)
ff83c66e 133{
40daca54 134 return !(use_icount && (type == QEMU_CLOCK_VIRTUAL));
ff83c66e
AB
135}
136
40daca54 137void qemu_clock_notify(QEMUClockType type)
b1bbfe72
AB
138{
139 QEMUTimerList *timer_list;
40daca54 140 QEMUClock *clock = qemu_clock_ptr(type);
b1bbfe72
AB
141 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
142 timerlist_notify(timer_list);
143 }
144}
145
40daca54 146void qemu_clock_enable(QEMUClockType type, bool enabled)
db1a4972 147{
40daca54 148 QEMUClock *clock = qemu_clock_ptr(type);
fbdc14eb 149 bool old = clock->enabled;
db1a4972 150 clock->enabled = enabled;
fbdc14eb 151 if (enabled && !old) {
40daca54 152 qemu_clock_notify(type);
fbdc14eb 153 }
db1a4972
PB
154}
155
ff83c66e 156bool timerlist_has_timers(QEMUTimerList *timer_list)
dc2dfcf0 157{
ff83c66e 158 return !!timer_list->active_timers;
dc2dfcf0
PB
159}
160
40daca54 161bool qemu_clock_has_timers(QEMUClockType type)
dc2dfcf0 162{
40daca54 163 return timerlist_has_timers(
7bf8fbde 164 main_loop_tlg.tl[type]);
dc2dfcf0
PB
165}
166
ff83c66e
AB
167bool timerlist_expired(QEMUTimerList *timer_list)
168{
978f2205
SH
169 int64_t expire_time;
170
171 qemu_mutex_lock(&timer_list->active_timers_lock);
172 if (!timer_list->active_timers) {
173 qemu_mutex_unlock(&timer_list->active_timers_lock);
174 return false;
175 }
176 expire_time = timer_list->active_timers->expire_time;
177 qemu_mutex_unlock(&timer_list->active_timers_lock);
178
179 return expire_time < qemu_clock_get_ns(timer_list->clock->type);
ff83c66e
AB
180}
181
40daca54 182bool qemu_clock_expired(QEMUClockType type)
ff83c66e 183{
40daca54 184 return timerlist_expired(
7bf8fbde 185 main_loop_tlg.tl[type]);
ff83c66e
AB
186}
187
02a03a9f
AB
188/*
189 * As above, but return -1 for no deadline, and do not cap to 2^32
190 * as we know the result is always positive.
191 */
192
ff83c66e 193int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
02a03a9f
AB
194{
195 int64_t delta;
978f2205 196 int64_t expire_time;
02a03a9f 197
978f2205 198 if (!timer_list->clock->enabled) {
02a03a9f
AB
199 return -1;
200 }
201
978f2205
SH
202 /* The active timers list may be modified before the caller uses our return
203 * value but ->notify_cb() is called when the deadline changes. Therefore
204 * the caller should notice the change and there is no race condition.
205 */
206 qemu_mutex_lock(&timer_list->active_timers_lock);
207 if (!timer_list->active_timers) {
208 qemu_mutex_unlock(&timer_list->active_timers_lock);
209 return -1;
210 }
211 expire_time = timer_list->active_timers->expire_time;
212 qemu_mutex_unlock(&timer_list->active_timers_lock);
213
214 delta = expire_time - qemu_clock_get_ns(timer_list->clock->type);
02a03a9f
AB
215
216 if (delta <= 0) {
217 return 0;
218 }
219
220 return delta;
221}
222
ac70aafc
AB
223/* Calculate the soonest deadline across all timerlists attached
224 * to the clock. This is used for the icount timeout so we
225 * ignore whether or not the clock should be used in deadline
226 * calculations.
227 */
40daca54 228int64_t qemu_clock_deadline_ns_all(QEMUClockType type)
ac70aafc
AB
229{
230 int64_t deadline = -1;
231 QEMUTimerList *timer_list;
40daca54 232 QEMUClock *clock = qemu_clock_ptr(type);
ac70aafc
AB
233 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
234 deadline = qemu_soonest_timeout(deadline,
235 timerlist_deadline_ns(timer_list));
236 }
237 return deadline;
238}
239
40daca54 240QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list)
ff83c66e 241{
40daca54 242 return timer_list->clock->type;
ff83c66e
AB
243}
244
40daca54 245QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)
ff83c66e 246{
7bf8fbde 247 return main_loop_tlg.tl[type];
ff83c66e
AB
248}
249
d5541d86
AB
250void timerlist_notify(QEMUTimerList *timer_list)
251{
252 if (timer_list->notify_cb) {
253 timer_list->notify_cb(timer_list->notify_opaque);
254 } else {
255 qemu_notify_event();
256 }
257}
258
02a03a9f
AB
259/* Transition function to convert a nanosecond timeout to ms
260 * This is used where a system does not support ppoll
261 */
262int qemu_timeout_ns_to_ms(int64_t ns)
263{
264 int64_t ms;
265 if (ns < 0) {
266 return -1;
267 }
268
269 if (!ns) {
270 return 0;
271 }
272
273 /* Always round up, because it's better to wait too long than to wait too
274 * little and effectively busy-wait
275 */
276 ms = (ns + SCALE_MS - 1) / SCALE_MS;
277
278 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
279 if (ms > (int64_t) INT32_MAX) {
280 ms = INT32_MAX;
281 }
282
283 return (int) ms;
284}
285
286
4e0c6529
AB
287/* qemu implementation of g_poll which uses a nanosecond timeout but is
288 * otherwise identical to g_poll
289 */
290int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
291{
292#ifdef CONFIG_PPOLL
293 if (timeout < 0) {
294 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
295 } else {
296 struct timespec ts;
297 ts.tv_sec = timeout / 1000000000LL;
298 ts.tv_nsec = timeout % 1000000000LL;
299 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
300 }
301#else
302 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
303#endif
304}
305
306
ff83c66e
AB
307void timer_init(QEMUTimer *ts,
308 QEMUTimerList *timer_list, int scale,
309 QEMUTimerCB *cb, void *opaque)
db1a4972 310{
ff83c66e 311 ts->timer_list = timer_list;
db1a4972
PB
312 ts->cb = cb;
313 ts->opaque = opaque;
4a998740 314 ts->scale = scale;
ff83c66e
AB
315}
316
40daca54 317void timer_free(QEMUTimer *ts)
db1a4972 318{
7267c094 319 g_free(ts);
db1a4972
PB
320}
321
978f2205 322static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
db1a4972
PB
323{
324 QEMUTimer **pt, *t;
325
978f2205 326 pt = &timer_list->active_timers;
db1a4972
PB
327 for(;;) {
328 t = *pt;
329 if (!t)
330 break;
331 if (t == ts) {
332 *pt = t->next;
333 break;
334 }
335 pt = &t->next;
336 }
337}
338
978f2205
SH
339/* stop a timer, but do not dealloc it */
340void timer_del(QEMUTimer *ts)
341{
342 QEMUTimerList *timer_list = ts->timer_list;
343
344 qemu_mutex_lock(&timer_list->active_timers_lock);
345 timer_del_locked(timer_list, ts);
346 qemu_mutex_unlock(&timer_list->active_timers_lock);
347}
348
db1a4972
PB
349/* modify the current timer so that it will be fired when current_time
350 >= expire_time. The corresponding callback will be called. */
40daca54 351void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
db1a4972 352{
978f2205 353 QEMUTimerList *timer_list = ts->timer_list;
db1a4972
PB
354 QEMUTimer **pt, *t;
355
978f2205
SH
356 qemu_mutex_lock(&timer_list->active_timers_lock);
357 timer_del_locked(timer_list, ts);
db1a4972
PB
358
359 /* add the timer in the sorted list */
978f2205 360 pt = &timer_list->active_timers;
db1a4972
PB
361 for(;;) {
362 t = *pt;
e93379b0 363 if (!timer_expired_ns(t, expire_time)) {
db1a4972 364 break;
45c7b37f 365 }
db1a4972
PB
366 pt = &t->next;
367 }
368 ts->expire_time = expire_time;
369 ts->next = *pt;
370 *pt = ts;
978f2205 371 qemu_mutex_unlock(&timer_list->active_timers_lock);
db1a4972
PB
372
373 /* Rearm if necessary */
978f2205 374 if (pt == &timer_list->active_timers) {
db1a4972 375 /* Interrupt execution to force deadline recalculation. */
978f2205
SH
376 qemu_clock_warp(timer_list->clock->type);
377 timerlist_notify(timer_list);
db1a4972
PB
378 }
379}
380
40daca54 381void timer_mod(QEMUTimer *ts, int64_t expire_time)
4a998740 382{
40daca54 383 timer_mod_ns(ts, expire_time * ts->scale);
4a998740
PB
384}
385
e93379b0 386bool timer_pending(QEMUTimer *ts)
db1a4972 387{
978f2205 388 QEMUTimerList *timer_list = ts->timer_list;
db1a4972 389 QEMUTimer *t;
978f2205
SH
390 bool found = false;
391
392 qemu_mutex_lock(&timer_list->active_timers_lock);
393 for (t = timer_list->active_timers; t != NULL; t = t->next) {
5e1ec7b2 394 if (t == ts) {
978f2205
SH
395 found = true;
396 break;
5e1ec7b2 397 }
db1a4972 398 }
978f2205
SH
399 qemu_mutex_unlock(&timer_list->active_timers_lock);
400 return found;
db1a4972
PB
401}
402
e93379b0 403bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
db1a4972 404{
e93379b0 405 return timer_expired_ns(timer_head, current_time * timer_head->scale);
db1a4972
PB
406}
407
ff83c66e 408bool timerlist_run_timers(QEMUTimerList *timer_list)
db1a4972 409{
144b97c2 410 QEMUTimer *ts;
db1a4972 411 int64_t current_time;
f9a976b7 412 bool progress = false;
978f2205
SH
413 QEMUTimerCB *cb;
414 void *opaque;
415
ff83c66e 416 if (!timer_list->clock->enabled) {
f9a976b7 417 return progress;
ff83c66e 418 }
db1a4972 419
40daca54 420 current_time = qemu_clock_get_ns(timer_list->clock->type);
db1a4972 421 for(;;) {
978f2205 422 qemu_mutex_lock(&timer_list->active_timers_lock);
ff83c66e 423 ts = timer_list->active_timers;
e93379b0 424 if (!timer_expired_ns(ts, current_time)) {
978f2205 425 qemu_mutex_unlock(&timer_list->active_timers_lock);
db1a4972 426 break;
45c7b37f 427 }
978f2205 428
db1a4972 429 /* remove timer from the list before calling the callback */
ff83c66e 430 timer_list->active_timers = ts->next;
db1a4972 431 ts->next = NULL;
978f2205
SH
432 cb = ts->cb;
433 opaque = ts->opaque;
434 qemu_mutex_unlock(&timer_list->active_timers_lock);
db1a4972
PB
435
436 /* run the callback (the timer list can be modified) */
978f2205 437 cb(opaque);
f9a976b7 438 progress = true;
db1a4972 439 }
f9a976b7 440 return progress;
db1a4972
PB
441}
442
40daca54
AB
443bool qemu_clock_run_timers(QEMUClockType type)
444{
7bf8fbde 445 return timerlist_run_timers(main_loop_tlg.tl[type]);
40daca54
AB
446}
447
d5541d86
AB
448void timerlistgroup_init(QEMUTimerListGroup *tlg,
449 QEMUTimerListNotifyCB *cb, void *opaque)
754d6a54
AB
450{
451 QEMUClockType type;
452 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
d5541d86 453 tlg->tl[type] = timerlist_new(type, cb, opaque);
754d6a54
AB
454 }
455}
456
457void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
458{
459 QEMUClockType type;
460 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
461 timerlist_free(tlg->tl[type]);
462 }
463}
464
465bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
466{
467 QEMUClockType type;
468 bool progress = false;
469 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
470 progress |= timerlist_run_timers(tlg->tl[type]);
471 }
472 return progress;
473}
474
475int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
476{
477 int64_t deadline = -1;
478 QEMUClockType type;
479 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
40daca54 480 if (qemu_clock_use_for_deadline(tlg->tl[type]->clock->type)) {
754d6a54
AB
481 deadline = qemu_soonest_timeout(deadline,
482 timerlist_deadline_ns(
483 tlg->tl[type]));
484 }
485 }
486 return deadline;
487}
488
40daca54 489int64_t qemu_clock_get_ns(QEMUClockType type)
db1a4972 490{
691a0c9c 491 int64_t now, last;
40daca54 492 QEMUClock *clock = qemu_clock_ptr(type);
691a0c9c 493
40daca54 494 switch (type) {
db1a4972
PB
495 case QEMU_CLOCK_REALTIME:
496 return get_clock();
497 default:
498 case QEMU_CLOCK_VIRTUAL:
499 if (use_icount) {
500 return cpu_get_icount();
501 } else {
502 return cpu_get_clock();
503 }
504 case QEMU_CLOCK_HOST:
691a0c9c
JK
505 now = get_clock_realtime();
506 last = clock->last;
507 clock->last = now;
508 if (now < last) {
509 notifier_list_notify(&clock->reset_notifiers, &now);
510 }
511 return now;
db1a4972
PB
512 }
513}
514
40daca54
AB
515void qemu_clock_register_reset_notifier(QEMUClockType type,
516 Notifier *notifier)
517{
518 QEMUClock *clock = qemu_clock_ptr(type);
691a0c9c
JK
519 notifier_list_add(&clock->reset_notifiers, notifier);
520}
521
40daca54
AB
522void qemu_clock_unregister_reset_notifier(QEMUClockType type,
523 Notifier *notifier)
691a0c9c 524{
31552529 525 notifier_remove(notifier);
691a0c9c
JK
526}
527
db1a4972
PB
528void init_clocks(void)
529{
ff83c66e
AB
530 QEMUClockType type;
531 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
7bf8fbde 532 qemu_clock_init(type);
744ca8e3 533 }
ff83c66e 534
cd758dd0
AB
535#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
536 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
537#endif
db1a4972
PB
538}
539
e93379b0 540uint64_t timer_expire_time_ns(QEMUTimer *ts)
db1a4972 541{
e93379b0 542 return timer_pending(ts) ? ts->expire_time : -1;
db1a4972
PB
543}
544
40daca54 545bool qemu_clock_run_all_timers(void)
db1a4972 546{
f9a976b7 547 bool progress = false;
ff83c66e 548 QEMUClockType type;
6d327171 549
ff83c66e 550 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
40daca54 551 progress |= qemu_clock_run_timers(type);
ff83c66e 552 }
158fd3ce 553
f9a976b7 554 return progress;
db1a4972 555}
This page took 0.505816 seconds and 4 git commands to generate.