qemu-coroutine-sleep.c 988 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 "qemu/osdep.h"
  14. #include "qemu/coroutine.h"
  15. #include "qemu/timer.h"
  16. #include "block/aio.h"
  17. typedef struct CoSleepCB {
  18. QEMUTimer *ts;
  19. Coroutine *co;
  20. } CoSleepCB;
  21. static void co_sleep_cb(void *opaque)
  22. {
  23. CoSleepCB *sleep_cb = opaque;
  24. qemu_coroutine_enter(sleep_cb->co, NULL);
  25. }
  26. void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type,
  27. int64_t ns)
  28. {
  29. CoSleepCB sleep_cb = {
  30. .co = qemu_coroutine_self(),
  31. };
  32. sleep_cb.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &sleep_cb);
  33. timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns);
  34. qemu_coroutine_yield();
  35. timer_del(sleep_cb.ts);
  36. timer_free(sleep_cb.ts);
  37. }