coroutine-gthread.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "qemu-coroutine-int.h"
  23. typedef struct {
  24. Coroutine base;
  25. GThread *thread;
  26. bool runnable;
  27. CoroutineAction action;
  28. } CoroutineGThread;
  29. static GCond *coroutine_cond;
  30. static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT;
  31. static GStaticPrivate coroutine_key = G_STATIC_PRIVATE_INIT;
  32. static void __attribute__((constructor)) coroutine_init(void)
  33. {
  34. if (!g_thread_supported()) {
  35. g_thread_init(NULL);
  36. }
  37. coroutine_cond = g_cond_new();
  38. }
  39. static void coroutine_wait_runnable_locked(CoroutineGThread *co)
  40. {
  41. while (!co->runnable) {
  42. g_cond_wait(coroutine_cond, g_static_mutex_get_mutex(&coroutine_lock));
  43. }
  44. }
  45. static void coroutine_wait_runnable(CoroutineGThread *co)
  46. {
  47. g_static_mutex_lock(&coroutine_lock);
  48. coroutine_wait_runnable_locked(co);
  49. g_static_mutex_unlock(&coroutine_lock);
  50. }
  51. static gpointer coroutine_thread(gpointer opaque)
  52. {
  53. CoroutineGThread *co = opaque;
  54. g_static_private_set(&coroutine_key, co, NULL);
  55. coroutine_wait_runnable(co);
  56. co->base.entry(co->base.entry_arg);
  57. qemu_coroutine_switch(&co->base, co->base.caller, COROUTINE_TERMINATE);
  58. return NULL;
  59. }
  60. Coroutine *qemu_coroutine_new(void)
  61. {
  62. CoroutineGThread *co;
  63. co = g_malloc0(sizeof(*co));
  64. co->thread = g_thread_create_full(coroutine_thread, co, 0, TRUE, TRUE,
  65. G_THREAD_PRIORITY_NORMAL, NULL);
  66. if (!co->thread) {
  67. g_free(co);
  68. return NULL;
  69. }
  70. return &co->base;
  71. }
  72. void qemu_coroutine_delete(Coroutine *co_)
  73. {
  74. CoroutineGThread *co = DO_UPCAST(CoroutineGThread, base, co_);
  75. g_thread_join(co->thread);
  76. g_free(co);
  77. }
  78. CoroutineAction qemu_coroutine_switch(Coroutine *from_,
  79. Coroutine *to_,
  80. CoroutineAction action)
  81. {
  82. CoroutineGThread *from = DO_UPCAST(CoroutineGThread, base, from_);
  83. CoroutineGThread *to = DO_UPCAST(CoroutineGThread, base, to_);
  84. g_static_mutex_lock(&coroutine_lock);
  85. from->runnable = false;
  86. from->action = action;
  87. to->runnable = true;
  88. to->action = action;
  89. g_cond_broadcast(coroutine_cond);
  90. if (action != COROUTINE_TERMINATE) {
  91. coroutine_wait_runnable_locked(from);
  92. }
  93. g_static_mutex_unlock(&coroutine_lock);
  94. return from->action;
  95. }
  96. Coroutine *qemu_coroutine_self(void)
  97. {
  98. CoroutineGThread *co = g_static_private_get(&coroutine_key);
  99. if (!co) {
  100. co = g_malloc0(sizeof(*co));
  101. co->runnable = true;
  102. g_static_private_set(&coroutine_key, co, (GDestroyNotify)g_free);
  103. }
  104. return &co->base;
  105. }
  106. bool qemu_in_coroutine(void)
  107. {
  108. CoroutineGThread *co = g_static_private_get(&coroutine_key);
  109. return co && co->base.caller;
  110. }