All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jamie Lokier <jamie@shareable.org>
To: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>,
	Venkateswararao Jujjuri <jvrao@linux.vnet.ibm.com>
Subject: Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Date: Tue, 24 May 2011 21:51:06 +0100	[thread overview]
Message-ID: <20110524205106.GS969@shareable.org> (raw)
In-Reply-To: <20110524195812.GA13211@stefanha-thinkpad.localdomain>

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().  According to 'info libc "Signal Stack"'.  It won't help you
get a new stack by itself.

Maybe take a look at what GNU Pth does.  It 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.  Hopefully I'll be able to cook
> 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.  That 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.

If the large local array insists on being optimised away, it's
probably better anyway to track the address of a local variable, and
split the stack whenever the address has changed by enough.  Try to
make sure the compiler doesn't optimise away the tail recursion :-)

It works better on non-threaded programs as per-thread stacks are more
likely to have limited size.  *But* the initial thread often has a
large growable stack, just like a single-threaded program.  So it's a
good idea to do the stack carving in the initial thread (doesn't
necessarily have to be at the start of the program).  You may be able
to add guard pages afterwards with mprotect() if you're paranoid :-)

-- Jamie

  reply	other threads:[~2011-05-24 20:51 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-11 10:15 [Qemu-devel] [PATCH 0/2] Coroutines for better asynchronous programming Stefan Hajnoczi
2011-05-11 10:15 ` [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines Stefan Hajnoczi
2011-05-11 11:20   ` Kevin Wolf
2011-05-11 12:04   ` Paolo Bonzini
2011-05-11 12:15     ` Kevin Wolf
2011-05-11 12:51     ` Anthony Liguori
2011-05-11 12:52       ` Paolo Bonzini
2011-05-11 13:05         ` Anthony Liguori
2011-05-11 13:45           ` Paolo Bonzini
2011-05-11 13:51             ` Daniel P. Berrange
2011-05-24 19:37               ` Jamie Lokier
2011-05-24 19:58                 ` Stefan Hajnoczi
2011-05-24 20:51                   ` Jamie Lokier [this message]
2011-05-25  7:09                     ` Stefan Hajnoczi
2011-05-25 18:54                       ` Richard Henderson
2011-05-24 21:21                   ` Anthony Liguori
2011-05-25  7:32                     ` Paolo Bonzini
2011-05-24 21:22                   ` Anthony Liguori
2011-05-25 11:43                   ` Bastien ROUCARIES
2011-05-11 12:36   ` Anthony Liguori
2011-05-11 12:46     ` Paolo Bonzini
2011-05-11 12:54       ` Stefan Hajnoczi
2011-05-11 13:08     ` Kevin Wolf
2011-05-11 19:12   ` Stefan Weil
2011-05-12  7:59     ` Kevin Wolf
2011-05-12  9:51   ` Jan Kiszka
2011-05-12  9:59     ` Stefan Hajnoczi
2011-05-24 19:54       ` Jamie Lokier
2011-05-12 10:02     ` Kevin Wolf
2011-05-11 10:15 ` [Qemu-devel] [PATCH 2/2] coroutine: add check-coroutine automated tests Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110524205106.GS969@shareable.org \
    --to=jamie@shareable.org \
    --cc=aliguori@us.ibm.com \
    --cc=jvrao@linux.vnet.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.