diff --git a/src/events.cc b/src/events.cc index 064ddff0f2d..c1af9b62fc0 100644 --- a/src/events.cc +++ b/src/events.cc @@ -22,6 +22,9 @@ using namespace node; Persistent EventEmitter::constructor_template; +/* Poor Man's coroutines */ +static Promise *coroutine_top; + void EventEmitter::Initialize (Local ctemplate) { @@ -33,6 +36,8 @@ EventEmitter::Initialize (Local ctemplate) constructor_template->PrototypeTemplate()->Set(String::NewSymbol("emit"), __emit); // All other prototype methods are defined in events.js + + coroutine_top = NULL; } static bool @@ -187,19 +192,38 @@ void Promise::Block (void) { blocking_ = true; + + assert(prev_ == NULL); + if (coroutine_top) prev_ = coroutine_top; + coroutine_top = this; + ev_loop(EV_DEFAULT_UC_ 0); + + assert(!blocking_); +} + +void +Promise::Destack () +{ + assert(coroutine_top == this); + ev_unloop(EV_DEFAULT_ EVUNLOOP_ONE); + coroutine_top = prev_; + prev_ = NULL; } void Promise::Detach (void) { - if (blocking_) { - ev_unloop(EV_DEFAULT_ EVUNLOOP_ONE); - blocking_ = false; + /* Poor Man's coroutines */ + blocking_ = false; + while (coroutine_top && !coroutine_top->blocking_) { + coroutine_top->Destack(); } + if (ref_) { ev_unref(EV_DEFAULT_UC); } + ObjectWrap::Detach(); } diff --git a/src/events.h b/src/events.h index 579af9087ba..91a8f804ecb 100644 --- a/src/events.h +++ b/src/events.h @@ -46,11 +46,15 @@ class Promise : public EventEmitter { bool blocking_; bool ref_; + Promise *prev_; /* for the prev in the Poor Man's coroutine stack */ + + void Destack (); Promise () : EventEmitter() { blocking_ = false; ref_ = false; + prev_ = NULL; } }; diff --git a/test/mjsunit/test-wait-ordering.js b/test/mjsunit/test-wait-ordering.js new file mode 100644 index 00000000000..8f5a5fd0ce6 --- /dev/null +++ b/test/mjsunit/test-wait-ordering.js @@ -0,0 +1,30 @@ +include("mjsunit.js"); + +function timer (t) { + var promise = new node.Promise(); + setTimeout(function () { + promise.emitSuccess(); + }, t); + return promise; +} + +order = 0; +var a = new Date(); +function test_timeout_order(delay, desired_order) { + timer(0).addCallback(function() { + timer(delay).wait() + var b = new Date(); + assertTrue(b - a >= delay); + order++; + // A stronger assertion would be that the ordering is correct. + // With Poor Man's coroutines we cannot guarentee that. + // Replacing wait() with actual coroutines would solve that issue. + // assertEquals(desired_order, order); + }); +} +test_timeout_order(10000, 6); // Why does this have the proper order?? +test_timeout_order(5000, 5); +test_timeout_order(4000, 4); +test_timeout_order(3000, 3); +test_timeout_order(2000, 2); +test_timeout_order(1000, 1); diff --git a/website/api.txt b/website/api.txt index b1a29fb2625..179c928754d 100644 --- a/website/api.txt +++ b/website/api.txt @@ -173,6 +173,15 @@ If there were multiple arguments to +"success"+ then they are returned as an array. + If +"error"+ was emitted instead, +wait()+ throws an error. ++ +*IMPORTANT* +promise.wait()+ is not a true fiber/coroutine. If any other +promises are created and made to wait while the first promise waits, the +first promise's wait will not return until all others return. The benefit of +this is a simple implementation and the event loop does not get blocked. +Disadvantage is the possibility of situations where the promise stack grows +infinitely large because promises keep getting created and keep being told +to wait(). Use +promise.wait()+ sparingly--probably best used only during +program setup, not during busy server activity. === Standard I/O