From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:48376) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPGPJ-00068Y-DL for qemu-devel@nongnu.org; Wed, 25 May 2011 11:54:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QPGPH-0006XL-Cf for qemu-devel@nongnu.org; Wed, 25 May 2011 11:54:01 -0400 Received: from mail-yw0-f45.google.com ([209.85.213.45]:42391) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPGPH-0006Ww-7K for qemu-devel@nongnu.org; Wed, 25 May 2011 11:53:59 -0400 Received: by ywl41 with SMTP id 41so3713108ywl.4 for ; Wed, 25 May 2011 08:53:58 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20110524205106.GS969@shareable.org> References: <1305108925-26048-1-git-send-email-stefanha@linux.vnet.ibm.com> <1305108925-26048-2-git-send-email-stefanha@linux.vnet.ibm.com> <4DCA7B64.7000900@redhat.com> <4DCA8655.3070807@codemonkey.ws> <4DCA86A1.2020306@redhat.com> <4DCA89A9.8070000@us.ibm.com> <4DCA9303.5040400@redhat.com> <20110511135154.GU2661@redhat.com> <20110524193750.GQ969@shareable.org> <20110524195812.GA13211@stefanha-thinkpad.localdomain> <20110524205106.GS969@shareable.org> Date: Wed, 25 May 2011 08:09:02 +0100 Message-ID: From: Stefan Hajnoczi Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jamie Lokier Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi , qemu-devel@nongnu.org, Paolo Bonzini , Venkateswararao Jujjuri On Tue, May 24, 2011 at 9:51 PM, Jamie Lokier wrote: > Stefan Hajnoczi wrote: >> My current plan is to try using sigaltstack(2) instead of >> makecontext()/swapcontext() as a hack since OpenBSD doesn't have >> makecontext()/swapcontext(). > > sigaltstack() is just a system call to tell the system about an > alternative signal stack - that you have allocated yourself using > malloc(). =A0According to 'info libc "Signal Stack"'. =A0It won't help yo= u > get a new stack by itself. Issue sigaltstack() with the malloced new stack. Send yourself a signal and in a custom signal handler setjmp() to stash away the state (you're now on the new stack). > > Maybe take a look at what GNU Pth does. =A0It has a similar matrix of > tested platforms using different strategies on each, though it is > slightly different because it obviously doesn't link with > libpthread.so (it provides it!), and it has to context switch from the > SIGALRM handler for pre-emption. > >> TBH I'm almost at the stage where I think we should just use threads >> and/or async callbacks, as appropriate. =A0Hopefully I'll be able to coo= k >> up a reasonably portable implementation of coroutines though, because >> the prospect of having to go fully threaded or do async callbacks isn't >> attractive in many cases. > > Another classic trick is just to call a function recursively which has > a large local array(*), setjmp() every M calls, and longjmp() back to > the start after M*N calls. =A0That gets you N setjmp() contexts to > switch between, all in the same larger stack so it's fine even with > old pthread implementations, providing the total stack used isn't too > big, and the individual stacks you've allocated aren't too small for > the program. True, I think I've done something like this with alloca() before :(. It's extremely hacky though. Stefan