coroutine-ucontext.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * ucontext coroutine initialization code
  3. *
  4. * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
  5. * Copyright (C) 2011 Kevin Wolf <kwolf@redhat.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. /* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
  21. #ifdef _FORTIFY_SOURCE
  22. #undef _FORTIFY_SOURCE
  23. #endif
  24. #include <stdlib.h>
  25. #include <setjmp.h>
  26. #include <stdint.h>
  27. #include <pthread.h>
  28. #include <ucontext.h>
  29. #include "qemu-common.h"
  30. #include "qemu-coroutine-int.h"
  31. enum {
  32. /* Maximum free pool size prevents holding too many freed coroutines */
  33. POOL_MAX_SIZE = 64,
  34. };
  35. /** Free list to speed up creation */
  36. static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
  37. static unsigned int pool_size;
  38. typedef struct {
  39. Coroutine base;
  40. void *stack;
  41. jmp_buf env;
  42. } CoroutineUContext;
  43. /**
  44. * Per-thread coroutine bookkeeping
  45. */
  46. typedef struct {
  47. /** Currently executing coroutine */
  48. Coroutine *current;
  49. /** The default coroutine */
  50. CoroutineUContext leader;
  51. } CoroutineThreadState;
  52. static pthread_key_t thread_state_key;
  53. /*
  54. * va_args to makecontext() must be type 'int', so passing
  55. * the pointer we need may require several int args. This
  56. * union is a quick hack to let us do that
  57. */
  58. union cc_arg {
  59. void *p;
  60. int i[2];
  61. };
  62. static CoroutineThreadState *coroutine_get_thread_state(void)
  63. {
  64. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  65. if (!s) {
  66. s = g_malloc0(sizeof(*s));
  67. s->current = &s->leader.base;
  68. pthread_setspecific(thread_state_key, s);
  69. }
  70. return s;
  71. }
  72. static void qemu_coroutine_thread_cleanup(void *opaque)
  73. {
  74. CoroutineThreadState *s = opaque;
  75. g_free(s);
  76. }
  77. static void __attribute__((destructor)) coroutine_cleanup(void)
  78. {
  79. Coroutine *co;
  80. Coroutine *tmp;
  81. QSLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) {
  82. g_free(DO_UPCAST(CoroutineUContext, base, co)->stack);
  83. g_free(co);
  84. }
  85. }
  86. static void __attribute__((constructor)) coroutine_init(void)
  87. {
  88. int ret;
  89. ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
  90. if (ret != 0) {
  91. fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
  92. abort();
  93. }
  94. }
  95. static void coroutine_trampoline(int i0, int i1)
  96. {
  97. union cc_arg arg;
  98. CoroutineUContext *self;
  99. Coroutine *co;
  100. arg.i[0] = i0;
  101. arg.i[1] = i1;
  102. self = arg.p;
  103. co = &self->base;
  104. /* Initialize longjmp environment and switch back the caller */
  105. if (!setjmp(self->env)) {
  106. longjmp(*(jmp_buf *)co->entry_arg, 1);
  107. }
  108. while (true) {
  109. co->entry(co->entry_arg);
  110. qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
  111. }
  112. }
  113. static Coroutine *coroutine_new(void)
  114. {
  115. const size_t stack_size = 1 << 20;
  116. CoroutineUContext *co;
  117. ucontext_t old_uc, uc;
  118. jmp_buf old_env;
  119. union cc_arg arg = {0};
  120. /* The ucontext functions preserve signal masks which incurs a system call
  121. * overhead. setjmp()/longjmp() does not preserve signal masks but only
  122. * works on the current stack. Since we need a way to create and switch to
  123. * a new stack, use the ucontext functions for that but setjmp()/longjmp()
  124. * for everything else.
  125. */
  126. if (getcontext(&uc) == -1) {
  127. abort();
  128. }
  129. co = g_malloc0(sizeof(*co));
  130. co->stack = g_malloc(stack_size);
  131. co->base.entry_arg = &old_env; /* stash away our jmp_buf */
  132. uc.uc_link = &old_uc;
  133. uc.uc_stack.ss_sp = co->stack;
  134. uc.uc_stack.ss_size = stack_size;
  135. uc.uc_stack.ss_flags = 0;
  136. arg.p = co;
  137. makecontext(&uc, (void (*)(void))coroutine_trampoline,
  138. 2, arg.i[0], arg.i[1]);
  139. /* swapcontext() in, longjmp() back out */
  140. if (!setjmp(old_env)) {
  141. swapcontext(&old_uc, &uc);
  142. }
  143. return &co->base;
  144. }
  145. Coroutine *qemu_coroutine_new(void)
  146. {
  147. Coroutine *co;
  148. co = QSLIST_FIRST(&pool);
  149. if (co) {
  150. QSLIST_REMOVE_HEAD(&pool, pool_next);
  151. pool_size--;
  152. } else {
  153. co = coroutine_new();
  154. }
  155. return co;
  156. }
  157. void qemu_coroutine_delete(Coroutine *co_)
  158. {
  159. CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
  160. if (pool_size < POOL_MAX_SIZE) {
  161. QSLIST_INSERT_HEAD(&pool, &co->base, pool_next);
  162. co->base.caller = NULL;
  163. pool_size++;
  164. return;
  165. }
  166. g_free(co->stack);
  167. g_free(co);
  168. }
  169. CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
  170. CoroutineAction action)
  171. {
  172. CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
  173. CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
  174. CoroutineThreadState *s = coroutine_get_thread_state();
  175. int ret;
  176. s->current = to_;
  177. ret = setjmp(from->env);
  178. if (ret == 0) {
  179. longjmp(to->env, action);
  180. }
  181. return ret;
  182. }
  183. Coroutine *qemu_coroutine_self(void)
  184. {
  185. CoroutineThreadState *s = coroutine_get_thread_state();
  186. return s->current;
  187. }
  188. bool qemu_in_coroutine(void)
  189. {
  190. CoroutineThreadState *s = pthread_getspecific(thread_state_key);
  191. return s && s->current->caller;
  192. }