Skip to content

Commit

Permalink
Use libcoro to get away from ucontext for Lion
Browse files Browse the repository at this point in the history
ucontext in Lion is straight up buggy. These functions have been marked
as deprecated for a while so it's not totally surprising, but still
ridic. libcoro has implemented a very similar API using setjmp and
longjmp internally (among other configurable backends). Switching over
to setjmp\longjmp, along with the pthread* hook changes fixes fiber
support in Lion.

Closes gh-28
  • Loading branch information
laverdet committed Jul 25, 2011
1 parent 23127dc commit f144c86
Show file tree
Hide file tree
Showing 9 changed files with 988 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
*.dylib
*.dSYM
*.o
*.so
*.node
*.swp
12 changes: 8 additions & 4 deletions src/Makefile
Expand Up @@ -9,17 +9,21 @@ ifeq ($(NODE_PLATFORM), darwin)
CPP_NODEFLAGS = -bundle -undefined dynamic_lookup
endif
COROUTINE_SO_FULL := $(shell echo `pwd`/$(COROUTINE_SO))
CPPFLAGS += -DCORO_SJLJ

all: $(COROUTINE_SO) fibers.node

libcoro.o: libcoro/coro.c
$(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -c -o $@ $^

$(COROUTINE_SO_FULL): $(COROUTINE_SO)

$(COROUTINE_SO): coroutine.cc
$(CXX) $(CPPFLAGS) $(CPP_DYFLAGS) -o $@ $^ -lpthread -ldl
$(COROUTINE_SO): coroutine.cc libcoro.o
$(CXX) $(CPP_DYFLAGS) $(CXXFLAGS) $(CPPFLAGS) -o $@ $^ -lpthread -ldl

fibers.node: fibers.cc $(COROUTINE_SO_FULL)
$(CXX) $(CPPFLAGS) $(CPP_NODEFLAGS) -o $@ $^
$(CXX) $(CPP_NODEFLAGS) $(CPPFLAGS) -o $@ $^

clean:
-$(RM) fibers.node $(COROUTINE_SO)
-$(RM) fibers.node libcoro.o $(COROUTINE_SO)
-$(RM) -r *.dSYM
21 changes: 11 additions & 10 deletions src/coroutine.cc
Expand Up @@ -3,7 +3,6 @@
#define __GNU_SOURCE
#include <dlfcn.h>
#include <pthread.h>
#include <ucontext.h>

#include <stdexcept>
#include <stack>
Expand Down Expand Up @@ -176,9 +175,9 @@ vector<pthread_dtor_t> Thread::dtors;
* Coroutine class definition
*/
size_t Coroutine::stack_size = 0;
void Coroutine::trampoline(Coroutine &that) {
void Coroutine::trampoline(void* that) {
while (true) {
that.entry(const_cast<void*>(that.arg));
static_cast<Coroutine*>(that)->entry(const_cast<void*>(static_cast<Coroutine*>(that)->arg));
}
}

Expand All @@ -202,10 +201,11 @@ Coroutine::Coroutine(Thread& t, entry_t& entry, void* arg) :
stack(stack_size),
entry(entry),
arg(arg) {
getcontext(&context);
context.uc_stack.ss_size = stack_size;
context.uc_stack.ss_sp = &stack[0];
makecontext(&context, (void(*)(void))trampoline, 1, this);
coro_create(&context, trampoline, this, &stack[0], stack_size);
}

Coroutine::~Coroutine() {
coro_destroy(&context);
}

Coroutine& Coroutine::create_fiber(entry_t* entry, void* arg) {
Expand All @@ -228,7 +228,7 @@ void Coroutine::run() {
assert(&current != this);

thread.current_fiber = this;
swapcontext(&current.context, &context);
coro_transfer(&current.context, &context);

if (thread.delete_me) {
// This means finish() was called on the coroutine and the pool was full so this coroutine needs
Expand All @@ -246,7 +246,7 @@ void Coroutine::finish(Coroutine& next) {
assert(thread.current_fiber == this);
thread.fiber_did_finish(*this);
thread.current_fiber = &next;
swapcontext(&context, &next.context);
coro_transfer(&context, &next.context);
}

void* Coroutine::bottom() const {
Expand Down Expand Up @@ -423,8 +423,9 @@ int pthread_equal(pthread_t left, pthread_t right) {
}
#endif

int pthread_join(pthread_t thread, void** retval) {
int pthread_join(void* thread, void** retval) {
assert(Loader::initialized);
cout <<(void*)thread <<"\n";
// pthread_join should return EDEADLK if you try to join with yourself..
return pthread_join(reinterpret_cast<Thread*>(thread)->handle, retval);
}
Expand Down
12 changes: 4 additions & 8 deletions src/coroutine.h
@@ -1,11 +1,7 @@
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif

#include <stdlib.h>
#include <ucontext.h>
#include <ext/pool_allocator.h>
#include <vector>
#include "libcoro/coro.h"

class Coroutine {
public:
Expand All @@ -17,15 +13,15 @@ class Coroutine {
// around that, as there is no constructor.
struct char_noinit { char x; };
class Thread& thread;
ucontext_t context;
coro_context context;
std::vector<char_noinit, __gnu_cxx::__pool_alloc<char_noinit> > stack;
std::vector<void*> fls_data;
entry_t* entry;
void* arg;
static size_t stack_size;

static void trampoline(Coroutine& that);
~Coroutine() {}
static void trampoline(void* that);
~Coroutine();

/**
* Constructor for currently running "fiber". This is really just original thread, but we
Expand Down
26 changes: 26 additions & 0 deletions src/libcoro/LICENSE
@@ -0,0 +1,26 @@
Copyright (c) 2000-2009 Marc Alexander Lehmann <schmorp@schmorp.de>

Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

Alternatively, the following files carry an additional notice that
explicitly allows relicensing under the GPLv2: coro.c, coro.h.

6 changes: 6 additions & 0 deletions src/libcoro/README
@@ -0,0 +1,6 @@
Configuration, documentation etc. is provided in the coro.h file. Please
note that the file conftest.c in this distribution is under the GPL. It is
not needed for proper operation of this library though, for that, coro.h
and coro.c suffice.

Marc Lehmann <schmorp@schmorp.de>
154 changes: 154 additions & 0 deletions src/libcoro/conftest.c
@@ -0,0 +1,154 @@
/*
* This file was taken from pth-1.40/aclocal.m4
* The original copyright is below.
*
* GNU Pth - The GNU Portable Threads
* Copyright (c) 1999-2001 Ralf S. Engelschall <rse@engelschall.com>
*
* This file is part of GNU Pth, a non-preemptive thread scheduling
* library which can be found at http://www.gnu.org/software/pth/.
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA, or contact Marc Lehmann <schmorp@schmorp.de>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_sigstack) || defined(TEST_sigaltstack)
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#endif
#if defined(TEST_makecontext)
#include <ucontext.h>
#endif
union alltypes {
long l;
double d;
void *vp;
void (*fp)(void);
char *cp;
};
static volatile char *handler_addr = (char *)0xDEAD;
#if defined(TEST_sigstack) || defined(TEST_sigaltstack)
static volatile int handler_done = 0;
void handler(int sig)
{
char garbage[1024];
int i;
auto int dummy;
for (i = 0; i < 1024; i++)
garbage[i] = 'X';
handler_addr = (char *)&dummy;
handler_done = 1;
return;
}
#endif
#if defined(TEST_makecontext)
static ucontext_t uc_handler;
static ucontext_t uc_main;
void handler(void)
{
char garbage[1024];
int i;
auto int dummy;
for (i = 0; i < 1024; i++)
garbage[i] = 'X';
handler_addr = (char *)&dummy;
swapcontext(&uc_handler, &uc_main);
return;
}
#endif
int main(int argc, char *argv[])
{
FILE *f;
char *skaddr;
char *skbuf;
int sksize;
char result[1024];
int i;
sksize = 32768;
skbuf = (char *)malloc(sksize*2+2*sizeof(union alltypes));
if (skbuf == NULL)
exit(1);
for (i = 0; i < sksize*2+2*sizeof(union alltypes); i++)
skbuf[i] = 'A';
skaddr = skbuf+sizeof(union alltypes);
#if defined(TEST_sigstack) || defined(TEST_sigaltstack)
{
struct sigaction sa;
#if defined(TEST_sigstack)
struct sigstack ss;
#elif defined(TEST_sigaltstack) && defined(HAVE_STACK_T)
stack_t ss;
#else
struct sigaltstack ss;
#endif
#if defined(TEST_sigstack)
ss.ss_sp = (void *)(skaddr + sksize);
ss.ss_onstack = 0;
if (sigstack(&ss, NULL) < 0)
exit(1);
#elif defined(TEST_sigaltstack)
ss.ss_sp = (void *)(skaddr + sksize);
ss.ss_size = sksize;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) < 0)
exit(1);
#endif
memset((void *)&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handler;
sa.sa_flags = SA_ONSTACK;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, NULL);
kill(getpid(), SIGUSR1);
while (!handler_done)
/*nop*/;
}
#endif
#if defined(TEST_makecontext)
{
if (getcontext(&uc_handler) != 0)
exit(1);
uc_handler.uc_link = NULL;
uc_handler.uc_stack.ss_sp = (void *)(skaddr + sksize);
uc_handler.uc_stack.ss_size = sksize;
uc_handler.uc_stack.ss_flags = 0;
makecontext(&uc_handler, handler, 1);
swapcontext(&uc_main, &uc_handler);
}
#endif
if (handler_addr == (char *)0xDEAD)
exit(1);
if (handler_addr < skaddr+sksize) {
/* stack was placed into lower area */
if (*(skaddr+sksize) != 'A')
sprintf(result, "(skaddr)+(sksize)-%d,(sksize)-%d",
sizeof(union alltypes), sizeof(union alltypes));
else
strcpy(result, "(skaddr)+(sksize),(sksize)");
}
else {
/* stack was placed into higher area */
if (*(skaddr+sksize*2) != 'A')
sprintf(result, "(skaddr),(sksize)-%d", sizeof(union alltypes));
else
strcpy(result, "(skaddr),(sksize)");
}
printf("%s\n", result);
exit(0);
}

0 comments on commit f144c86

Please sign in to comment.