Permalink
Browse files

Fibers based on pthreads instead of ucontext

This code is a little scrappy.. this is just an experiment I wanted to
run to quantify the advantages of using makecontext over pthreads. It
was possible to implement the current fibers library using only pthreads
with cond signaling (similar to v8cgi's implementation), however the
performance wasn't close enough to ucontext's performance.

In one case I wasn't able to utilize 100% CPU usage and saw a
performance hit there. In another case I was hitting >115% CPU usage
(multi-core) however much of this was spent in the scheduler, and the
test in question ran about half as fast.

The advantage is that you no longer need the node-fibers script with
this implementation, which means it's more portable and there's no more
LD_LIBRARY_PRELOAD'ing black magic which is desirable. Perhaps this code
could find some use after a clean up and more experimentation.
  • Loading branch information...
1 parent 3913a7e commit aaaaa52ddb348b0dc4d18b1fd014253cff01bc8c @laverdet committed Mar 24, 2011
Showing with 117 additions and 428 deletions.
  1. +2 −25 bin/node-fibers
  2. +0 −8 fibers.js
  3. +6 −18 src/Makefile
  4. +80 −356 src/coroutine.cc
  5. +21 −20 src/coroutine.h
  6. +8 −1 src/platform.mk
View
@@ -1,33 +1,10 @@
#!/bin/bash
+echo 'You don'\''t need to call node-fibers anymore! Just use `node` directly!' 1>&2
NODE=`which node`
if [[ -z "$NODE" ]]; then
echo "Could not find node in PATH!" 1>&2
exit 1
fi
-if [[ -e `dirname "$0"`/../src/fibers.node ]]; then
- # running straight from dist
- FIBERS_ROOT=`dirname "$0"`/../src
-else
- # in npm
- FIBERS_PATH=`echo 'require.resolve("fibers")' | $NODE 2>/dev/null | head -n1 | egrep -o '/[^'\'']+'`
- FIBERS_ROOT=`dirname "$FIBERS_PATH"`/src
- if [[ ! -e "$FIBERS_ROOT/fibers.node" ]]; then
- echo "Could not find the coroutine shim!" 1>&2
- exit 1
- fi
-fi
-
-UNAME=`uname`
-if [[ "$UNAME" == "Linux" ]]; then
- FIBER_SHIM=1 \
- LD_PRELOAD="$FIBERS_ROOT/coroutine.so" \
- $NODE "$@"
-elif [[ "$UNAME" == "Darwin" ]]; then
- FIBER_SHIM=1 \
- DYLD_INSERT_LIBRARIES="$FIBERS_ROOT/coroutine.dylib" \
- DYLD_FORCE_FLAT_NAMESPACE=1 \
- DYLD_LIBRARY_PATH="$FIBERS_ROOT" \
- $NODE "$@"
-fi
+$NODE "$@"
View
@@ -7,14 +7,6 @@ if (fs.statSync(process.execPath).mtime >
'could happen if you upgrade node. Try `npm rebuild fibers` to rebuild. If that doesn\'t ' +
'work you could consider running `touch ' + __dirname + 'src/fibers` and maybe there won\'t ' +
'be a problem.');
-} else if (!process.env.FIBER_SHIM) {
- throw new Error(
- 'Fiber support was not enabled when you ran node. To enable support for fibers, please run ' +
- 'node with the included `node-fibers` script. For example, instead of running:\n\n' +
- ' node script.js\n\n' +
- 'You should run:\n\n' +
- ' node-fibers script.js\n\n' +
- 'You will not be able to use Fiber without this support enabled.');
}
require('./src/fibers');
View
@@ -1,25 +1,13 @@
include platform.mk
-ifeq ($(NODE_PLATFORM), linux)
- CPP_DYFLAGS = -fPIC -shared
- CPP_NODEFLAGS = -fPIC -shared -Wl,-Bdynamic
-endif
-ifeq ($(NODE_PLATFORM), darwin)
- CPP_DYFLAGS = -dynamiclib
- CPP_NODEFLAGS = -bundle -undefined dynamic_lookup
-endif
-COROUTINE_SO_FULL := $(shell echo `pwd`/$(COROUTINE_SO))
+all: fibers.node
-all: $(COROUTINE_SO) fibers.node
+%.o: %.cc
+ $(CXX) $(CPPFLAGS) -c -o $@ $^
-$(COROUTINE_SO_FULL): $(COROUTINE_SO)
-
-$(COROUTINE_SO): coroutine.cc
- $(CXX) $(CPPFLAGS) $(CPP_DYFLAGS) -o $@ $^ -lpthread -ldl
-
-fibers.node: fibers.cc $(COROUTINE_SO_FULL)
- $(CXX) $(CPPFLAGS) $(CPP_NODEFLAGS) -o $@ $^
+fibers.node: fibers.o coroutine.o
+ $(CXX) $(CPPFLAGS) $(CPP_NODEFLAGS) -o $@ $^ -lpthread
clean:
- -$(RM) fibers.node $(COROUTINE_SO)
+ -$(RM) fibers.node coroutine.o fibers.o
-$(RM) -r *.dSYM
Oops, something went wrong.

0 comments on commit aaaaa52

Please sign in to comment.