2
0

qemu-coroutine.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /**
  19. * Coroutines are a mechanism for stack switching and can be used for
  20. * cooperative userspace threading. These functions provide a simple but
  21. * useful flavor of coroutines that is suitable for writing sequential code,
  22. * rather than callbacks, for operations that need to give up control while
  23. * waiting for events to complete.
  24. *
  25. * These functions are re-entrant and may be used outside the global mutex.
  26. */
  27. /**
  28. * Mark a function that executes in coroutine context
  29. *
  30. * Functions that execute in coroutine context cannot be called directly from
  31. * normal functions. In the future it would be nice to enable compiler or
  32. * static checker support for catching such errors. This annotation might make
  33. * it possible and in the meantime it serves as documentation.
  34. *
  35. * For example:
  36. *
  37. * static void coroutine_fn foo(void) {
  38. * ....
  39. * }
  40. */
  41. #define coroutine_fn
  42. typedef struct Coroutine Coroutine;
  43. /**
  44. * Coroutine entry point
  45. *
  46. * When the coroutine is entered for the first time, opaque is passed in as an
  47. * argument.
  48. *
  49. * When this function returns, the coroutine is destroyed automatically and
  50. * execution continues in the caller who last entered the coroutine.
  51. */
  52. typedef void coroutine_fn CoroutineEntry(void *opaque);
  53. /**
  54. * Create a new coroutine
  55. *
  56. * Use qemu_coroutine_enter() to actually transfer control to the coroutine.
  57. */
  58. Coroutine *qemu_coroutine_create(CoroutineEntry *entry);
  59. /**
  60. * Transfer control to a coroutine
  61. *
  62. * The opaque argument is passed as the argument to the entry point when
  63. * entering the coroutine for the first time. It is subsequently ignored.
  64. */
  65. void qemu_coroutine_enter(Coroutine *coroutine, void *opaque);
  66. /**
  67. * Transfer control back to a coroutine's caller
  68. *
  69. * This function does not return until the coroutine is re-entered using
  70. * qemu_coroutine_enter().
  71. */
  72. void coroutine_fn qemu_coroutine_yield(void);
  73. /**
  74. * Get the currently executing coroutine
  75. */
  76. Coroutine *coroutine_fn qemu_coroutine_self(void);
  77. /**
  78. * Return whether or not currently inside a coroutine
  79. *
  80. * This can be used to write functions that work both when in coroutine context
  81. * and when not in coroutine context. Note that such functions cannot use the
  82. * coroutine_fn annotation since they work outside coroutine context.
  83. */
  84. bool qemu_in_coroutine(void);
  85. /**
  86. * CoQueues are a mechanism to queue coroutines in order to continue executing
  87. * them later. They provide the fundamental primitives on which coroutine locks
  88. * are built.
  89. */
  90. typedef struct CoQueue {
  91. QTAILQ_HEAD(, Coroutine) entries;
  92. } CoQueue;
  93. /**
  94. * Initialise a CoQueue. This must be called before any other operation is used
  95. * on the CoQueue.
  96. */
  97. void qemu_co_queue_init(CoQueue *queue);
  98. /**
  99. * Adds the current coroutine to the CoQueue and transfers control to the
  100. * caller of the coroutine.
  101. */
  102. void coroutine_fn qemu_co_queue_wait(CoQueue *queue);
  103. /**
  104. * Restarts the next coroutine in the CoQueue and removes it from the queue.
  105. *
  106. * Returns true if a coroutine was restarted, false if the queue is empty.
  107. */
  108. bool qemu_co_queue_next(CoQueue *queue);
  109. /**
  110. * Checks if the CoQueue is empty.
  111. */
  112. bool qemu_co_queue_empty(CoQueue *queue);
  113. /**
  114. * Provides a mutex that can be used to synchronise coroutines
  115. */
  116. typedef struct CoMutex {
  117. bool locked;
  118. CoQueue queue;
  119. } CoMutex;
  120. /**
  121. * Initialises a CoMutex. This must be called before any other operation is used
  122. * on the CoMutex.
  123. */
  124. void qemu_co_mutex_init(CoMutex *mutex);
  125. /**
  126. * Locks the mutex. If the lock cannot be taken immediately, control is
  127. * transferred to the caller of the current coroutine.
  128. */
  129. void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex);
  130. /**
  131. * Unlocks the mutex and schedules the next coroutine that was waiting for this
  132. * lock to be run.
  133. */
  134. void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex);
  135. typedef struct CoRwlock {
  136. bool writer;
  137. int reader;
  138. CoQueue queue;
  139. } CoRwlock;
  140. /**
  141. * Initialises a CoRwlock. This must be called before any other operation
  142. * is used on the CoRwlock
  143. */
  144. void qemu_co_rwlock_init(CoRwlock *lock);
  145. /**
  146. * Read locks the CoRwlock. If the lock cannot be taken immediately because
  147. * of a parallel writer, control is transferred to the caller of the current
  148. * coroutine.
  149. */
  150. void qemu_co_rwlock_rdlock(CoRwlock *lock);
  151. /**
  152. * Write Locks the mutex. If the lock cannot be taken immediately because
  153. * of a parallel reader, control is transferred to the caller of the current
  154. * coroutine.
  155. */
  156. void qemu_co_rwlock_wrlock(CoRwlock *lock);
  157. /**
  158. * Unlocks the read/write lock and schedules the next coroutine that was
  159. * waiting for this lock to be run.
  160. */
  161. void qemu_co_rwlock_unlock(CoRwlock *lock);
  162. #endif /* QEMU_COROUTINE_H */