2
0

qemu-coroutine-sleep.c 898 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * QEMU coroutine sleep
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. *
  12. */
  13. #include "block/coroutine.h"
  14. #include "qemu/timer.h"
  15. typedef struct CoSleepCB {
  16. QEMUTimer *ts;
  17. Coroutine *co;
  18. } CoSleepCB;
  19. static void co_sleep_cb(void *opaque)
  20. {
  21. CoSleepCB *sleep_cb = opaque;
  22. qemu_coroutine_enter(sleep_cb->co, NULL);
  23. }
  24. void coroutine_fn co_sleep_ns(QEMUClock *clock, int64_t ns)
  25. {
  26. CoSleepCB sleep_cb = {
  27. .co = qemu_coroutine_self(),
  28. };
  29. sleep_cb.ts = qemu_new_timer(clock, SCALE_NS, co_sleep_cb, &sleep_cb);
  30. qemu_mod_timer(sleep_cb.ts, qemu_get_clock_ns(clock) + ns);
  31. qemu_coroutine_yield();
  32. qemu_del_timer(sleep_cb.ts);
  33. qemu_free_timer(sleep_cb.ts);
  34. }