2
0

qemu-coroutine.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * QEMU coroutine implementation
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  8. * Kevin Wolf <kwolf@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  11. * See the COPYING.LIB file in the top-level directory.
  12. *
  13. */
  14. #ifndef QEMU_COROUTINE_H
  15. #define QEMU_COROUTINE_H
  16. #include <stdbool.h>
  17. #include "qemu-queue.h"
  18. #include "qemu-timer.h"
  19. /**
  20. * Coroutines are a mechanism for stack switching and can be used for
  21. * cooperative userspace threading. These functions provide a simple but
  22. * useful flavor of coroutines that is suitable for writing sequential code,
  23. * rather than callbacks, for operations that need to give up control while
  24. * waiting for events to complete.
  25. *
  26. * These functions are re-entrant and may be used outside the global mutex.
  27. */
  28. /**
  29. * Mark a function that executes in coroutine context
  30. *
  31. * Functions that execute in coroutine context cannot be called directly from
  32. * normal functions. In the future it would be nice to enable compiler or
  33. * static checker support for catching such errors. This annotation might make
  34. * it possible and in the meantime it serves as documentation.
  35. *
  36. * For example:
  37. *
  38. * static void coroutine_fn foo(void) {
  39. * ....
  40. * }
  41. */
  42. #define coroutine_fn
  43. typedef struct Coroutine Coroutine;
  44. /**
  45. * Coroutine entry point
  46. *
  47. * When the coroutine is entered for the first time, opaque is passed in as an
  48. * argument.
  49. *
  50. * When this function returns, the coroutine is destroyed automatically and
  51. * execution continues in the caller who last entered the coroutine.
  52. */
  53. typedef void coroutine_fn CoroutineEntry(void *opaque);
  54. /**
  55. * Create a new coroutine
  56. *
  57. * Use qemu_coroutine_enter() to actually transfer control to the coroutine.
  58. */
  59. Coroutine *qemu_coroutine_create(CoroutineEntry *entry);
  60. /**
  61. * Transfer control to a coroutine
  62. *
  63. * The opaque argument is passed as the argument to the entry point when
  64. * entering the coroutine for the first time. It is subsequently ignored.
  65. */
  66. void qemu_coroutine_enter(Coroutine *coroutine, void *opaque);
  67. /**
  68. * Transfer control back to a coroutine's caller
  69. *
  70. * This function does not return until the coroutine is re-entered using
  71. * qemu_coroutine_enter().
  72. */
  73. void coroutine_fn qemu_coroutine_yield(void);
  74. /**
  75. * Get the currently executing coroutine
  76. */
  77. Coroutine *coroutine_fn qemu_coroutine_self(void);
  78. /**
  79. * Return whether or not currently inside a coroutine
  80. *
  81. * This can be used to write functions that work both when in coroutine context
  82. * and when not in coroutine context. Note that such functions cannot use the
  83. * coroutine_fn annotation since they work outside coroutine context.
  84. */
  85. bool qemu_in_coroutine(void);
  86. /**
  87. * CoQueues are a mechanism to queue coroutines in order to continue executing
  88. * them later. They provide the fundamental primitives on which coroutine locks
  89. * are built.
  90. */
  91. typedef struct CoQueue {
  92. QTAILQ_HEAD(, Coroutine) entries;
  93. } CoQueue;
  94. /**
  95. * Initialise a CoQueue. This must be called before any other operation is used
  96. * on the CoQueue.
  97. */
  98. void qemu_co_queue_init(CoQueue *queue);
  99. /**
  100. * Adds the current coroutine to the CoQueue and transfers control to the
  101. * caller of the coroutine.
  102. */
  103. void coroutine_fn qemu_co_queue_wait(CoQueue *queue);
  104. /**
  105. * Adds the current coroutine to the head of the CoQueue and transfers control to the
  106. * caller of the coroutine.
  107. */
  108. void coroutine_fn qemu_co_queue_wait_insert_head(CoQueue *queue);
  109. /**
  110. * Restarts the next coroutine in the CoQueue and removes it from the queue.
  111. *
  112. * Returns true if a coroutine was restarted, false if the queue is empty.
  113. */
  114. bool qemu_co_queue_next(CoQueue *queue);
  115. /**
  116. * Restarts all coroutines in the CoQueue and leaves the queue empty.
  117. */
  118. void qemu_co_queue_restart_all(CoQueue *queue);
  119. /**
  120. * Checks if the CoQueue is empty.
  121. */
  122. bool qemu_co_queue_empty(CoQueue *queue);
  123. /**
  124. * Provides a mutex that can be used to synchronise coroutines
  125. */
  126. typedef struct CoMutex {
  127. bool locked;
  128. CoQueue queue;
  129. } CoMutex;
  130. /**
  131. * Initialises a CoMutex. This must be called before any other operation is used
  132. * on the CoMutex.
  133. */
  134. void qemu_co_mutex_init(CoMutex *mutex);
  135. /**
  136. * Locks the mutex. If the lock cannot be taken immediately, control is
  137. * transferred to the caller of the current coroutine.
  138. */
  139. void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex);
  140. /**
  141. * Unlocks the mutex and schedules the next coroutine that was waiting for this
  142. * lock to be run.
  143. */
  144. void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex);
  145. typedef struct CoRwlock {
  146. bool writer;
  147. int reader;
  148. CoQueue queue;
  149. } CoRwlock;
  150. /**
  151. * Initialises a CoRwlock. This must be called before any other operation
  152. * is used on the CoRwlock
  153. */
  154. void qemu_co_rwlock_init(CoRwlock *lock);
  155. /**
  156. * Read locks the CoRwlock. If the lock cannot be taken immediately because
  157. * of a parallel writer, control is transferred to the caller of the current
  158. * coroutine.
  159. */
  160. void qemu_co_rwlock_rdlock(CoRwlock *lock);
  161. /**
  162. * Write Locks the mutex. If the lock cannot be taken immediately because
  163. * of a parallel reader, control is transferred to the caller of the current
  164. * coroutine.
  165. */
  166. void qemu_co_rwlock_wrlock(CoRwlock *lock);
  167. /**
  168. * Unlocks the read/write lock and schedules the next coroutine that was
  169. * waiting for this lock to be run.
  170. */
  171. void qemu_co_rwlock_unlock(CoRwlock *lock);
  172. /**
  173. * Yield the coroutine for a given duration
  174. *
  175. * Note this function uses timers and hence only works when a main loop is in
  176. * use. See main-loop.h and do not use from qemu-tool programs.
  177. */
  178. void coroutine_fn co_sleep_ns(QEMUClock *clock, int64_t ns);
  179. #endif /* QEMU_COROUTINE_H */