110817bf0SDaniel P. Berrange /* 210817bf0SDaniel P. Berrange * Coroutine internals 310817bf0SDaniel P. Berrange * 410817bf0SDaniel P. Berrange * Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com> 510817bf0SDaniel P. Berrange * 610817bf0SDaniel P. Berrange * Permission is hereby granted, free of charge, to any person obtaining a copy 710817bf0SDaniel P. Berrange * of this software and associated documentation files (the "Software"), to deal 810817bf0SDaniel P. Berrange * in the Software without restriction, including without limitation the rights 910817bf0SDaniel P. Berrange * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1010817bf0SDaniel P. Berrange * copies of the Software, and to permit persons to whom the Software is 1110817bf0SDaniel P. Berrange * furnished to do so, subject to the following conditions: 1210817bf0SDaniel P. Berrange * 1310817bf0SDaniel P. Berrange * The above copyright notice and this permission notice shall be included in 1410817bf0SDaniel P. Berrange * all copies or substantial portions of the Software. 1510817bf0SDaniel P. Berrange * 1610817bf0SDaniel P. Berrange * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1710817bf0SDaniel P. Berrange * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1810817bf0SDaniel P. Berrange * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1910817bf0SDaniel P. Berrange * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2010817bf0SDaniel P. Berrange * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2110817bf0SDaniel P. Berrange * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2210817bf0SDaniel P. Berrange * THE SOFTWARE. 2310817bf0SDaniel P. Berrange */ 2410817bf0SDaniel P. Berrange 2510817bf0SDaniel P. Berrange #ifndef QEMU_COROUTINE_INT_H 2610817bf0SDaniel P. Berrange #define QEMU_COROUTINE_INT_H 2710817bf0SDaniel P. Berrange 2810817bf0SDaniel P. Berrange #include "qemu/queue.h" 2910817bf0SDaniel P. Berrange #include "qemu/coroutine.h" 3010817bf0SDaniel P. Berrange 31*58ebc2c3SDaniele Buono #ifdef CONFIG_SAFESTACK 32*58ebc2c3SDaniele Buono /* Pointer to the unsafe stack, defined by the compiler */ 33*58ebc2c3SDaniele Buono extern __thread void *__safestack_unsafe_stack_ptr; 34*58ebc2c3SDaniele Buono #endif 35*58ebc2c3SDaniele Buono 368adcd6fbSPeter Lieven #define COROUTINE_STACK_SIZE (1 << 20) 378adcd6fbSPeter Lieven 3810817bf0SDaniel P. Berrange typedef enum { 3910817bf0SDaniel P. Berrange COROUTINE_YIELD = 1, 4010817bf0SDaniel P. Berrange COROUTINE_TERMINATE = 2, 4110817bf0SDaniel P. Berrange COROUTINE_ENTER = 3, 4210817bf0SDaniel P. Berrange } CoroutineAction; 4310817bf0SDaniel P. Berrange 4410817bf0SDaniel P. Berrange struct Coroutine { 4510817bf0SDaniel P. Berrange CoroutineEntry *entry; 4610817bf0SDaniel P. Berrange void *entry_arg; 4710817bf0SDaniel P. Berrange Coroutine *caller; 480c330a73SPaolo Bonzini 490c330a73SPaolo Bonzini /* Only used when the coroutine has terminated. */ 5010817bf0SDaniel P. Berrange QSLIST_ENTRY(Coroutine) pool_next; 510c330a73SPaolo Bonzini 521b7f01d9SKevin Wolf size_t locks_held; 5310817bf0SDaniel P. Berrange 546133b39fSJeff Cody /* Only used when the coroutine has yielded. */ 556133b39fSJeff Cody AioContext *ctx; 566133b39fSJeff Cody 576133b39fSJeff Cody /* Used to catch and abort on illegal co-routine entry. 586133b39fSJeff Cody * Will contain the name of the function that had first 596133b39fSJeff Cody * scheduled the coroutine. */ 606133b39fSJeff Cody const char *scheduled; 616133b39fSJeff Cody 626133b39fSJeff Cody QSIMPLEQ_ENTRY(Coroutine) co_queue_next; 636133b39fSJeff Cody 640c330a73SPaolo Bonzini /* Coroutines that should be woken up when we yield or terminate. 650c330a73SPaolo Bonzini * Only used when the coroutine is running. 660c330a73SPaolo Bonzini */ 677d9c8581SPaolo Bonzini QSIMPLEQ_HEAD(, Coroutine) co_queue_wakeup; 680c330a73SPaolo Bonzini 690c330a73SPaolo Bonzini QSLIST_ENTRY(Coroutine) co_scheduled_next; 7010817bf0SDaniel P. Berrange }; 7110817bf0SDaniel P. Berrange 7210817bf0SDaniel P. Berrange Coroutine *qemu_coroutine_new(void); 7310817bf0SDaniel P. Berrange void qemu_coroutine_delete(Coroutine *co); 7410817bf0SDaniel P. Berrange CoroutineAction qemu_coroutine_switch(Coroutine *from, Coroutine *to, 7510817bf0SDaniel P. Berrange CoroutineAction action); 7610817bf0SDaniel P. Berrange 7710817bf0SDaniel P. Berrange #endif 78