|
From: | Anthony Liguori |
Subject: | Re: [Qemu-devel] [RFC][PATCH 01/12] coroutine: Add gtk-vnc coroutines library |
Date: | Wed, 26 Jan 2011 10:19:41 -0600 |
User-agent: | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.15) Gecko/20101027 Lightning/1.0b1 Thunderbird/3.0.10 |
On 01/26/2011 10:13 AM, Avi Kivity wrote:
On 01/26/2011 06:00 PM, Anthony Liguori wrote:On 01/26/2011 09:25 AM, Avi Kivity wrote:On 01/22/2011 11:29 AM, Stefan Hajnoczi wrote:Asynchronous image format code is becoming very complex. Let's try using coroutines to write sequential code without callbacks but use coroutines to switch stacks under the hood. + +int cc_swap(struct continuation *from, struct continuation *to) +{ + to->exited = 0; + if (getcontext(&to->last) == -1) + return -1; + else if (to->exited == 0) + to->exited = 1; + else if (to->exited == 1) + return 1; + + return swapcontext(&from->uc,&to->uc); +}swapcontext() is very slow, involving the fpu and a syscall.A nice trick I've used in the past is to use getcontext/makecontext for the initial setup and setjmp/longjmp for switching. Of course this can be done later, as an optimization.Yeah, there are further optimizations that can be had but really, it's not important here. In fact, I'd rather start with the threaded version just because it's far more portable than ucontext.What do you mean by threaded version?
Stefan didn't post it, but the original code also has a GThread based implementation when ucontext isn't available (like on Windows). It uses a mutex to control the execution of the coroutines.
Regards, Anthony Liguori
[Prev in Thread] | Current Thread | [Next in Thread] |