qemu-coroutine-sleep.c 965 B

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