[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Any suggestion for a robust C coroutine library which can be used with LuaJIT ?
- From: Tony Finch <dot@...>
- Date: Thu, 12 Jan 2012 20:03:32 +0000
David Given <firstname.lastname@example.org> wrote:
> A while back I wrote a coroutine library using
> setcontext()/getcontext(), for a project I was doing in C++ (which, it
> turns out, would have been much easier in Lua, but that's beside the point).
> The lesson I learned from this is: don't use setcontext()/getcontext().
> Some Linux libc versions do not allow you to use pthreads functions from
> stacks that were not allocated through pthreads. (Because they use magic
> numbers at the top of the stack to identify which thread is running.)
> This means that if you call a function in any library which has been
> compiled with pthreads support, like malloc(), really bad things happen.
That's pretty sad.
GNU Pth is a co-operative threading library designed to look a bit like
POSIX threads. I don't know what primitives it uses.
If you know which direction your stack grows in, you can implement
coroutines with setjmp+longjmp (to switch contexts) and either alloca() or
C99 dynamic arrays (to perform arithmetic on the stack pointer when
creating a new context). This is a fun low-level hack rather than
something you should use in real life...
f.anthony.n.finch <email@example.com> http://dotat.at/
Sole: Variable 3 or 4, becoming southeasterly 4 or 5 later in west. Moderate.
Showers. Moderate or good.