cannam@147: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@147: // Licensed under the MIT License: cannam@147: // cannam@147: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@147: // of this software and associated documentation files (the "Software"), to deal cannam@147: // in the Software without restriction, including without limitation the rights cannam@147: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@147: // copies of the Software, and to permit persons to whom the Software is cannam@147: // furnished to do so, subject to the following conditions: cannam@147: // cannam@147: // The above copyright notice and this permission notice shall be included in cannam@147: // all copies or substantial portions of the Software. cannam@147: // cannam@147: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@147: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@147: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@147: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@147: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@147: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@147: // THE SOFTWARE. cannam@147: cannam@147: // This file contains extended inline implementation details that are required along with async.h. cannam@147: // We move this all into a separate file to make async.h more readable. cannam@147: // cannam@147: // Non-inline declarations here are defined in async.c++. cannam@147: cannam@147: #ifndef KJ_ASYNC_H_ cannam@147: #error "Do not include this directly; include kj/async.h." cannam@147: #include "async.h" // help IDE parse this file cannam@147: #endif cannam@147: cannam@147: #ifndef KJ_ASYNC_INL_H_ cannam@147: #define KJ_ASYNC_INL_H_ cannam@147: cannam@147: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS cannam@147: #pragma GCC system_header cannam@147: #endif cannam@147: cannam@147: namespace kj { cannam@147: namespace _ { // private cannam@147: cannam@147: template cannam@147: class ExceptionOr; cannam@147: cannam@147: class ExceptionOrValue { cannam@147: public: cannam@147: ExceptionOrValue(bool, Exception&& exception): exception(kj::mv(exception)) {} cannam@147: KJ_DISALLOW_COPY(ExceptionOrValue); cannam@147: cannam@147: void addException(Exception&& exception) { cannam@147: if (this->exception == nullptr) { cannam@147: this->exception = kj::mv(exception); cannam@147: } cannam@147: } cannam@147: cannam@147: template cannam@147: ExceptionOr& as() { return *static_cast*>(this); } cannam@147: template cannam@147: const ExceptionOr& as() const { return *static_cast*>(this); } cannam@147: cannam@147: Maybe exception; cannam@147: cannam@147: protected: cannam@147: // Allow subclasses to have move constructor / assignment. cannam@147: ExceptionOrValue() = default; cannam@147: ExceptionOrValue(ExceptionOrValue&& other) = default; cannam@147: ExceptionOrValue& operator=(ExceptionOrValue&& other) = default; cannam@147: }; cannam@147: cannam@147: template cannam@147: class ExceptionOr: public ExceptionOrValue { cannam@147: public: cannam@147: ExceptionOr() = default; cannam@147: ExceptionOr(T&& value): value(kj::mv(value)) {} cannam@147: ExceptionOr(bool, Exception&& exception): ExceptionOrValue(false, kj::mv(exception)) {} cannam@147: ExceptionOr(ExceptionOr&&) = default; cannam@147: ExceptionOr& operator=(ExceptionOr&&) = default; cannam@147: cannam@147: Maybe value; cannam@147: }; cannam@147: cannam@147: class Event { cannam@147: // An event waiting to be executed. Not for direct use by applications -- promises use this cannam@147: // internally. cannam@147: cannam@147: public: cannam@147: Event(); cannam@147: ~Event() noexcept(false); cannam@147: KJ_DISALLOW_COPY(Event); cannam@147: cannam@147: void armDepthFirst(); cannam@147: // Enqueue this event so that `fire()` will be called from the event loop soon. cannam@147: // cannam@147: // Events scheduled in this way are executed in depth-first order: if an event callback arms cannam@147: // more events, those events are placed at the front of the queue (in the order in which they cannam@147: // were armed), so that they run immediately after the first event's callback returns. cannam@147: // cannam@147: // Depth-first event scheduling is appropriate for events that represent simple continuations cannam@147: // of a previous event that should be globbed together for performance. Depth-first scheduling cannam@147: // can lead to starvation, so any long-running task must occasionally yield with cannam@147: // `armBreadthFirst()`. (Promise::then() uses depth-first whereas evalLater() uses cannam@147: // breadth-first.) cannam@147: // cannam@147: // To use breadth-first scheduling instead, use `armBreadthFirst()`. cannam@147: cannam@147: void armBreadthFirst(); cannam@147: // Like `armDepthFirst()` except that the event is placed at the end of the queue. cannam@147: cannam@147: kj::String trace(); cannam@147: // Dump debug info about this event. cannam@147: cannam@147: virtual _::PromiseNode* getInnerForTrace(); cannam@147: // If this event wraps a PromiseNode, get that node. Used for debug tracing. cannam@147: // Default implementation returns nullptr. cannam@147: cannam@147: protected: cannam@147: virtual Maybe> fire() = 0; cannam@147: // Fire the event. Possibly returns a pointer to itself, which will be discarded by the cannam@147: // caller. This is the only way that an event can delete itself as a result of firing, as cannam@147: // doing so from within fire() will throw an exception. cannam@147: cannam@147: private: cannam@147: friend class kj::EventLoop; cannam@147: EventLoop& loop; cannam@147: Event* next; cannam@147: Event** prev; cannam@147: bool firing = false; cannam@147: }; cannam@147: cannam@147: class PromiseNode { cannam@147: // A Promise contains a chain of PromiseNodes tracking the pending transformations. cannam@147: // cannam@147: // To reduce generated code bloat, PromiseNode is not a template. Instead, it makes very hacky cannam@147: // use of pointers to ExceptionOrValue which actually point to ExceptionOr, but are only cannam@147: // so down-cast in the few places that really need to be templated. Luckily this is all cannam@147: // internal implementation details. cannam@147: cannam@147: public: cannam@147: virtual void onReady(Event& event) noexcept = 0; cannam@147: // Arms the given event when ready. cannam@147: cannam@147: virtual void setSelfPointer(Own* selfPtr) noexcept; cannam@147: // Tells the node that `selfPtr` is the pointer that owns this node, and will continue to own cannam@147: // this node until it is destroyed or setSelfPointer() is called again. ChainPromiseNode uses cannam@147: // this to shorten redundant chains. The default implementation does nothing; only cannam@147: // ChainPromiseNode should implement this. cannam@147: cannam@147: virtual void get(ExceptionOrValue& output) noexcept = 0; cannam@147: // Get the result. `output` points to an ExceptionOr into which the result will be written. cannam@147: // Can only be called once, and only after the node is ready. Must be called directly from the cannam@147: // event loop, with no application code on the stack. cannam@147: cannam@147: virtual PromiseNode* getInnerForTrace(); cannam@147: // If this node wraps some other PromiseNode, get the wrapped node. Used for debug tracing. cannam@147: // Default implementation returns nullptr. cannam@147: cannam@147: protected: cannam@147: class OnReadyEvent { cannam@147: // Helper class for implementing onReady(). cannam@147: cannam@147: public: cannam@147: void init(Event& newEvent); cannam@147: // Returns true if arm() was already called. cannam@147: cannam@147: void arm(); cannam@147: // Arms the event if init() has already been called and makes future calls to init() return cannam@147: // true. cannam@147: cannam@147: private: cannam@147: Event* event = nullptr; cannam@147: }; cannam@147: }; cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class ImmediatePromiseNodeBase: public PromiseNode { cannam@147: public: cannam@147: ImmediatePromiseNodeBase(); cannam@147: ~ImmediatePromiseNodeBase() noexcept(false); cannam@147: cannam@147: void onReady(Event& event) noexcept override; cannam@147: }; cannam@147: cannam@147: template cannam@147: class ImmediatePromiseNode final: public ImmediatePromiseNodeBase { cannam@147: // A promise that has already been resolved to an immediate value or exception. cannam@147: cannam@147: public: cannam@147: ImmediatePromiseNode(ExceptionOr&& result): result(kj::mv(result)) {} cannam@147: cannam@147: void get(ExceptionOrValue& output) noexcept override { cannam@147: output.as() = kj::mv(result); cannam@147: } cannam@147: cannam@147: private: cannam@147: ExceptionOr result; cannam@147: }; cannam@147: cannam@147: class ImmediateBrokenPromiseNode final: public ImmediatePromiseNodeBase { cannam@147: public: cannam@147: ImmediateBrokenPromiseNode(Exception&& exception); cannam@147: cannam@147: void get(ExceptionOrValue& output) noexcept override; cannam@147: cannam@147: private: cannam@147: Exception exception; cannam@147: }; cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class AttachmentPromiseNodeBase: public PromiseNode { cannam@147: public: cannam@147: AttachmentPromiseNodeBase(Own&& dependency); cannam@147: cannam@147: void onReady(Event& event) noexcept override; cannam@147: void get(ExceptionOrValue& output) noexcept override; cannam@147: PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: private: cannam@147: Own dependency; cannam@147: cannam@147: void dropDependency(); cannam@147: cannam@147: template cannam@147: friend class AttachmentPromiseNode; cannam@147: }; cannam@147: cannam@147: template cannam@147: class AttachmentPromiseNode final: public AttachmentPromiseNodeBase { cannam@147: // A PromiseNode that holds on to some object (usually, an Own, but could be any movable cannam@147: // object) until the promise resolves. cannam@147: cannam@147: public: cannam@147: AttachmentPromiseNode(Own&& dependency, Attachment&& attachment) cannam@147: : AttachmentPromiseNodeBase(kj::mv(dependency)), cannam@147: attachment(kj::mv(attachment)) {} cannam@147: cannam@147: ~AttachmentPromiseNode() noexcept(false) { cannam@147: // We need to make sure the dependency is deleted before we delete the attachment because the cannam@147: // dependency may be using the attachment. cannam@147: dropDependency(); cannam@147: } cannam@147: cannam@147: private: cannam@147: Attachment attachment; cannam@147: }; cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class PtmfHelper { cannam@147: // This class is a private helper for GetFunctorStartAddress. The class represents the internal cannam@147: // representation of a pointer-to-member-function. cannam@147: cannam@147: template cannam@147: friend struct GetFunctorStartAddress; cannam@147: cannam@147: #if __GNUG__ cannam@147: cannam@147: void* ptr; cannam@147: ptrdiff_t adj; cannam@147: // Layout of a pointer-to-member-function used by GCC and compatible compilers. cannam@147: cannam@147: void* apply(void* obj) { cannam@147: #if defined(__arm__) || defined(__mips__) || defined(__aarch64__) cannam@147: if (adj & 1) { cannam@147: ptrdiff_t voff = (ptrdiff_t)ptr; cannam@147: #else cannam@147: ptrdiff_t voff = (ptrdiff_t)ptr; cannam@147: if (voff & 1) { cannam@147: voff &= ~1; cannam@147: #endif cannam@147: return *(void**)(*(char**)obj + voff); cannam@147: } else { cannam@147: return ptr; cannam@147: } cannam@147: } cannam@147: cannam@147: #define BODY \ cannam@147: PtmfHelper result; \ cannam@147: static_assert(sizeof(p) == sizeof(result), "unknown ptmf layout"); \ cannam@147: memcpy(&result, &p, sizeof(result)); \ cannam@147: return result cannam@147: cannam@147: #else // __GNUG__ cannam@147: cannam@147: void* apply(void* obj) { return nullptr; } cannam@147: // TODO(port): PTMF instruction address extraction cannam@147: cannam@147: #define BODY return PtmfHelper{} cannam@147: cannam@147: #endif // __GNUG__, else cannam@147: cannam@147: template cannam@147: static PtmfHelper from(F p) { BODY; } cannam@147: // Create a PtmfHelper from some arbitrary pointer-to-member-function which is not cannam@147: // overloaded nor a template. In this case the compiler is able to deduce the full function cannam@147: // signature directly given the name since there is only one function with that name. cannam@147: cannam@147: template cannam@147: static PtmfHelper from(R (C::*p)(NoInfer

...)) { BODY; } cannam@147: template cannam@147: static PtmfHelper from(R (C::*p)(NoInfer

...) const) { BODY; } cannam@147: // Create a PtmfHelper from some poniter-to-member-function which is a template. In this case cannam@147: // the function must match exactly the containing type C, return type R, and parameter types P... cannam@147: // GetFunctorStartAddress normally specifies exactly the correct C and R, but can only make a cannam@147: // guess at P. Luckily, if the function parameters are template parameters then it's not cannam@147: // necessary to be precise about P. cannam@147: #undef BODY cannam@147: }; cannam@147: cannam@147: template cannam@147: struct GetFunctorStartAddress { cannam@147: // Given a functor (any object defining operator()), return the start address of the function, cannam@147: // suitable for passing to addr2line to obtain a source file/line for debugging purposes. cannam@147: // cannam@147: // This turns out to be incredibly hard to implement in the presence of overloaded or templated cannam@147: // functors. Therefore, we impose these specific restrictions, specific to our use case: cannam@147: // - Overloading is not allowed, but templating is. (Generally we only intend to support lambdas cannam@147: // anyway.) cannam@147: // - The template parameters to GetFunctorStartAddress specify a hint as to the expected cannam@147: // parameter types. If the functor is templated, its parameters must match exactly these types. cannam@147: // (If it's not templated, ParamTypes are ignored.) cannam@147: cannam@147: template cannam@147: static void* apply(Func&& func) { cannam@147: typedef decltype(func(instance()...)) ReturnType; cannam@147: return PtmfHelper::from, ParamTypes...>( cannam@147: &Decay::operator()).apply(&func); cannam@147: } cannam@147: }; cannam@147: cannam@147: template <> cannam@147: struct GetFunctorStartAddress: public GetFunctorStartAddress<> {}; cannam@147: // Hack for TransformPromiseNode use case: an input type of `Void` indicates that the function cannam@147: // actually has no parameters. cannam@147: cannam@147: class TransformPromiseNodeBase: public PromiseNode { cannam@147: public: cannam@147: TransformPromiseNodeBase(Own&& dependency, void* continuationTracePtr); cannam@147: cannam@147: void onReady(Event& event) noexcept override; cannam@147: void get(ExceptionOrValue& output) noexcept override; cannam@147: PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: private: cannam@147: Own dependency; cannam@147: void* continuationTracePtr; cannam@147: cannam@147: void dropDependency(); cannam@147: void getDepResult(ExceptionOrValue& output); cannam@147: cannam@147: virtual void getImpl(ExceptionOrValue& output) = 0; cannam@147: cannam@147: template cannam@147: friend class TransformPromiseNode; cannam@147: }; cannam@147: cannam@147: template cannam@147: class TransformPromiseNode final: public TransformPromiseNodeBase { cannam@147: // A PromiseNode that transforms the result of another PromiseNode through an application-provided cannam@147: // function (implements `then()`). cannam@147: cannam@147: public: cannam@147: TransformPromiseNode(Own&& dependency, Func&& func, ErrorFunc&& errorHandler) cannam@147: : TransformPromiseNodeBase(kj::mv(dependency), cannam@147: GetFunctorStartAddress::apply(func)), cannam@147: func(kj::fwd(func)), errorHandler(kj::fwd(errorHandler)) {} cannam@147: cannam@147: ~TransformPromiseNode() noexcept(false) { cannam@147: // We need to make sure the dependency is deleted before we delete the continuations because it cannam@147: // is a common pattern for the continuations to hold ownership of objects that might be in-use cannam@147: // by the dependency. cannam@147: dropDependency(); cannam@147: } cannam@147: cannam@147: private: cannam@147: Func func; cannam@147: ErrorFunc errorHandler; cannam@147: cannam@147: void getImpl(ExceptionOrValue& output) override { cannam@147: ExceptionOr depResult; cannam@147: getDepResult(depResult); cannam@147: KJ_IF_MAYBE(depException, depResult.exception) { cannam@147: output.as() = handle( cannam@147: MaybeVoidCaller>>::apply( cannam@147: errorHandler, kj::mv(*depException))); cannam@147: } else KJ_IF_MAYBE(depValue, depResult.value) { cannam@147: output.as() = handle(MaybeVoidCaller::apply(func, kj::mv(*depValue))); cannam@147: } cannam@147: } cannam@147: cannam@147: ExceptionOr handle(T&& value) { cannam@147: return kj::mv(value); cannam@147: } cannam@147: ExceptionOr handle(PropagateException::Bottom&& value) { cannam@147: return ExceptionOr(false, value.asException()); cannam@147: } cannam@147: }; cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class ForkHubBase; cannam@147: cannam@147: class ForkBranchBase: public PromiseNode { cannam@147: public: cannam@147: ForkBranchBase(Own&& hub); cannam@147: ~ForkBranchBase() noexcept(false); cannam@147: cannam@147: void hubReady() noexcept; cannam@147: // Called by the hub to indicate that it is ready. cannam@147: cannam@147: // implements PromiseNode ------------------------------------------ cannam@147: void onReady(Event& event) noexcept override; cannam@147: PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: protected: cannam@147: inline ExceptionOrValue& getHubResultRef(); cannam@147: cannam@147: void releaseHub(ExceptionOrValue& output); cannam@147: // Release the hub. If an exception is thrown, add it to `output`. cannam@147: cannam@147: private: cannam@147: OnReadyEvent onReadyEvent; cannam@147: cannam@147: Own hub; cannam@147: ForkBranchBase* next = nullptr; cannam@147: ForkBranchBase** prevPtr = nullptr; cannam@147: cannam@147: friend class ForkHubBase; cannam@147: }; cannam@147: cannam@147: template T copyOrAddRef(T& t) { return t; } cannam@147: template Own copyOrAddRef(Own& t) { return t->addRef(); } cannam@147: cannam@147: template cannam@147: class ForkBranch final: public ForkBranchBase { cannam@147: // A PromiseNode that implements one branch of a fork -- i.e. one of the branches that receives cannam@147: // a const reference. cannam@147: cannam@147: public: cannam@147: ForkBranch(Own&& hub): ForkBranchBase(kj::mv(hub)) {} cannam@147: cannam@147: void get(ExceptionOrValue& output) noexcept override { cannam@147: ExceptionOr& hubResult = getHubResultRef().template as(); cannam@147: KJ_IF_MAYBE(value, hubResult.value) { cannam@147: output.as().value = copyOrAddRef(*value); cannam@147: } else { cannam@147: output.as().value = nullptr; cannam@147: } cannam@147: output.exception = hubResult.exception; cannam@147: releaseHub(output); cannam@147: } cannam@147: }; cannam@147: cannam@147: template cannam@147: class SplitBranch final: public ForkBranchBase { cannam@147: // A PromiseNode that implements one branch of a fork -- i.e. one of the branches that receives cannam@147: // a const reference. cannam@147: cannam@147: public: cannam@147: SplitBranch(Own&& hub): ForkBranchBase(kj::mv(hub)) {} cannam@147: cannam@147: typedef kj::Decay(kj::instance()))> Element; cannam@147: cannam@147: void get(ExceptionOrValue& output) noexcept override { cannam@147: ExceptionOr& hubResult = getHubResultRef().template as(); cannam@147: KJ_IF_MAYBE(value, hubResult.value) { cannam@147: output.as().value = kj::mv(kj::get(*value)); cannam@147: } else { cannam@147: output.as().value = nullptr; cannam@147: } cannam@147: output.exception = hubResult.exception; cannam@147: releaseHub(output); cannam@147: } cannam@147: }; cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class ForkHubBase: public Refcounted, protected Event { cannam@147: public: cannam@147: ForkHubBase(Own&& inner, ExceptionOrValue& resultRef); cannam@147: cannam@147: inline ExceptionOrValue& getResultRef() { return resultRef; } cannam@147: cannam@147: private: cannam@147: Own inner; cannam@147: ExceptionOrValue& resultRef; cannam@147: cannam@147: ForkBranchBase* headBranch = nullptr; cannam@147: ForkBranchBase** tailBranch = &headBranch; cannam@147: // Tail becomes null once the inner promise is ready and all branches have been notified. cannam@147: cannam@147: Maybe> fire() override; cannam@147: _::PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: friend class ForkBranchBase; cannam@147: }; cannam@147: cannam@147: template cannam@147: class ForkHub final: public ForkHubBase { cannam@147: // A PromiseNode that implements the hub of a fork. The first call to Promise::fork() replaces cannam@147: // the promise's outer node with a ForkHub, and subsequent calls add branches to that hub (if cannam@147: // possible). cannam@147: cannam@147: public: cannam@147: ForkHub(Own&& inner): ForkHubBase(kj::mv(inner), result) {} cannam@147: cannam@147: Promise<_::UnfixVoid> addBranch() { cannam@147: return Promise<_::UnfixVoid>(false, kj::heap>(addRef(*this))); cannam@147: } cannam@147: cannam@147: _::SplitTuplePromise split() { cannam@147: return splitImpl(MakeIndexes()>()); cannam@147: } cannam@147: cannam@147: private: cannam@147: ExceptionOr result; cannam@147: cannam@147: template cannam@147: _::SplitTuplePromise splitImpl(Indexes) { cannam@147: return kj::tuple(addSplit()...); cannam@147: } cannam@147: cannam@147: template cannam@147: Promise::Element>> addSplit() { cannam@147: return Promise::Element>>( cannam@147: false, maybeChain(kj::heap>(addRef(*this)), cannam@147: implicitCast::Element*>(nullptr))); cannam@147: } cannam@147: }; cannam@147: cannam@147: inline ExceptionOrValue& ForkBranchBase::getHubResultRef() { cannam@147: return hub->getResultRef(); cannam@147: } cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class ChainPromiseNode final: public PromiseNode, public Event { cannam@147: // Promise node which reduces Promise> to Promise. cannam@147: // cannam@147: // `Event` is only a public base class because otherwise we can't cast Own to cannam@147: // Own. Ugh, templates and private... cannam@147: cannam@147: public: cannam@147: explicit ChainPromiseNode(Own inner); cannam@147: ~ChainPromiseNode() noexcept(false); cannam@147: cannam@147: void onReady(Event& event) noexcept override; cannam@147: void setSelfPointer(Own* selfPtr) noexcept override; cannam@147: void get(ExceptionOrValue& output) noexcept override; cannam@147: PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: private: cannam@147: enum State { cannam@147: STEP1, cannam@147: STEP2 cannam@147: }; cannam@147: cannam@147: State state; cannam@147: cannam@147: Own inner; cannam@147: // In STEP1, a PromiseNode for a Promise. cannam@147: // In STEP2, a PromiseNode for a T. cannam@147: cannam@147: Event* onReadyEvent = nullptr; cannam@147: Own* selfPtr = nullptr; cannam@147: cannam@147: Maybe> fire() override; cannam@147: }; cannam@147: cannam@147: template cannam@147: Own maybeChain(Own&& node, Promise*) { cannam@147: return heap(kj::mv(node)); cannam@147: } cannam@147: cannam@147: template cannam@147: Own&& maybeChain(Own&& node, T*) { cannam@147: return kj::mv(node); cannam@147: } cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class ExclusiveJoinPromiseNode final: public PromiseNode { cannam@147: public: cannam@147: ExclusiveJoinPromiseNode(Own left, Own right); cannam@147: ~ExclusiveJoinPromiseNode() noexcept(false); cannam@147: cannam@147: void onReady(Event& event) noexcept override; cannam@147: void get(ExceptionOrValue& output) noexcept override; cannam@147: PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: private: cannam@147: class Branch: public Event { cannam@147: public: cannam@147: Branch(ExclusiveJoinPromiseNode& joinNode, Own dependency); cannam@147: ~Branch() noexcept(false); cannam@147: cannam@147: bool get(ExceptionOrValue& output); cannam@147: // Returns true if this is the side that finished. cannam@147: cannam@147: Maybe> fire() override; cannam@147: _::PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: private: cannam@147: ExclusiveJoinPromiseNode& joinNode; cannam@147: Own dependency; cannam@147: }; cannam@147: cannam@147: Branch left; cannam@147: Branch right; cannam@147: OnReadyEvent onReadyEvent; cannam@147: }; cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class ArrayJoinPromiseNodeBase: public PromiseNode { cannam@147: public: cannam@147: ArrayJoinPromiseNodeBase(Array> promises, cannam@147: ExceptionOrValue* resultParts, size_t partSize); cannam@147: ~ArrayJoinPromiseNodeBase() noexcept(false); cannam@147: cannam@147: void onReady(Event& event) noexcept override final; cannam@147: void get(ExceptionOrValue& output) noexcept override final; cannam@147: PromiseNode* getInnerForTrace() override final; cannam@147: cannam@147: protected: cannam@147: virtual void getNoError(ExceptionOrValue& output) noexcept = 0; cannam@147: // Called to compile the result only in the case where there were no errors. cannam@147: cannam@147: private: cannam@147: uint countLeft; cannam@147: OnReadyEvent onReadyEvent; cannam@147: cannam@147: class Branch final: public Event { cannam@147: public: cannam@147: Branch(ArrayJoinPromiseNodeBase& joinNode, Own dependency, cannam@147: ExceptionOrValue& output); cannam@147: ~Branch() noexcept(false); cannam@147: cannam@147: Maybe> fire() override; cannam@147: _::PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: Maybe getPart(); cannam@147: // Calls dependency->get(output). If there was an exception, return it. cannam@147: cannam@147: private: cannam@147: ArrayJoinPromiseNodeBase& joinNode; cannam@147: Own dependency; cannam@147: ExceptionOrValue& output; cannam@147: }; cannam@147: cannam@147: Array branches; cannam@147: }; cannam@147: cannam@147: template cannam@147: class ArrayJoinPromiseNode final: public ArrayJoinPromiseNodeBase { cannam@147: public: cannam@147: ArrayJoinPromiseNode(Array> promises, cannam@147: Array> resultParts) cannam@147: : ArrayJoinPromiseNodeBase(kj::mv(promises), resultParts.begin(), sizeof(ExceptionOr)), cannam@147: resultParts(kj::mv(resultParts)) {} cannam@147: cannam@147: protected: cannam@147: void getNoError(ExceptionOrValue& output) noexcept override { cannam@147: auto builder = heapArrayBuilder(resultParts.size()); cannam@147: for (auto& part: resultParts) { cannam@147: KJ_IASSERT(part.value != nullptr, cannam@147: "Bug in KJ promise framework: Promise result had neither value no exception."); cannam@147: builder.add(kj::mv(*_::readMaybe(part.value))); cannam@147: } cannam@147: output.as>() = builder.finish(); cannam@147: } cannam@147: cannam@147: private: cannam@147: Array> resultParts; cannam@147: }; cannam@147: cannam@147: template <> cannam@147: class ArrayJoinPromiseNode final: public ArrayJoinPromiseNodeBase { cannam@147: public: cannam@147: ArrayJoinPromiseNode(Array> promises, cannam@147: Array> resultParts); cannam@147: ~ArrayJoinPromiseNode(); cannam@147: cannam@147: protected: cannam@147: void getNoError(ExceptionOrValue& output) noexcept override; cannam@147: cannam@147: private: cannam@147: Array> resultParts; cannam@147: }; cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class EagerPromiseNodeBase: public PromiseNode, protected Event { cannam@147: // A PromiseNode that eagerly evaluates its dependency even if its dependent does not eagerly cannam@147: // evaluate it. cannam@147: cannam@147: public: cannam@147: EagerPromiseNodeBase(Own&& dependency, ExceptionOrValue& resultRef); cannam@147: cannam@147: void onReady(Event& event) noexcept override; cannam@147: PromiseNode* getInnerForTrace() override; cannam@147: cannam@147: private: cannam@147: Own dependency; cannam@147: OnReadyEvent onReadyEvent; cannam@147: cannam@147: ExceptionOrValue& resultRef; cannam@147: cannam@147: Maybe> fire() override; cannam@147: }; cannam@147: cannam@147: template cannam@147: class EagerPromiseNode final: public EagerPromiseNodeBase { cannam@147: public: cannam@147: EagerPromiseNode(Own&& dependency) cannam@147: : EagerPromiseNodeBase(kj::mv(dependency), result) {} cannam@147: cannam@147: void get(ExceptionOrValue& output) noexcept override { cannam@147: output.as() = kj::mv(result); cannam@147: } cannam@147: cannam@147: private: cannam@147: ExceptionOr result; cannam@147: }; cannam@147: cannam@147: template cannam@147: Own spark(Own&& node) { cannam@147: // Forces evaluation of the given node to begin as soon as possible, even if no one is waiting cannam@147: // on it. cannam@147: return heap>(kj::mv(node)); cannam@147: } cannam@147: cannam@147: // ------------------------------------------------------------------- cannam@147: cannam@147: class AdapterPromiseNodeBase: public PromiseNode { cannam@147: public: cannam@147: void onReady(Event& event) noexcept override; cannam@147: cannam@147: protected: cannam@147: inline void setReady() { cannam@147: onReadyEvent.arm(); cannam@147: } cannam@147: cannam@147: private: cannam@147: OnReadyEvent onReadyEvent; cannam@147: }; cannam@147: cannam@147: template cannam@147: class AdapterPromiseNode final: public AdapterPromiseNodeBase, cannam@147: private PromiseFulfiller> { cannam@147: // A PromiseNode that wraps a PromiseAdapter. cannam@147: cannam@147: public: cannam@147: template cannam@147: AdapterPromiseNode(Params&&... params) cannam@147: : adapter(static_cast>&>(*this), kj::fwd(params)...) {} cannam@147: cannam@147: void get(ExceptionOrValue& output) noexcept override { cannam@147: KJ_IREQUIRE(!isWaiting()); cannam@147: output.as() = kj::mv(result); cannam@147: } cannam@147: cannam@147: private: cannam@147: ExceptionOr result; cannam@147: bool waiting = true; cannam@147: Adapter adapter; cannam@147: cannam@147: void fulfill(T&& value) override { cannam@147: if (waiting) { cannam@147: waiting = false; cannam@147: result = ExceptionOr(kj::mv(value)); cannam@147: setReady(); cannam@147: } cannam@147: } cannam@147: cannam@147: void reject(Exception&& exception) override { cannam@147: if (waiting) { cannam@147: waiting = false; cannam@147: result = ExceptionOr(false, kj::mv(exception)); cannam@147: setReady(); cannam@147: } cannam@147: } cannam@147: cannam@147: bool isWaiting() override { cannam@147: return waiting; cannam@147: } cannam@147: }; cannam@147: cannam@147: } // namespace _ (private) cannam@147: cannam@147: // ======================================================================================= cannam@147: cannam@147: template cannam@147: Promise::Promise(_::FixVoid value) cannam@147: : PromiseBase(heap<_::ImmediatePromiseNode<_::FixVoid>>(kj::mv(value))) {} cannam@147: cannam@147: template cannam@147: Promise::Promise(kj::Exception&& exception) cannam@147: : PromiseBase(heap<_::ImmediateBrokenPromiseNode>(kj::mv(exception))) {} cannam@147: cannam@147: template cannam@147: template cannam@147: PromiseForResult Promise::then(Func&& func, ErrorFunc&& errorHandler) { cannam@147: typedef _::FixVoid<_::ReturnType> ResultT; cannam@147: cannam@147: Own<_::PromiseNode> intermediate = cannam@147: heap<_::TransformPromiseNode, Func, ErrorFunc>>( cannam@147: kj::mv(node), kj::fwd(func), kj::fwd(errorHandler)); cannam@147: return PromiseForResult(false, cannam@147: _::maybeChain(kj::mv(intermediate), implicitCast(nullptr))); cannam@147: } cannam@147: cannam@147: namespace _ { // private cannam@147: cannam@147: template cannam@147: struct IdentityFunc { cannam@147: inline T operator()(T&& value) const { cannam@147: return kj::mv(value); cannam@147: } cannam@147: }; cannam@147: template cannam@147: struct IdentityFunc> { cannam@147: inline Promise operator()(T&& value) const { cannam@147: return kj::mv(value); cannam@147: } cannam@147: }; cannam@147: template <> cannam@147: struct IdentityFunc { cannam@147: inline void operator()() const {} cannam@147: }; cannam@147: template <> cannam@147: struct IdentityFunc> { cannam@147: Promise operator()() const; cannam@147: // This can't be inline because it will make the translation unit depend on kj-async. Awkwardly, cannam@147: // Cap'n Proto relies on being able to include this header without creating such a link-time cannam@147: // dependency. cannam@147: }; cannam@147: cannam@147: } // namespace _ (private) cannam@147: cannam@147: template cannam@147: template cannam@147: Promise Promise::catch_(ErrorFunc&& errorHandler) { cannam@147: // then()'s ErrorFunc can only return a Promise if Func also returns a Promise. In this case, cannam@147: // Func is being filled in automatically. We want to make sure ErrorFunc can return a Promise, cannam@147: // but we don't want the extra overhead of promise chaining if ErrorFunc doesn't actually cannam@147: // return a promise. So we make our Func return match ErrorFunc. cannam@147: return then(_::IdentityFunc()))>(), cannam@147: kj::fwd(errorHandler)); cannam@147: } cannam@147: cannam@147: template cannam@147: T Promise::wait(WaitScope& waitScope) { cannam@147: _::ExceptionOr<_::FixVoid> result; cannam@147: cannam@147: waitImpl(kj::mv(node), result, waitScope); cannam@147: cannam@147: KJ_IF_MAYBE(value, result.value) { cannam@147: KJ_IF_MAYBE(exception, result.exception) { cannam@147: throwRecoverableException(kj::mv(*exception)); cannam@147: } cannam@147: return _::returnMaybeVoid(kj::mv(*value)); cannam@147: } else KJ_IF_MAYBE(exception, result.exception) { cannam@147: throwFatalException(kj::mv(*exception)); cannam@147: } else { cannam@147: // Result contained neither a value nor an exception? cannam@147: KJ_UNREACHABLE; cannam@147: } cannam@147: } cannam@147: cannam@147: template <> cannam@147: inline void Promise::wait(WaitScope& waitScope) { cannam@147: // Override case to use throwRecoverableException(). cannam@147: cannam@147: _::ExceptionOr<_::Void> result; cannam@147: cannam@147: waitImpl(kj::mv(node), result, waitScope); cannam@147: cannam@147: if (result.value != nullptr) { cannam@147: KJ_IF_MAYBE(exception, result.exception) { cannam@147: throwRecoverableException(kj::mv(*exception)); cannam@147: } cannam@147: } else KJ_IF_MAYBE(exception, result.exception) { cannam@147: throwRecoverableException(kj::mv(*exception)); cannam@147: } else { cannam@147: // Result contained neither a value nor an exception? cannam@147: KJ_UNREACHABLE; cannam@147: } cannam@147: } cannam@147: cannam@147: template cannam@147: ForkedPromise Promise::fork() { cannam@147: return ForkedPromise(false, refcounted<_::ForkHub<_::FixVoid>>(kj::mv(node))); cannam@147: } cannam@147: cannam@147: template cannam@147: Promise ForkedPromise::addBranch() { cannam@147: return hub->addBranch(); cannam@147: } cannam@147: cannam@147: template cannam@147: _::SplitTuplePromise Promise::split() { cannam@147: return refcounted<_::ForkHub<_::FixVoid>>(kj::mv(node))->split(); cannam@147: } cannam@147: cannam@147: template cannam@147: Promise Promise::exclusiveJoin(Promise&& other) { cannam@147: return Promise(false, heap<_::ExclusiveJoinPromiseNode>(kj::mv(node), kj::mv(other.node))); cannam@147: } cannam@147: cannam@147: template cannam@147: template cannam@147: Promise Promise::attach(Attachments&&... attachments) { cannam@147: return Promise(false, kj::heap<_::AttachmentPromiseNode>>( cannam@147: kj::mv(node), kj::tuple(kj::fwd(attachments)...))); cannam@147: } cannam@147: cannam@147: template cannam@147: template cannam@147: Promise Promise::eagerlyEvaluate(ErrorFunc&& errorHandler) { cannam@147: // See catch_() for commentary. cannam@147: return Promise(false, _::spark<_::FixVoid>(then( cannam@147: _::IdentityFunc()))>(), cannam@147: kj::fwd(errorHandler)).node)); cannam@147: } cannam@147: cannam@147: template cannam@147: Promise Promise::eagerlyEvaluate(decltype(nullptr)) { cannam@147: return Promise(false, _::spark<_::FixVoid>(kj::mv(node))); cannam@147: } cannam@147: cannam@147: template cannam@147: kj::String Promise::trace() { cannam@147: return PromiseBase::trace(); cannam@147: } cannam@147: cannam@147: template cannam@147: inline PromiseForResult evalLater(Func&& func) { cannam@147: return _::yield().then(kj::fwd(func), _::PropagateException()); cannam@147: } cannam@147: cannam@147: template cannam@147: inline PromiseForResult evalNow(Func&& func) { cannam@147: PromiseForResult result = nullptr; cannam@147: KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { cannam@147: result = func(); cannam@147: })) { cannam@147: result = kj::mv(*e); cannam@147: } cannam@147: return result; cannam@147: } cannam@147: cannam@147: template cannam@147: template cannam@147: void Promise::detach(ErrorFunc&& errorHandler) { cannam@147: return _::detach(then([](T&&) {}, kj::fwd(errorHandler))); cannam@147: } cannam@147: cannam@147: template <> cannam@147: template cannam@147: void Promise::detach(ErrorFunc&& errorHandler) { cannam@147: return _::detach(then([]() {}, kj::fwd(errorHandler))); cannam@147: } cannam@147: cannam@147: template cannam@147: Promise> joinPromises(Array>&& promises) { cannam@147: return Promise>(false, kj::heap<_::ArrayJoinPromiseNode>( cannam@147: KJ_MAP(p, promises) { return kj::mv(p.node); }, cannam@147: heapArray<_::ExceptionOr>(promises.size()))); cannam@147: } cannam@147: cannam@147: // ======================================================================================= cannam@147: cannam@147: namespace _ { // private cannam@147: cannam@147: template cannam@147: class WeakFulfiller final: public PromiseFulfiller, private kj::Disposer { cannam@147: // A wrapper around PromiseFulfiller which can be detached. cannam@147: // cannam@147: // There are a couple non-trivialities here: cannam@147: // - If the WeakFulfiller is discarded, we want the promise it fulfills to be implicitly cannam@147: // rejected. cannam@147: // - We cannot destroy the WeakFulfiller until the application has discarded it *and* it has been cannam@147: // detached from the underlying fulfiller, because otherwise the later detach() call will go cannam@147: // to a dangling pointer. Essentially, WeakFulfiller is reference counted, although the cannam@147: // refcount never goes over 2 and we manually implement the refcounting because we need to do cannam@147: // other special things when each side detaches anyway. To this end, WeakFulfiller is its own cannam@147: // Disposer -- dispose() is called when the application discards its owned pointer to the cannam@147: // fulfiller and detach() is called when the promise is destroyed. cannam@147: cannam@147: public: cannam@147: KJ_DISALLOW_COPY(WeakFulfiller); cannam@147: cannam@147: static kj::Own make() { cannam@147: WeakFulfiller* ptr = new WeakFulfiller; cannam@147: return Own(ptr, *ptr); cannam@147: } cannam@147: cannam@147: void fulfill(FixVoid&& value) override { cannam@147: if (inner != nullptr) { cannam@147: inner->fulfill(kj::mv(value)); cannam@147: } cannam@147: } cannam@147: cannam@147: void reject(Exception&& exception) override { cannam@147: if (inner != nullptr) { cannam@147: inner->reject(kj::mv(exception)); cannam@147: } cannam@147: } cannam@147: cannam@147: bool isWaiting() override { cannam@147: return inner != nullptr && inner->isWaiting(); cannam@147: } cannam@147: cannam@147: void attach(PromiseFulfiller& newInner) { cannam@147: inner = &newInner; cannam@147: } cannam@147: cannam@147: void detach(PromiseFulfiller& from) { cannam@147: if (inner == nullptr) { cannam@147: // Already disposed. cannam@147: delete this; cannam@147: } else { cannam@147: KJ_IREQUIRE(inner == &from); cannam@147: inner = nullptr; cannam@147: } cannam@147: } cannam@147: cannam@147: private: cannam@147: mutable PromiseFulfiller* inner; cannam@147: cannam@147: WeakFulfiller(): inner(nullptr) {} cannam@147: cannam@147: void disposeImpl(void* pointer) const override { cannam@147: // TODO(perf): Factor some of this out so it isn't regenerated for every fulfiller type? cannam@147: cannam@147: if (inner == nullptr) { cannam@147: // Already detached. cannam@147: delete this; cannam@147: } else { cannam@147: if (inner->isWaiting()) { cannam@147: inner->reject(kj::Exception(kj::Exception::Type::FAILED, __FILE__, __LINE__, cannam@147: kj::heapString("PromiseFulfiller was destroyed without fulfilling the promise."))); cannam@147: } cannam@147: inner = nullptr; cannam@147: } cannam@147: } cannam@147: }; cannam@147: cannam@147: template cannam@147: class PromiseAndFulfillerAdapter { cannam@147: public: cannam@147: PromiseAndFulfillerAdapter(PromiseFulfiller& fulfiller, cannam@147: WeakFulfiller& wrapper) cannam@147: : fulfiller(fulfiller), wrapper(wrapper) { cannam@147: wrapper.attach(fulfiller); cannam@147: } cannam@147: cannam@147: ~PromiseAndFulfillerAdapter() noexcept(false) { cannam@147: wrapper.detach(fulfiller); cannam@147: } cannam@147: cannam@147: private: cannam@147: PromiseFulfiller& fulfiller; cannam@147: WeakFulfiller& wrapper; cannam@147: }; cannam@147: cannam@147: } // namespace _ (private) cannam@147: cannam@147: template cannam@147: template cannam@147: bool PromiseFulfiller::rejectIfThrows(Func&& func) { cannam@147: KJ_IF_MAYBE(exception, kj::runCatchingExceptions(kj::mv(func))) { cannam@147: reject(kj::mv(*exception)); cannam@147: return false; cannam@147: } else { cannam@147: return true; cannam@147: } cannam@147: } cannam@147: cannam@147: template cannam@147: bool PromiseFulfiller::rejectIfThrows(Func&& func) { cannam@147: KJ_IF_MAYBE(exception, kj::runCatchingExceptions(kj::mv(func))) { cannam@147: reject(kj::mv(*exception)); cannam@147: return false; cannam@147: } else { cannam@147: return true; cannam@147: } cannam@147: } cannam@147: cannam@147: template cannam@147: Promise newAdaptedPromise(Params&&... adapterConstructorParams) { cannam@147: return Promise(false, heap<_::AdapterPromiseNode<_::FixVoid, Adapter>>( cannam@147: kj::fwd(adapterConstructorParams)...)); cannam@147: } cannam@147: cannam@147: template cannam@147: PromiseFulfillerPair newPromiseAndFulfiller() { cannam@147: auto wrapper = _::WeakFulfiller::make(); cannam@147: cannam@147: Own<_::PromiseNode> intermediate( cannam@147: heap<_::AdapterPromiseNode<_::FixVoid, _::PromiseAndFulfillerAdapter>>(*wrapper)); cannam@147: Promise<_::JoinPromises> promise(false, cannam@147: _::maybeChain(kj::mv(intermediate), implicitCast(nullptr))); cannam@147: cannam@147: return PromiseFulfillerPair { kj::mv(promise), kj::mv(wrapper) }; cannam@147: } cannam@147: cannam@147: } // namespace kj cannam@147: cannam@147: #endif // KJ_ASYNC_INL_H_