coroutine-gthread.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * GThread coroutine initialization code
  3. *
  4. * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
  5. * Copyright (C) 2011 Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.0 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <glib.h>
  21. #include "qemu-common.h"
  22. #include "block/coroutine_int.h"
  23. typedef struct {
  24. Coroutine base;
  25. GThread *thread;
  26. bool runnable;
  27. bool free_on_thread_exit;
  28. CoroutineAction action;
  29. } CoroutineGThread;
  30. static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT;
  31. /* GLib 2.31 and beyond deprecated various parts of the thread API,
  32. * but the new interfaces are not available in older GLib versions
  33. * so we have to cope with both.
  34. */
  35. #if GLIB_CHECK_VERSION(2, 31, 0)
  36. /* Default zero-initialisation is sufficient for 2.31+ GCond */
  37. static GCond the_coroutine_cond;
  38. static GCond *coroutine_cond = &the_coroutine_cond;
  39. static inline void init_coroutine_cond(void)
  40. {
  41. }
  42. /* Awkwardly, the GPrivate API doesn't provide a way to update the
  43. * GDestroyNotify handler for the coroutine key dynamically. So instead
  44. * we track whether or not the CoroutineGThread should be freed on
  45. * thread exit / coroutine key update using the free_on_thread_exit
  46. * field.
  47. */
  48. static void coroutine_destroy_notify(gpointer data)
  49. {
  50. CoroutineGThread *co = data;
  51. if (co && co->free_on_thread_exit) {
  52. g_free(co);
  53. }
  54. }
  55. static GPrivate coroutine_key = G_PRIVATE_INIT(coroutine_destroy_notify);
  56. static inline CoroutineGThread *get_coroutine_key(void)
  57. {
  58. return g_private_get(&coroutine_key);
  59. }
  60. static inline void set_coroutine_key(CoroutineGThread *co,
  61. bool free_on_thread_exit)
  62. {
  63. /* Unlike g_static_private_set() this does not call the GDestroyNotify
  64. * if the previous value of the key was NULL. Fortunately we only need
  65. * the GDestroyNotify in the non-NULL key case.
  66. */
  67. co->free_on_thread_exit = free_on_thread_exit;
  68. g_private_replace(&coroutine_key, co);
  69. }
  70. static inline GThread *create_thread(GThreadFunc func, gpointer data)
  71. {
  72. return g_thread_new("coroutine", func, data);
  73. }
  74. #else
  75. /* Handle older GLib versions */
  76. static GCond *coroutine_cond;
  77. static inline void init_coroutine_cond(void)
  78. {
  79. coroutine_cond = g_cond_new();
  80. }
  81. static GStaticPrivate coroutine_key = G_STATIC_PRIVATE_INIT;
  82. static inline CoroutineGThread *get_coroutine_key(void)
  83. {
  84. return g_static_private_get(&coroutine_key);
  85. }
  86. static inline void set_coroutine_key(CoroutineGThread *co,
  87. bool free_on_thread_exit)
  88. {
  89. g_static_private_set(&coroutine_key, co,
  90. free_on_thread_exit ? (GDestroyNotify)g_free : NULL);
  91. }
  92. static inline GThread *create_thread(GThreadFunc func, gpointer data)
  93. {
  94. return g_thread_create_full(func, data, 0, TRUE, TRUE,
  95. G_THREAD_PRIORITY_NORMAL, NULL);
  96. }
  97. #endif
  98. static void __attribute__((constructor)) coroutine_init(void)
  99. {
  100. if (!g_thread_supported()) {
  101. #if !GLIB_CHECK_VERSION(2, 31, 0)
  102. g_thread_init(NULL);
  103. #else
  104. fprintf(stderr, "glib threading failed to initialize.\n");
  105. exit(1);
  106. #endif
  107. }
  108. init_coroutine_cond();
  109. }
  110. static void coroutine_wait_runnable_locked(CoroutineGThread *co)
  111. {
  112. while (!co->runnable) {
  113. g_cond_wait(coroutine_cond, g_static_mutex_get_mutex(&coroutine_lock));
  114. }
  115. }
  116. static void coroutine_wait_runnable(CoroutineGThread *co)
  117. {
  118. g_static_mutex_lock(&coroutine_lock);
  119. coroutine_wait_runnable_locked(co);
  120. g_static_mutex_unlock(&coroutine_lock);
  121. }
  122. static gpointer coroutine_thread(gpointer opaque)
  123. {
  124. CoroutineGThread *co = opaque;
  125. set_coroutine_key(co, false);
  126. coroutine_wait_runnable(co);
  127. co->base.entry(co->base.entry_arg);
  128. qemu_coroutine_switch(&co->base, co->base.caller, COROUTINE_TERMINATE);
  129. return NULL;
  130. }
  131. Coroutine *qemu_coroutine_new(void)
  132. {
  133. CoroutineGThread *co;
  134. co = g_malloc0(sizeof(*co));
  135. co->thread = create_thread(coroutine_thread, co);
  136. if (!co->thread) {
  137. g_free(co);
  138. return NULL;
  139. }
  140. return &co->base;
  141. }
  142. void qemu_coroutine_delete(Coroutine *co_)
  143. {
  144. CoroutineGThread *co = DO_UPCAST(CoroutineGThread, base, co_);
  145. g_thread_join(co->thread);
  146. g_free(co);
  147. }
  148. CoroutineAction qemu_coroutine_switch(Coroutine *from_,
  149. Coroutine *to_,
  150. CoroutineAction action)
  151. {
  152. CoroutineGThread *from = DO_UPCAST(CoroutineGThread, base, from_);
  153. CoroutineGThread *to = DO_UPCAST(CoroutineGThread, base, to_);
  154. g_static_mutex_lock(&coroutine_lock);
  155. from->runnable = false;
  156. from->action = action;
  157. to->runnable = true;
  158. to->action = action;
  159. g_cond_broadcast(coroutine_cond);
  160. if (action != COROUTINE_TERMINATE) {
  161. coroutine_wait_runnable_locked(from);
  162. }
  163. g_static_mutex_unlock(&coroutine_lock);
  164. return from->action;
  165. }
  166. Coroutine *qemu_coroutine_self(void)
  167. {
  168. CoroutineGThread *co = get_coroutine_key();
  169. if (!co) {
  170. co = g_malloc0(sizeof(*co));
  171. co->runnable = true;
  172. set_coroutine_key(co, true);
  173. }
  174. return &co->base;
  175. }
  176. bool qemu_in_coroutine(void)
  177. {
  178. CoroutineGThread *co = get_coroutine_key();
  179. return co && co->base.caller;
  180. }