Commit e8a14a7a authored by Kenton Varda's avatar Kenton Varda

Implement fibers on Unix.

Fibers allow code to be written in a synchronous / blocking style while running inside the KJ event loop, by executing the code on an alternate call stack and switching back to the main stack whenever it waits.

We introduce a new function, `kj::startFiber(stackSize, func)`. `func` is executed on the fiber stack. It is passed as its parameter a `WaitScope&`, which can then be passed into the `.wait()` method of any promise in order to wait on the promise in a blocking style. `startFiber()` returns a promise for the eventual value returned by `func()` (much as `evalLater()` and friends do).

This commit implements fibers on Unix via ucontext_t. Windows will come next (and will probably be easier...).
parent c28fb99c
......@@ -871,6 +871,77 @@ private:
}
};
// -------------------------------------------------------------------
class FiberBase: public PromiseNode, private Event {
// Base class for the outer PromiseNode representing a fiber.
public:
FiberBase(size_t stackSize, _::ExceptionOrValue& result);
~FiberBase() noexcept(false);
void start() { armDepthFirst(); }
// Call immediately after construction to begin executing the fiber.
class WaitDoneEvent;
void onReady(_::Event* event) noexcept override;
PromiseNode* getInnerForTrace() override;
protected:
bool isFinished() { return state == FINISHED; }
private:
enum { WAITING, RUNNING, CANCELED, FINISHED } state;
size_t stackSize;
struct Impl;
Impl& impl;
_::PromiseNode* currentInner = nullptr;
OnReadyEvent onReadyEvent;
_::ExceptionOrValue& result;
void run();
virtual void runImpl(WaitScope& waitScope) = 0;
struct StartRoutine;
void switchToFiber();
void switchToMain();
Maybe<Own<Event>> fire() override;
// Implements Event. Each time the event is fired, switchToFiber() is called.
friend class WaitScope;
friend void _::waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result,
WaitScope& waitScope);
friend bool _::pollImpl(_::PromiseNode& node, WaitScope& waitScope);
};
template <typename Func>
class Fiber final: public FiberBase {
public:
Fiber(size_t stackSize, Func&& func): FiberBase(stackSize, result), func(kj::fwd<Func>(func)) {}
typedef FixVoid<decltype(kj::instance<Func&>()(kj::instance<WaitScope&>()))> ResultType;
void get(ExceptionOrValue& output) noexcept override {
KJ_IREQUIRE(isFinished());
output.as<ResultType>() = kj::mv(result);
}
private:
Func func;
ExceptionOr<ResultType> result;
void runImpl(WaitScope& waitScope) override {
result.template as<ResultType>() =
MaybeVoidCaller<WaitScope&, ResultType>::apply(func, waitScope);
}
};
} // namespace _ (private)
// =======================================================================================
......@@ -1014,6 +1085,17 @@ inline PromiseForResult<Func, void> evalNow(Func&& func) {
return result;
}
template <typename Func>
inline PromiseForResult<Func, WaitScope&> startFiber(size_t stackSize, Func&& func) {
typedef _::FixVoid<_::ReturnType<Func, WaitScope&>> ResultT;
Own<_::FiberBase> intermediate = kj::heap<_::Fiber<Func>>(stackSize, kj::fwd<Func>(func));
intermediate->start();
auto result = _::PromiseNode::to<_::ChainPromises<_::ReturnType<Func, WaitScope&>>>(
_::maybeChain(kj::mv(intermediate), implicitCast<ResultT*>(nullptr)));
return _::maybeReduce(kj::mv(result), false);
}
template <typename T>
template <typename ErrorFunc>
void Promise<T>::detach(ErrorFunc&& errorHandler) {
......
......@@ -188,6 +188,7 @@ class PromiseNode;
class ChainPromiseNode;
template <typename T>
class ForkHub;
class FiberBase;
class Event;
class XThreadEvent;
......
......@@ -844,5 +844,93 @@ KJ_TEST("exclusiveJoin both events complete simultaneously") {
KJ_EXPECT(!joined.poll(waitScope));
}
KJ_TEST("start a fiber") {
EventLoop loop;
WaitScope waitScope(loop);
auto paf = newPromiseAndFulfiller<int>();
Promise<StringPtr> fiber = startFiber(65536,
[promise = kj::mv(paf.promise)](WaitScope& fiberScope) mutable {
int i = promise.wait(fiberScope);
KJ_EXPECT(i == 123);
return "foo"_kj;
});
KJ_EXPECT(!fiber.poll(waitScope));
paf.fulfiller->fulfill(123);
KJ_ASSERT(fiber.poll(waitScope));
KJ_EXPECT(fiber.wait(waitScope) == "foo");
}
KJ_TEST("fiber promise chaining") {
EventLoop loop;
WaitScope waitScope(loop);
auto paf = newPromiseAndFulfiller<int>();
bool ran = false;
Promise<int> fiber = startFiber(65536,
[promise = kj::mv(paf.promise), &ran](WaitScope& fiberScope) mutable {
ran = true;
return kj::mv(promise);
});
KJ_EXPECT(!ran);
KJ_EXPECT(!fiber.poll(waitScope));
KJ_EXPECT(ran);
paf.fulfiller->fulfill(123);
KJ_ASSERT(fiber.poll(waitScope));
KJ_EXPECT(fiber.wait(waitScope) == 123);
}
KJ_TEST("throw from a fiber") {
EventLoop loop;
WaitScope waitScope(loop);
auto paf = newPromiseAndFulfiller<void>();
Promise<void> fiber = startFiber(65536,
[promise = kj::mv(paf.promise)](WaitScope& fiberScope) mutable {
promise.wait(fiberScope);
KJ_FAIL_EXPECT("wait() should have thrown");
});
KJ_EXPECT(!fiber.poll(waitScope));
paf.fulfiller->reject(KJ_EXCEPTION(FAILED, "test exception"));
KJ_ASSERT(fiber.poll(waitScope));
KJ_EXPECT_THROW_MESSAGE("test exception", fiber.wait(waitScope));
}
KJ_TEST("cancel a fiber") {
EventLoop loop;
WaitScope waitScope(loop);
auto paf = newPromiseAndFulfiller<int>();
bool exited = false;
{
Promise<StringPtr> fiber = startFiber(65536,
[promise = kj::mv(paf.promise), &exited](WaitScope& fiberScope) mutable {
KJ_DEFER(exited = true);
int i = promise.wait(fiberScope);
KJ_EXPECT(i == 123);
return "foo"_kj;
});
KJ_EXPECT(!fiber.poll(waitScope));
KJ_EXPECT(!exited);
}
KJ_EXPECT(exited);
}
} // namespace
} // namespace kj
This diff is collapsed.
......@@ -229,8 +229,16 @@ public:
// around them in arbitrary ways. Therefore, callers really need to know if a function they
// are calling might wait(), and the `WaitScope&` parameter makes this clear.
//
// TODO(someday): Implement fibers, and let them call wait() even when they are handling an
// event.
// Usually, there is only one `WaitScope` for each `EventLoop`, and it can only be used at the
// top level of the thread owning the loop. Calling `wait()` with this `WaitScope` is what
// actually causes the event loop to run at all. This top-level `WaitScope` cannot be used
// recursively, so cannot be used within an event callback.
//
// However, it is possible to obtain a `WaitScope` in lower-level code by using fibers. Use
// kj::startFiber() to start some code executing on an alternate call stack. That code will get
// its own `WaitScope` allowing it to operate in a synchronous style. In this case, `wait()`
// switches back to the main stack in order to run the event loop, returning to the fiber's stack
// once the awaited promise resolves.
bool poll(WaitScope& waitScope);
// Returns true if a call to wait() would complete without blocking, false if it would block.
......@@ -244,6 +252,8 @@ public:
// The first poll() verifies that the promise doesn't resolve early, which would otherwise be
// hard to do deterministically. The second poll() allows you to check that the promise has
// resolved and avoid a wait() that might deadlock in the case that it hasn't.
//
// poll() is not supported in fibers; it will throw an exception.
ForkedPromise<T> fork() KJ_WARN_UNUSED_RESULT;
// Forks the promise, so that multiple different clients can independently wait on the result.
......@@ -380,6 +390,29 @@ PromiseForResult<Func, void> evalLast(Func&& func) KJ_WARN_UNUSED_RESULT;
// callback enqueues new events, then latter callbacks will not execute until those events are
// drained.
template <typename Func>
PromiseForResult<Func, WaitScope&> startFiber(size_t stackSize, Func&& func) KJ_WARN_UNUSED_RESULT;
// Executes `func()` in a fiber, returning a promise for the eventual reseult. `func()` will be
// passed a `WaitScope&` as its parameter, allowing it to call `.wait()` on promises. Thus, `func()`
// can be written in a synchronous, blocking style, instead of using `.then()`. This is often much
// easier to write and read, and may even be significantly faster if it allows the use of stack
// allocation rather than heap allocation.
//
// However, fibers have a major disadvantage: memory must be allocated for the fiber's call stack.
// The entire stack must be allocated at once, making it necessary to choose a stack size upfront
// that is big enough for whatever the fiber needs to do. Estimating this is often difficult. That
// said, over-estimating is not too terrible since pages of the stack will actually be allocated
// lazily when first accessed; actual memory usage will correspond to the "high watermark" of the
// actual stack usage. That said, this lazy allocation forces page faults, which can be quite slow.
// Worse, freeing a stack forces a TLB flush and shootdown -- all currently-executing threads will
// have to be interrupted to flush their CPU cores' TLB caches.
//
// In short, when performance matters, you should try to avoid creating fibers very frequently.
//
// TODO(perf): We should add a mechanism for freelisting stacks. However, this improves CPU usage
// at the expense of memory usage: stacks on the freelist will consume however many pages they
// used at their high watermark, forever.
template <typename T>
Promise<Array<T>> joinPromises(Array<Promise<T>>&& promises);
// Join an array of promises into a promise for an array.
......@@ -903,20 +936,31 @@ class WaitScope {
public:
inline explicit WaitScope(EventLoop& loop): loop(loop) { loop.enterScope(); }
inline ~WaitScope() { loop.leaveScope(); }
inline ~WaitScope() { if (fiber == nullptr) loop.leaveScope(); }
KJ_DISALLOW_COPY(WaitScope);
void poll();
// Pumps the event queue and polls for I/O until there's nothing left to do (without blocking).
//
// Not supported in fibers.
void setBusyPollInterval(uint count) { busyPollInterval = count; }
// Set the maximum number of events to run in a row before calling poll() on the EventPort to
// check for new I/O.
//
// This has no effect when used in a fiber.
private:
EventLoop& loop;
uint busyPollInterval = kj::maxValue;
kj::Maybe<_::FiberBase&> fiber;
explicit WaitScope(EventLoop& loop, _::FiberBase& fiber)
: loop(loop), fiber(fiber) {}
friend class EventLoop;
friend class _::FiberBase;
friend void _::waitImpl(Own<_::PromiseNode>&& node, _::ExceptionOrValue& result,
WaitScope& waitScope);
friend bool _::pollImpl(_::PromiseNode& node, WaitScope& waitScope);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment