2
0

qemu-coroutine-sleep.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_sleep_ns(QEMUClockType type, int64_t ns)
  26. {
  27. CoSleepCB sleep_cb = {
  28. .co = qemu_coroutine_self(),
  29. };
  30. sleep_cb.ts = timer_new(type, SCALE_NS, co_sleep_cb, &sleep_cb);
  31. timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns);
  32. qemu_coroutine_yield();
  33. timer_del(sleep_cb.ts);
  34. timer_free(sleep_cb.ts);
  35. }
  36. void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type,
  37. int64_t ns)
  38. {
  39. CoSleepCB sleep_cb = {
  40. .co = qemu_coroutine_self(),
  41. };
  42. sleep_cb.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &sleep_cb);
  43. timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns);
  44. qemu_coroutine_yield();
  45. timer_del(sleep_cb.ts);
  46. timer_free(sleep_cb.ts);
  47. }