1 //===------- RPCUTils.h - Utilities for building RPC APIs -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Utilities to support construction of simple RPC APIs.
12 // The RPC utilities aim for ease of use (minimal conceptual overhead) for C++
13 // programmers, high performance, low memory overhead, and efficient use of the
14 // communications channel.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_EXECUTIONENGINE_ORC_RPCUTILS_H
19 #define LLVM_EXECUTIONENGINE_ORC_RPCUTILS_H
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ExecutionEngine/Orc/OrcError.h"
27 #include "llvm/ExecutionEngine/Orc/RPCSerialization.h"
35 /// Base class of all fatal RPC errors (those that necessarily result in the
36 /// termination of the RPC session).
37 class RPCFatalError : public ErrorInfo<RPCFatalError> {
42 /// RPCConnectionClosed is returned from RPC operations if the RPC connection
43 /// has already been closed due to either an error or graceful disconnection.
44 class ConnectionClosed : public ErrorInfo<ConnectionClosed> {
47 std::error_code convertToErrorCode() const override;
48 void log(raw_ostream &OS) const override;
51 /// BadFunctionCall is returned from handleOne when the remote makes a call with
52 /// an unrecognized function id.
54 /// This error is fatal because Orc RPC needs to know how to parse a function
55 /// call to know where the next call starts, and if it doesn't recognize the
56 /// function id it cannot parse the call.
57 template <typename FnIdT, typename SeqNoT>
59 : public ErrorInfo<BadFunctionCall<FnIdT, SeqNoT>, RPCFatalError> {
63 BadFunctionCall(FnIdT FnId, SeqNoT SeqNo)
64 : FnId(std::move(FnId)), SeqNo(std::move(SeqNo)) {}
66 std::error_code convertToErrorCode() const override {
67 return orcError(OrcErrorCode::UnexpectedRPCCall);
70 void log(raw_ostream &OS) const override {
71 OS << "Call to invalid RPC function id '" << FnId << "' with "
72 "sequence number " << SeqNo;
80 template <typename FnIdT, typename SeqNoT>
81 char BadFunctionCall<FnIdT, SeqNoT>::ID = 0;
83 /// InvalidSequenceNumberForResponse is returned from handleOne when a response
84 /// call arrives with a sequence number that doesn't correspond to any in-flight
87 /// This error is fatal because Orc RPC needs to know how to parse the rest of
88 /// the response call to know where the next call starts, and if it doesn't have
89 /// a result parser for this sequence number it can't do that.
90 template <typename SeqNoT>
91 class InvalidSequenceNumberForResponse
92 : public ErrorInfo<InvalidSequenceNumberForResponse<SeqNoT>, RPCFatalError> {
96 InvalidSequenceNumberForResponse(SeqNoT SeqNo)
97 : SeqNo(std::move(SeqNo)) {}
99 std::error_code convertToErrorCode() const override {
100 return orcError(OrcErrorCode::UnexpectedRPCCall);
103 void log(raw_ostream &OS) const override {
104 OS << "Response has unknown sequence number " << SeqNo;
110 template <typename SeqNoT>
111 char InvalidSequenceNumberForResponse<SeqNoT>::ID = 0;
113 /// This non-fatal error will be passed to asynchronous result handlers in place
114 /// of a result if the connection goes down before a result returns, or if the
115 /// function to be called cannot be negotiated with the remote.
116 class ResponseAbandoned : public ErrorInfo<ResponseAbandoned> {
120 std::error_code convertToErrorCode() const override;
121 void log(raw_ostream &OS) const override;
124 /// This error is returned if the remote does not have a handler installed for
125 /// the given RPC function.
126 class CouldNotNegotiate : public ErrorInfo<CouldNotNegotiate> {
130 CouldNotNegotiate(std::string Signature);
131 std::error_code convertToErrorCode() const override;
132 void log(raw_ostream &OS) const override;
133 const std::string &getSignature() const { return Signature; }
135 std::string Signature;
138 template <typename DerivedFunc, typename FnT> class Function;
140 // RPC Function class.
141 // DerivedFunc should be a user defined class with a static 'getName()' method
142 // returning a const char* representing the function's name.
143 template <typename DerivedFunc, typename RetT, typename... ArgTs>
144 class Function<DerivedFunc, RetT(ArgTs...)> {
146 /// User defined function type.
147 using Type = RetT(ArgTs...);
150 using ReturnType = RetT;
152 /// Returns the full function prototype as a string.
153 static const char *getPrototype() {
154 std::lock_guard<std::mutex> Lock(NameMutex);
156 raw_string_ostream(Name)
157 << RPCTypeName<RetT>::getName() << " " << DerivedFunc::getName()
158 << "(" << llvm::orc::rpc::RPCTypeNameSequence<ArgTs...>() << ")";
163 static std::mutex NameMutex;
164 static std::string Name;
167 template <typename DerivedFunc, typename RetT, typename... ArgTs>
168 std::mutex Function<DerivedFunc, RetT(ArgTs...)>::NameMutex;
170 template <typename DerivedFunc, typename RetT, typename... ArgTs>
171 std::string Function<DerivedFunc, RetT(ArgTs...)>::Name;
173 /// Allocates RPC function ids during autonegotiation.
174 /// Specializations of this class must provide four members:
176 /// static T getInvalidId():
177 /// Should return a reserved id that will be used to represent missing
178 /// functions during autonegotiation.
180 /// static T getResponseId():
181 /// Should return a reserved id that will be used to send function responses
184 /// static T getNegotiateId():
185 /// Should return a reserved id for the negotiate function, which will be used
186 /// to negotiate ids for user defined functions.
188 /// template <typename Func> T allocate():
189 /// Allocate a unique id for function Func.
190 template <typename T, typename = void> class RPCFunctionIdAllocator;
192 /// This specialization of RPCFunctionIdAllocator provides a default
193 /// implementation for integral types.
194 template <typename T>
195 class RPCFunctionIdAllocator<
196 T, typename std::enable_if<std::is_integral<T>::value>::type> {
198 static T getInvalidId() { return T(0); }
199 static T getResponseId() { return T(1); }
200 static T getNegotiateId() { return T(2); }
202 template <typename Func> T allocate() { return NextId++; }
210 // FIXME: Remove MSVCPError/MSVCPExpected once MSVC's future implementation
211 // supports classes without default constructors.
214 namespace msvc_hacks {
216 // Work around MSVC's future implementation's use of default constructors:
217 // A default constructed value in the promise will be overwritten when the
218 // real error is set - so the default constructed Error has to be checked
220 class MSVCPError : public Error {
222 MSVCPError() { (void)!!*this; }
224 MSVCPError(MSVCPError &&Other) : Error(std::move(Other)) {}
226 MSVCPError &operator=(MSVCPError Other) {
227 Error::operator=(std::move(Other));
231 MSVCPError(Error Err) : Error(std::move(Err)) {}
234 // Work around MSVC's future implementation, similar to MSVCPError.
235 template <typename T> class MSVCPExpected : public Expected<T> {
238 : Expected<T>(make_error<StringError>("", inconvertibleErrorCode())) {
239 consumeError(this->takeError());
242 MSVCPExpected(MSVCPExpected &&Other) : Expected<T>(std::move(Other)) {}
244 MSVCPExpected &operator=(MSVCPExpected &&Other) {
245 Expected<T>::operator=(std::move(Other));
249 MSVCPExpected(Error Err) : Expected<T>(std::move(Err)) {}
251 template <typename OtherT>
254 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
256 : Expected<T>(std::move(Val)) {}
258 template <class OtherT>
260 Expected<OtherT> &&Other,
261 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
263 : Expected<T>(std::move(Other)) {}
265 template <class OtherT>
266 explicit MSVCPExpected(
267 Expected<OtherT> &&Other,
268 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
270 : Expected<T>(std::move(Other)) {}
273 } // end namespace msvc_hacks
277 /// Provides a typedef for a tuple containing the decayed argument types.
278 template <typename T> class FunctionArgsTuple;
280 template <typename RetT, typename... ArgTs>
281 class FunctionArgsTuple<RetT(ArgTs...)> {
283 using Type = std::tuple<typename std::decay<
284 typename std::remove_reference<ArgTs>::type>::type...>;
287 // ResultTraits provides typedefs and utilities specific to the return type
289 template <typename RetT> class ResultTraits {
291 // The return type wrapped in llvm::Expected.
292 using ErrorReturnType = Expected<RetT>;
295 // The ErrorReturnType wrapped in a std::promise.
296 using ReturnPromiseType = std::promise<msvc_hacks::MSVCPExpected<RetT>>;
298 // The ErrorReturnType wrapped in a std::future.
299 using ReturnFutureType = std::future<msvc_hacks::MSVCPExpected<RetT>>;
301 // The ErrorReturnType wrapped in a std::promise.
302 using ReturnPromiseType = std::promise<ErrorReturnType>;
304 // The ErrorReturnType wrapped in a std::future.
305 using ReturnFutureType = std::future<ErrorReturnType>;
308 // Create a 'blank' value of the ErrorReturnType, ready and safe to
310 static ErrorReturnType createBlankErrorReturnValue() {
311 return ErrorReturnType(RetT());
314 // Consume an abandoned ErrorReturnType.
315 static void consumeAbandoned(ErrorReturnType RetOrErr) {
316 consumeError(RetOrErr.takeError());
320 // ResultTraits specialization for void functions.
321 template <> class ResultTraits<void> {
323 // For void functions, ErrorReturnType is llvm::Error.
324 using ErrorReturnType = Error;
327 // The ErrorReturnType wrapped in a std::promise.
328 using ReturnPromiseType = std::promise<msvc_hacks::MSVCPError>;
330 // The ErrorReturnType wrapped in a std::future.
331 using ReturnFutureType = std::future<msvc_hacks::MSVCPError>;
333 // The ErrorReturnType wrapped in a std::promise.
334 using ReturnPromiseType = std::promise<ErrorReturnType>;
336 // The ErrorReturnType wrapped in a std::future.
337 using ReturnFutureType = std::future<ErrorReturnType>;
340 // Create a 'blank' value of the ErrorReturnType, ready and safe to
342 static ErrorReturnType createBlankErrorReturnValue() {
343 return ErrorReturnType::success();
346 // Consume an abandoned ErrorReturnType.
347 static void consumeAbandoned(ErrorReturnType Err) {
348 consumeError(std::move(Err));
352 // ResultTraits<Error> is equivalent to ResultTraits<void>. This allows
353 // handlers for void RPC functions to return either void (in which case they
354 // implicitly succeed) or Error (in which case their error return is
355 // propagated). See usage in HandlerTraits::runHandlerHelper.
356 template <> class ResultTraits<Error> : public ResultTraits<void> {};
358 // ResultTraits<Expected<T>> is equivalent to ResultTraits<T>. This allows
359 // handlers for RPC functions returning a T to return either a T (in which
360 // case they implicitly succeed) or Expected<T> (in which case their error
361 // return is propagated). See usage in HandlerTraits::runHandlerHelper.
362 template <typename RetT>
363 class ResultTraits<Expected<RetT>> : public ResultTraits<RetT> {};
365 // Determines whether an RPC function's defined error return type supports
366 // error return value.
367 template <typename T>
368 class SupportsErrorReturn {
370 static const bool value = false;
374 class SupportsErrorReturn<Error> {
376 static const bool value = true;
379 template <typename T>
380 class SupportsErrorReturn<Expected<T>> {
382 static const bool value = true;
385 // RespondHelper packages return values based on whether or not the declared
386 // RPC function return type supports error returns.
387 template <bool FuncSupportsErrorReturn>
390 // RespondHelper specialization for functions that support error returns.
392 class RespondHelper<true> {
396 template <typename WireRetT, typename HandlerRetT, typename ChannelT,
397 typename FunctionIdT, typename SequenceNumberT>
398 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
399 SequenceNumberT SeqNo,
400 Expected<HandlerRetT> ResultOrErr) {
401 if (!ResultOrErr && ResultOrErr.template errorIsA<RPCFatalError>())
402 return ResultOrErr.takeError();
404 // Open the response message.
405 if (auto Err = C.startSendMessage(ResponseId, SeqNo))
408 // Serialize the result.
410 SerializationTraits<ChannelT, WireRetT,
411 Expected<HandlerRetT>>::serialize(
412 C, std::move(ResultOrErr)))
415 // Close the response message.
416 return C.endSendMessage();
419 template <typename ChannelT, typename FunctionIdT, typename SequenceNumberT>
420 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
421 SequenceNumberT SeqNo, Error Err) {
422 if (Err && Err.isA<RPCFatalError>())
424 if (auto Err2 = C.startSendMessage(ResponseId, SeqNo))
426 if (auto Err2 = serializeSeq(C, std::move(Err)))
428 return C.endSendMessage();
433 // RespondHelper specialization for functions that do not support error returns.
435 class RespondHelper<false> {
438 template <typename WireRetT, typename HandlerRetT, typename ChannelT,
439 typename FunctionIdT, typename SequenceNumberT>
440 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
441 SequenceNumberT SeqNo,
442 Expected<HandlerRetT> ResultOrErr) {
443 if (auto Err = ResultOrErr.takeError())
446 // Open the response message.
447 if (auto Err = C.startSendMessage(ResponseId, SeqNo))
450 // Serialize the result.
452 SerializationTraits<ChannelT, WireRetT, HandlerRetT>::serialize(
456 // Close the response message.
457 return C.endSendMessage();
460 template <typename ChannelT, typename FunctionIdT, typename SequenceNumberT>
461 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
462 SequenceNumberT SeqNo, Error Err) {
465 if (auto Err2 = C.startSendMessage(ResponseId, SeqNo))
467 return C.endSendMessage();
473 // Send a response of the given wire return type (WireRetT) over the
474 // channel, with the given sequence number.
475 template <typename WireRetT, typename HandlerRetT, typename ChannelT,
476 typename FunctionIdT, typename SequenceNumberT>
477 Error respond(ChannelT &C, const FunctionIdT &ResponseId,
478 SequenceNumberT SeqNo, Expected<HandlerRetT> ResultOrErr) {
479 return RespondHelper<SupportsErrorReturn<WireRetT>::value>::
480 template sendResult<WireRetT>(C, ResponseId, SeqNo, std::move(ResultOrErr));
483 // Send an empty response message on the given channel to indicate that
485 template <typename WireRetT, typename ChannelT, typename FunctionIdT,
486 typename SequenceNumberT>
487 Error respond(ChannelT &C, const FunctionIdT &ResponseId, SequenceNumberT SeqNo,
489 return RespondHelper<SupportsErrorReturn<WireRetT>::value>::
490 sendResult(C, ResponseId, SeqNo, std::move(Err));
493 // Converts a given type to the equivalent error return type.
494 template <typename T> class WrappedHandlerReturn {
496 using Type = Expected<T>;
499 template <typename T> class WrappedHandlerReturn<Expected<T>> {
501 using Type = Expected<T>;
504 template <> class WrappedHandlerReturn<void> {
509 template <> class WrappedHandlerReturn<Error> {
514 template <> class WrappedHandlerReturn<ErrorSuccess> {
519 // Traits class that strips the response function from the list of handler
521 template <typename FnT> class AsyncHandlerTraits;
523 template <typename ResultT, typename... ArgTs>
524 class AsyncHandlerTraits<Error(std::function<Error(Expected<ResultT>)>, ArgTs...)> {
526 using Type = Error(ArgTs...);
527 using ResultType = Expected<ResultT>;
530 template <typename... ArgTs>
531 class AsyncHandlerTraits<Error(std::function<Error(Error)>, ArgTs...)> {
533 using Type = Error(ArgTs...);
534 using ResultType = Error;
537 template <typename... ArgTs>
538 class AsyncHandlerTraits<ErrorSuccess(std::function<Error(Error)>, ArgTs...)> {
540 using Type = Error(ArgTs...);
541 using ResultType = Error;
544 template <typename... ArgTs>
545 class AsyncHandlerTraits<void(std::function<Error(Error)>, ArgTs...)> {
547 using Type = Error(ArgTs...);
548 using ResultType = Error;
551 template <typename ResponseHandlerT, typename... ArgTs>
552 class AsyncHandlerTraits<Error(ResponseHandlerT, ArgTs...)> :
553 public AsyncHandlerTraits<Error(typename std::decay<ResponseHandlerT>::type,
556 // This template class provides utilities related to RPC function handlers.
557 // The base case applies to non-function types (the template class is
558 // specialized for function types) and inherits from the appropriate
559 // speciilization for the given non-function type's call operator.
560 template <typename HandlerT>
561 class HandlerTraits : public HandlerTraits<decltype(
562 &std::remove_reference<HandlerT>::type::operator())> {
565 // Traits for handlers with a given function type.
566 template <typename RetT, typename... ArgTs>
567 class HandlerTraits<RetT(ArgTs...)> {
569 // Function type of the handler.
570 using Type = RetT(ArgTs...);
572 // Return type of the handler.
573 using ReturnType = RetT;
575 // Call the given handler with the given arguments.
576 template <typename HandlerT, typename... TArgTs>
577 static typename WrappedHandlerReturn<RetT>::Type
578 unpackAndRun(HandlerT &Handler, std::tuple<TArgTs...> &Args) {
579 return unpackAndRunHelper(Handler, Args,
580 llvm::index_sequence_for<TArgTs...>());
583 // Call the given handler with the given arguments.
584 template <typename HandlerT, typename ResponderT, typename... TArgTs>
585 static Error unpackAndRunAsync(HandlerT &Handler, ResponderT &Responder,
586 std::tuple<TArgTs...> &Args) {
587 return unpackAndRunAsyncHelper(Handler, Responder, Args,
588 llvm::index_sequence_for<TArgTs...>());
591 // Call the given handler with the given arguments.
592 template <typename HandlerT>
593 static typename std::enable_if<
594 std::is_void<typename HandlerTraits<HandlerT>::ReturnType>::value,
596 run(HandlerT &Handler, ArgTs &&... Args) {
597 Handler(std::move(Args)...);
598 return Error::success();
601 template <typename HandlerT, typename... TArgTs>
602 static typename std::enable_if<
603 !std::is_void<typename HandlerTraits<HandlerT>::ReturnType>::value,
604 typename HandlerTraits<HandlerT>::ReturnType>::type
605 run(HandlerT &Handler, TArgTs... Args) {
606 return Handler(std::move(Args)...);
609 // Serialize arguments to the channel.
610 template <typename ChannelT, typename... CArgTs>
611 static Error serializeArgs(ChannelT &C, const CArgTs... CArgs) {
612 return SequenceSerialization<ChannelT, ArgTs...>::serialize(C, CArgs...);
615 // Deserialize arguments from the channel.
616 template <typename ChannelT, typename... CArgTs>
617 static Error deserializeArgs(ChannelT &C, std::tuple<CArgTs...> &Args) {
618 return deserializeArgsHelper(C, Args,
619 llvm::index_sequence_for<CArgTs...>());
623 template <typename ChannelT, typename... CArgTs, size_t... Indexes>
624 static Error deserializeArgsHelper(ChannelT &C, std::tuple<CArgTs...> &Args,
625 llvm::index_sequence<Indexes...> _) {
626 return SequenceSerialization<ChannelT, ArgTs...>::deserialize(
627 C, std::get<Indexes>(Args)...);
630 template <typename HandlerT, typename ArgTuple, size_t... Indexes>
631 static typename WrappedHandlerReturn<
632 typename HandlerTraits<HandlerT>::ReturnType>::Type
633 unpackAndRunHelper(HandlerT &Handler, ArgTuple &Args,
634 llvm::index_sequence<Indexes...>) {
635 return run(Handler, std::move(std::get<Indexes>(Args))...);
639 template <typename HandlerT, typename ResponderT, typename ArgTuple,
641 static typename WrappedHandlerReturn<
642 typename HandlerTraits<HandlerT>::ReturnType>::Type
643 unpackAndRunAsyncHelper(HandlerT &Handler, ResponderT &Responder,
645 llvm::index_sequence<Indexes...>) {
646 return run(Handler, Responder, std::move(std::get<Indexes>(Args))...);
650 // Handler traits for free functions.
651 template <typename RetT, typename... ArgTs>
652 class HandlerTraits<RetT(*)(ArgTs...)>
653 : public HandlerTraits<RetT(ArgTs...)> {};
655 // Handler traits for class methods (especially call operators for lambdas).
656 template <typename Class, typename RetT, typename... ArgTs>
657 class HandlerTraits<RetT (Class::*)(ArgTs...)>
658 : public HandlerTraits<RetT(ArgTs...)> {};
660 // Handler traits for const class methods (especially call operators for
662 template <typename Class, typename RetT, typename... ArgTs>
663 class HandlerTraits<RetT (Class::*)(ArgTs...) const>
664 : public HandlerTraits<RetT(ArgTs...)> {};
666 // Utility to peel the Expected wrapper off a response handler error type.
667 template <typename HandlerT> class ResponseHandlerArg;
669 template <typename ArgT> class ResponseHandlerArg<Error(Expected<ArgT>)> {
671 using ArgType = Expected<ArgT>;
672 using UnwrappedArgType = ArgT;
675 template <typename ArgT>
676 class ResponseHandlerArg<ErrorSuccess(Expected<ArgT>)> {
678 using ArgType = Expected<ArgT>;
679 using UnwrappedArgType = ArgT;
682 template <> class ResponseHandlerArg<Error(Error)> {
684 using ArgType = Error;
687 template <> class ResponseHandlerArg<ErrorSuccess(Error)> {
689 using ArgType = Error;
692 // ResponseHandler represents a handler for a not-yet-received function call
694 template <typename ChannelT> class ResponseHandler {
696 virtual ~ResponseHandler() {}
698 // Reads the function result off the wire and acts on it. The meaning of
699 // "act" will depend on how this method is implemented in any given
700 // ResponseHandler subclass but could, for example, mean running a
701 // user-specified handler or setting a promise value.
702 virtual Error handleResponse(ChannelT &C) = 0;
704 // Abandons this outstanding result.
705 virtual void abandon() = 0;
707 // Create an error instance representing an abandoned response.
708 static Error createAbandonedResponseError() {
709 return make_error<ResponseAbandoned>();
713 // ResponseHandler subclass for RPC functions with non-void returns.
714 template <typename ChannelT, typename FuncRetT, typename HandlerT>
715 class ResponseHandlerImpl : public ResponseHandler<ChannelT> {
717 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
719 // Handle the result by deserializing it from the channel then passing it
720 // to the user defined handler.
721 Error handleResponse(ChannelT &C) override {
722 using UnwrappedArgType = typename ResponseHandlerArg<
723 typename HandlerTraits<HandlerT>::Type>::UnwrappedArgType;
724 UnwrappedArgType Result;
726 SerializationTraits<ChannelT, FuncRetT,
727 UnwrappedArgType>::deserialize(C, Result))
729 if (auto Err = C.endReceiveMessage())
731 return Handler(std::move(Result));
734 // Abandon this response by calling the handler with an 'abandoned response'
736 void abandon() override {
737 if (auto Err = Handler(this->createAbandonedResponseError())) {
738 // Handlers should not fail when passed an abandoned response error.
739 report_fatal_error(std::move(Err));
747 // ResponseHandler subclass for RPC functions with void returns.
748 template <typename ChannelT, typename HandlerT>
749 class ResponseHandlerImpl<ChannelT, void, HandlerT>
750 : public ResponseHandler<ChannelT> {
752 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
754 // Handle the result (no actual value, just a notification that the function
755 // has completed on the remote end) by calling the user-defined handler with
757 Error handleResponse(ChannelT &C) override {
758 if (auto Err = C.endReceiveMessage())
760 return Handler(Error::success());
763 // Abandon this response by calling the handler with an 'abandoned response'
765 void abandon() override {
766 if (auto Err = Handler(this->createAbandonedResponseError())) {
767 // Handlers should not fail when passed an abandoned response error.
768 report_fatal_error(std::move(Err));
776 template <typename ChannelT, typename FuncRetT, typename HandlerT>
777 class ResponseHandlerImpl<ChannelT, Expected<FuncRetT>, HandlerT>
778 : public ResponseHandler<ChannelT> {
780 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
782 // Handle the result by deserializing it from the channel then passing it
783 // to the user defined handler.
784 Error handleResponse(ChannelT &C) override {
785 using HandlerArgType = typename ResponseHandlerArg<
786 typename HandlerTraits<HandlerT>::Type>::ArgType;
787 HandlerArgType Result((typename HandlerArgType::value_type()));
790 SerializationTraits<ChannelT, Expected<FuncRetT>,
791 HandlerArgType>::deserialize(C, Result))
793 if (auto Err = C.endReceiveMessage())
795 return Handler(std::move(Result));
798 // Abandon this response by calling the handler with an 'abandoned response'
800 void abandon() override {
801 if (auto Err = Handler(this->createAbandonedResponseError())) {
802 // Handlers should not fail when passed an abandoned response error.
803 report_fatal_error(std::move(Err));
811 template <typename ChannelT, typename HandlerT>
812 class ResponseHandlerImpl<ChannelT, Error, HandlerT>
813 : public ResponseHandler<ChannelT> {
815 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
817 // Handle the result by deserializing it from the channel then passing it
818 // to the user defined handler.
819 Error handleResponse(ChannelT &C) override {
820 Error Result = Error::success();
822 SerializationTraits<ChannelT, Error, Error>::deserialize(C, Result))
824 if (auto Err = C.endReceiveMessage())
826 return Handler(std::move(Result));
829 // Abandon this response by calling the handler with an 'abandoned response'
831 void abandon() override {
832 if (auto Err = Handler(this->createAbandonedResponseError())) {
833 // Handlers should not fail when passed an abandoned response error.
834 report_fatal_error(std::move(Err));
842 // Create a ResponseHandler from a given user handler.
843 template <typename ChannelT, typename FuncRetT, typename HandlerT>
844 std::unique_ptr<ResponseHandler<ChannelT>> createResponseHandler(HandlerT H) {
845 return llvm::make_unique<ResponseHandlerImpl<ChannelT, FuncRetT, HandlerT>>(
849 // Helper for wrapping member functions up as functors. This is useful for
850 // installing methods as result handlers.
851 template <typename ClassT, typename RetT, typename... ArgTs>
852 class MemberFnWrapper {
854 using MethodT = RetT (ClassT::*)(ArgTs...);
855 MemberFnWrapper(ClassT &Instance, MethodT Method)
856 : Instance(Instance), Method(Method) {}
857 RetT operator()(ArgTs &&... Args) {
858 return (Instance.*Method)(std::move(Args)...);
866 // Helper that provides a Functor for deserializing arguments.
867 template <typename... ArgTs> class ReadArgs {
869 Error operator()() { return Error::success(); }
872 template <typename ArgT, typename... ArgTs>
873 class ReadArgs<ArgT, ArgTs...> : public ReadArgs<ArgTs...> {
875 ReadArgs(ArgT &Arg, ArgTs &... Args)
876 : ReadArgs<ArgTs...>(Args...), Arg(Arg) {}
878 Error operator()(ArgT &ArgVal, ArgTs &... ArgVals) {
879 this->Arg = std::move(ArgVal);
880 return ReadArgs<ArgTs...>::operator()(ArgVals...);
887 // Manage sequence numbers.
888 template <typename SequenceNumberT> class SequenceNumberManager {
890 // Reset, making all sequence numbers available.
892 std::lock_guard<std::mutex> Lock(SeqNoLock);
893 NextSequenceNumber = 0;
894 FreeSequenceNumbers.clear();
897 // Get the next available sequence number. Will re-use numbers that have
899 SequenceNumberT getSequenceNumber() {
900 std::lock_guard<std::mutex> Lock(SeqNoLock);
901 if (FreeSequenceNumbers.empty())
902 return NextSequenceNumber++;
903 auto SequenceNumber = FreeSequenceNumbers.back();
904 FreeSequenceNumbers.pop_back();
905 return SequenceNumber;
908 // Release a sequence number, making it available for re-use.
909 void releaseSequenceNumber(SequenceNumberT SequenceNumber) {
910 std::lock_guard<std::mutex> Lock(SeqNoLock);
911 FreeSequenceNumbers.push_back(SequenceNumber);
915 std::mutex SeqNoLock;
916 SequenceNumberT NextSequenceNumber = 0;
917 std::vector<SequenceNumberT> FreeSequenceNumbers;
920 // Checks that predicate P holds for each corresponding pair of type arguments
921 // from T1 and T2 tuple.
922 template <template <class, class> class P, typename T1Tuple, typename T2Tuple>
923 class RPCArgTypeCheckHelper;
925 template <template <class, class> class P>
926 class RPCArgTypeCheckHelper<P, std::tuple<>, std::tuple<>> {
928 static const bool value = true;
931 template <template <class, class> class P, typename T, typename... Ts,
932 typename U, typename... Us>
933 class RPCArgTypeCheckHelper<P, std::tuple<T, Ts...>, std::tuple<U, Us...>> {
935 static const bool value =
937 RPCArgTypeCheckHelper<P, std::tuple<Ts...>, std::tuple<Us...>>::value;
940 template <template <class, class> class P, typename T1Sig, typename T2Sig>
941 class RPCArgTypeCheck {
943 using T1Tuple = typename FunctionArgsTuple<T1Sig>::Type;
944 using T2Tuple = typename FunctionArgsTuple<T2Sig>::Type;
946 static_assert(std::tuple_size<T1Tuple>::value >=
947 std::tuple_size<T2Tuple>::value,
948 "Too many arguments to RPC call");
949 static_assert(std::tuple_size<T1Tuple>::value <=
950 std::tuple_size<T2Tuple>::value,
951 "Too few arguments to RPC call");
953 static const bool value = RPCArgTypeCheckHelper<P, T1Tuple, T2Tuple>::value;
956 template <typename ChannelT, typename WireT, typename ConcreteT>
959 using S = SerializationTraits<ChannelT, WireT, ConcreteT>;
961 template <typename T>
962 static std::true_type
963 check(typename std::enable_if<
964 std::is_same<decltype(T::serialize(std::declval<ChannelT &>(),
965 std::declval<const ConcreteT &>())),
969 template <typename> static std::false_type check(...);
972 static const bool value = decltype(check<S>(0))::value;
975 template <typename ChannelT, typename WireT, typename ConcreteT>
976 class CanDeserialize {
978 using S = SerializationTraits<ChannelT, WireT, ConcreteT>;
980 template <typename T>
981 static std::true_type
982 check(typename std::enable_if<
983 std::is_same<decltype(T::deserialize(std::declval<ChannelT &>(),
984 std::declval<ConcreteT &>())),
988 template <typename> static std::false_type check(...);
991 static const bool value = decltype(check<S>(0))::value;
994 /// Contains primitive utilities for defining, calling and handling calls to
995 /// remote procedures. ChannelT is a bidirectional stream conforming to the
996 /// RPCChannel interface (see RPCChannel.h), FunctionIdT is a procedure
997 /// identifier type that must be serializable on ChannelT, and SequenceNumberT
998 /// is an integral type that will be used to number in-flight function calls.
1000 /// These utilities support the construction of very primitive RPC utilities.
1001 /// Their intent is to ensure correct serialization and deserialization of
1002 /// procedure arguments, and to keep the client and server's view of the API in
1004 template <typename ImplT, typename ChannelT, typename FunctionIdT,
1005 typename SequenceNumberT>
1006 class RPCEndpointBase {
1008 class OrcRPCInvalid : public Function<OrcRPCInvalid, void()> {
1010 static const char *getName() { return "__orc_rpc$invalid"; }
1013 class OrcRPCResponse : public Function<OrcRPCResponse, void()> {
1015 static const char *getName() { return "__orc_rpc$response"; }
1018 class OrcRPCNegotiate
1019 : public Function<OrcRPCNegotiate, FunctionIdT(std::string)> {
1021 static const char *getName() { return "__orc_rpc$negotiate"; }
1024 // Helper predicate for testing for the presence of SerializeTraits
1026 template <typename WireT, typename ConcreteT>
1027 class CanSerializeCheck : detail::CanSerialize<ChannelT, WireT, ConcreteT> {
1029 using detail::CanSerialize<ChannelT, WireT, ConcreteT>::value;
1031 static_assert(value, "Missing serializer for argument (Can't serialize the "
1032 "first template type argument of CanSerializeCheck "
1033 "from the second)");
1036 // Helper predicate for testing for the presence of SerializeTraits
1038 template <typename WireT, typename ConcreteT>
1039 class CanDeserializeCheck
1040 : detail::CanDeserialize<ChannelT, WireT, ConcreteT> {
1042 using detail::CanDeserialize<ChannelT, WireT, ConcreteT>::value;
1044 static_assert(value, "Missing deserializer for argument (Can't deserialize "
1045 "the second template type argument of "
1046 "CanDeserializeCheck from the first)");
1050 /// Construct an RPC instance on a channel.
1051 RPCEndpointBase(ChannelT &C, bool LazyAutoNegotiation)
1052 : C(C), LazyAutoNegotiation(LazyAutoNegotiation) {
1053 // Hold ResponseId in a special variable, since we expect Response to be
1054 // called relatively frequently, and want to avoid the map lookup.
1055 ResponseId = FnIdAllocator.getResponseId();
1056 RemoteFunctionIds[OrcRPCResponse::getPrototype()] = ResponseId;
1058 // Register the negotiate function id and handler.
1059 auto NegotiateId = FnIdAllocator.getNegotiateId();
1060 RemoteFunctionIds[OrcRPCNegotiate::getPrototype()] = NegotiateId;
1061 Handlers[NegotiateId] = wrapHandler<OrcRPCNegotiate>(
1062 [this](const std::string &Name) { return handleNegotiate(Name); });
1066 /// Negotiate a function id for Func with the other end of the channel.
1067 template <typename Func> Error negotiateFunction(bool Retry = false) {
1068 return getRemoteFunctionId<Func>(true, Retry).takeError();
1071 /// Append a call Func, does not call send on the channel.
1072 /// The first argument specifies a user-defined handler to be run when the
1073 /// function returns. The handler should take an Expected<Func::ReturnType>,
1074 /// or an Error (if Func::ReturnType is void). The handler will be called
1075 /// with an error if the return value is abandoned due to a channel error.
1076 template <typename Func, typename HandlerT, typename... ArgTs>
1077 Error appendCallAsync(HandlerT Handler, const ArgTs &... Args) {
1080 detail::RPCArgTypeCheck<CanSerializeCheck, typename Func::Type,
1081 void(ArgTs...)>::value,
1084 // Look up the function ID.
1086 if (auto FnIdOrErr = getRemoteFunctionId<Func>(LazyAutoNegotiation, false))
1089 // Negotiation failed. Notify the handler then return the negotiate-failed
1091 cantFail(Handler(make_error<ResponseAbandoned>()));
1092 return FnIdOrErr.takeError();
1095 SequenceNumberT SeqNo; // initialized in locked scope below.
1097 // Lock the pending responses map and sequence number manager.
1098 std::lock_guard<std::mutex> Lock(ResponsesMutex);
1100 // Allocate a sequence number.
1101 SeqNo = SequenceNumberMgr.getSequenceNumber();
1102 assert(!PendingResponses.count(SeqNo) &&
1103 "Sequence number already allocated");
1105 // Install the user handler.
1106 PendingResponses[SeqNo] =
1107 detail::createResponseHandler<ChannelT, typename Func::ReturnType>(
1108 std::move(Handler));
1111 // Open the function call message.
1112 if (auto Err = C.startSendMessage(FnId, SeqNo)) {
1113 abandonPendingResponses();
1117 // Serialize the call arguments.
1118 if (auto Err = detail::HandlerTraits<typename Func::Type>::serializeArgs(
1120 abandonPendingResponses();
1124 // Close the function call messagee.
1125 if (auto Err = C.endSendMessage()) {
1126 abandonPendingResponses();
1130 return Error::success();
1133 Error sendAppendedCalls() { return C.send(); };
1135 template <typename Func, typename HandlerT, typename... ArgTs>
1136 Error callAsync(HandlerT Handler, const ArgTs &... Args) {
1137 if (auto Err = appendCallAsync<Func>(std::move(Handler), Args...))
1142 /// Handle one incoming call.
1145 SequenceNumberT SeqNo;
1146 if (auto Err = C.startReceiveMessage(FnId, SeqNo)) {
1147 abandonPendingResponses();
1150 if (FnId == ResponseId)
1151 return handleResponse(SeqNo);
1152 auto I = Handlers.find(FnId);
1153 if (I != Handlers.end())
1154 return I->second(C, SeqNo);
1156 // else: No handler found. Report error to client?
1157 return make_error<BadFunctionCall<FunctionIdT, SequenceNumberT>>(FnId,
1161 /// Helper for handling setter procedures - this method returns a functor that
1162 /// sets the variables referred to by Args... to values deserialized from the
1166 /// typedef Function<0, bool, int> Func1;
1171 /// if (auto Err = expect<Func1>(Channel, readArgs(B, I)))
1172 /// /* Handle Args */ ;
1174 template <typename... ArgTs>
1175 static detail::ReadArgs<ArgTs...> readArgs(ArgTs &... Args) {
1176 return detail::ReadArgs<ArgTs...>(Args...);
1179 /// Abandon all outstanding result handlers.
1181 /// This will call all currently registered result handlers to receive an
1182 /// "abandoned" error as their argument. This is used internally by the RPC
1183 /// in error situations, but can also be called directly by clients who are
1184 /// disconnecting from the remote and don't or can't expect responses to their
1185 /// outstanding calls. (Especially for outstanding blocking calls, calling
1186 /// this function may be necessary to avoid dead threads).
1187 void abandonPendingResponses() {
1188 // Lock the pending responses map and sequence number manager.
1189 std::lock_guard<std::mutex> Lock(ResponsesMutex);
1191 for (auto &KV : PendingResponses)
1192 KV.second->abandon();
1193 PendingResponses.clear();
1194 SequenceNumberMgr.reset();
1197 /// Remove the handler for the given function.
1198 /// A handler must currently be registered for this function.
1199 template <typename Func>
1200 void removeHandler() {
1201 auto IdItr = LocalFunctionIds.find(Func::getPrototype());
1202 assert(IdItr != LocalFunctionIds.end() &&
1203 "Function does not have a registered handler");
1204 auto HandlerItr = Handlers.find(IdItr->second);
1205 assert(HandlerItr != Handlers.end() &&
1206 "Function does not have a registered handler");
1207 Handlers.erase(HandlerItr);
1210 /// Clear all handlers.
1211 void clearHandlers() {
1217 FunctionIdT getInvalidFunctionId() const {
1218 return FnIdAllocator.getInvalidId();
1221 /// Add the given handler to the handler map and make it available for
1222 /// autonegotiation and execution.
1223 template <typename Func, typename HandlerT>
1224 void addHandlerImpl(HandlerT Handler) {
1226 static_assert(detail::RPCArgTypeCheck<
1227 CanDeserializeCheck, typename Func::Type,
1228 typename detail::HandlerTraits<HandlerT>::Type>::value,
1231 FunctionIdT NewFnId = FnIdAllocator.template allocate<Func>();
1232 LocalFunctionIds[Func::getPrototype()] = NewFnId;
1233 Handlers[NewFnId] = wrapHandler<Func>(std::move(Handler));
1236 template <typename Func, typename HandlerT>
1237 void addAsyncHandlerImpl(HandlerT Handler) {
1239 static_assert(detail::RPCArgTypeCheck<
1240 CanDeserializeCheck, typename Func::Type,
1241 typename detail::AsyncHandlerTraits<
1242 typename detail::HandlerTraits<HandlerT>::Type
1246 FunctionIdT NewFnId = FnIdAllocator.template allocate<Func>();
1247 LocalFunctionIds[Func::getPrototype()] = NewFnId;
1248 Handlers[NewFnId] = wrapAsyncHandler<Func>(std::move(Handler));
1251 Error handleResponse(SequenceNumberT SeqNo) {
1252 using Handler = typename decltype(PendingResponses)::mapped_type;
1256 // Lock the pending responses map and sequence number manager.
1257 std::unique_lock<std::mutex> Lock(ResponsesMutex);
1258 auto I = PendingResponses.find(SeqNo);
1260 if (I != PendingResponses.end()) {
1261 PRHandler = std::move(I->second);
1262 PendingResponses.erase(I);
1263 SequenceNumberMgr.releaseSequenceNumber(SeqNo);
1265 // Unlock the pending results map to prevent recursive lock.
1267 abandonPendingResponses();
1269 InvalidSequenceNumberForResponse<SequenceNumberT>>(SeqNo);
1274 "If we didn't find a response handler we should have bailed out");
1276 if (auto Err = PRHandler->handleResponse(C)) {
1277 abandonPendingResponses();
1281 return Error::success();
1284 FunctionIdT handleNegotiate(const std::string &Name) {
1285 auto I = LocalFunctionIds.find(Name);
1286 if (I == LocalFunctionIds.end())
1287 return getInvalidFunctionId();
1291 // Find the remote FunctionId for the given function.
1292 template <typename Func>
1293 Expected<FunctionIdT> getRemoteFunctionId(bool NegotiateIfNotInMap,
1294 bool NegotiateIfInvalid) {
1297 // Check if we already have a function id...
1298 auto I = RemoteFunctionIds.find(Func::getPrototype());
1299 if (I != RemoteFunctionIds.end()) {
1300 // If it's valid there's nothing left to do.
1301 if (I->second != getInvalidFunctionId())
1303 DoNegotiate = NegotiateIfInvalid;
1305 DoNegotiate = NegotiateIfNotInMap;
1307 // We don't have a function id for Func yet, but we're allowed to try to
1310 auto &Impl = static_cast<ImplT &>(*this);
1311 if (auto RemoteIdOrErr =
1312 Impl.template callB<OrcRPCNegotiate>(Func::getPrototype())) {
1313 RemoteFunctionIds[Func::getPrototype()] = *RemoteIdOrErr;
1314 if (*RemoteIdOrErr == getInvalidFunctionId())
1315 return make_error<CouldNotNegotiate>(Func::getPrototype());
1316 return *RemoteIdOrErr;
1318 return RemoteIdOrErr.takeError();
1321 // No key was available in the map and we weren't allowed to try to
1322 // negotiate one, so return an unknown function error.
1323 return make_error<CouldNotNegotiate>(Func::getPrototype());
1326 using WrappedHandlerFn = std::function<Error(ChannelT &, SequenceNumberT)>;
1328 // Wrap the given user handler in the necessary argument-deserialization code,
1329 // result-serialization code, and call to the launch policy (if present).
1330 template <typename Func, typename HandlerT>
1331 WrappedHandlerFn wrapHandler(HandlerT Handler) {
1332 return [this, Handler](ChannelT &Channel,
1333 SequenceNumberT SeqNo) mutable -> Error {
1334 // Start by deserializing the arguments.
1336 typename detail::FunctionArgsTuple<
1337 typename detail::HandlerTraits<HandlerT>::Type>::Type;
1338 auto Args = std::make_shared<ArgsTuple>();
1341 detail::HandlerTraits<typename Func::Type>::deserializeArgs(
1345 // GCC 4.7 and 4.8 incorrectly issue a -Wunused-but-set-variable warning
1346 // for RPCArgs. Void cast RPCArgs to work around this for now.
1347 // FIXME: Remove this workaround once we can assume a working GCC version.
1350 // End receieve message, unlocking the channel for reading.
1351 if (auto Err = Channel.endReceiveMessage())
1354 using HTraits = detail::HandlerTraits<HandlerT>;
1355 using FuncReturn = typename Func::ReturnType;
1356 return detail::respond<FuncReturn>(Channel, ResponseId, SeqNo,
1357 HTraits::unpackAndRun(Handler, *Args));
1361 // Wrap the given user handler in the necessary argument-deserialization code,
1362 // result-serialization code, and call to the launch policy (if present).
1363 template <typename Func, typename HandlerT>
1364 WrappedHandlerFn wrapAsyncHandler(HandlerT Handler) {
1365 return [this, Handler](ChannelT &Channel,
1366 SequenceNumberT SeqNo) mutable -> Error {
1367 // Start by deserializing the arguments.
1368 using AHTraits = detail::AsyncHandlerTraits<
1369 typename detail::HandlerTraits<HandlerT>::Type>;
1371 typename detail::FunctionArgsTuple<typename AHTraits::Type>::Type;
1372 auto Args = std::make_shared<ArgsTuple>();
1375 detail::HandlerTraits<typename Func::Type>::deserializeArgs(
1379 // GCC 4.7 and 4.8 incorrectly issue a -Wunused-but-set-variable warning
1380 // for RPCArgs. Void cast RPCArgs to work around this for now.
1381 // FIXME: Remove this workaround once we can assume a working GCC version.
1384 // End receieve message, unlocking the channel for reading.
1385 if (auto Err = Channel.endReceiveMessage())
1388 using HTraits = detail::HandlerTraits<HandlerT>;
1389 using FuncReturn = typename Func::ReturnType;
1391 [this, SeqNo](typename AHTraits::ResultType RetVal) -> Error {
1392 return detail::respond<FuncReturn>(C, ResponseId, SeqNo,
1396 return HTraits::unpackAndRunAsync(Handler, Responder, *Args);
1402 bool LazyAutoNegotiation;
1404 RPCFunctionIdAllocator<FunctionIdT> FnIdAllocator;
1406 FunctionIdT ResponseId;
1407 std::map<std::string, FunctionIdT> LocalFunctionIds;
1408 std::map<const char *, FunctionIdT> RemoteFunctionIds;
1410 std::map<FunctionIdT, WrappedHandlerFn> Handlers;
1412 std::mutex ResponsesMutex;
1413 detail::SequenceNumberManager<SequenceNumberT> SequenceNumberMgr;
1414 std::map<SequenceNumberT, std::unique_ptr<detail::ResponseHandler<ChannelT>>>
1418 } // end namespace detail
1420 template <typename ChannelT, typename FunctionIdT = uint32_t,
1421 typename SequenceNumberT = uint32_t>
1422 class MultiThreadedRPCEndpoint
1423 : public detail::RPCEndpointBase<
1424 MultiThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1425 ChannelT, FunctionIdT, SequenceNumberT> {
1428 detail::RPCEndpointBase<
1429 MultiThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1430 ChannelT, FunctionIdT, SequenceNumberT>;
1433 MultiThreadedRPCEndpoint(ChannelT &C, bool LazyAutoNegotiation)
1434 : BaseClass(C, LazyAutoNegotiation) {}
1436 /// Add a handler for the given RPC function.
1437 /// This installs the given handler functor for the given RPC Function, and
1438 /// makes the RPC function available for negotiation/calling from the remote.
1439 template <typename Func, typename HandlerT>
1440 void addHandler(HandlerT Handler) {
1441 return this->template addHandlerImpl<Func>(std::move(Handler));
1444 /// Add a class-method as a handler.
1445 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1446 void addHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1448 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1451 template <typename Func, typename HandlerT>
1452 void addAsyncHandler(HandlerT Handler) {
1453 return this->template addAsyncHandlerImpl<Func>(std::move(Handler));
1456 /// Add a class-method as a handler.
1457 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1458 void addAsyncHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1459 addAsyncHandler<Func>(
1460 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1463 /// Return type for non-blocking call primitives.
1464 template <typename Func>
1465 using NonBlockingCallResult = typename detail::ResultTraits<
1466 typename Func::ReturnType>::ReturnFutureType;
1468 /// Call Func on Channel C. Does not block, does not call send. Returns a pair
1469 /// of a future result and the sequence number assigned to the result.
1471 /// This utility function is primarily used for single-threaded mode support,
1472 /// where the sequence number can be used to wait for the corresponding
1473 /// result. In multi-threaded mode the appendCallNB method, which does not
1474 /// return the sequence numeber, should be preferred.
1475 template <typename Func, typename... ArgTs>
1476 Expected<NonBlockingCallResult<Func>> appendCallNB(const ArgTs &... Args) {
1477 using RTraits = detail::ResultTraits<typename Func::ReturnType>;
1478 using ErrorReturn = typename RTraits::ErrorReturnType;
1479 using ErrorReturnPromise = typename RTraits::ReturnPromiseType;
1481 // FIXME: Stack allocate and move this into the handler once LLVM builds
1483 auto Promise = std::make_shared<ErrorReturnPromise>();
1484 auto FutureResult = Promise->get_future();
1486 if (auto Err = this->template appendCallAsync<Func>(
1487 [Promise](ErrorReturn RetOrErr) {
1488 Promise->set_value(std::move(RetOrErr));
1489 return Error::success();
1492 RTraits::consumeAbandoned(FutureResult.get());
1493 return std::move(Err);
1495 return std::move(FutureResult);
1498 /// The same as appendCallNBWithSeq, except that it calls C.send() to
1499 /// flush the channel after serializing the call.
1500 template <typename Func, typename... ArgTs>
1501 Expected<NonBlockingCallResult<Func>> callNB(const ArgTs &... Args) {
1502 auto Result = appendCallNB<Func>(Args...);
1505 if (auto Err = this->C.send()) {
1506 this->abandonPendingResponses();
1507 detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
1508 std::move(Result->get()));
1509 return std::move(Err);
1514 /// Call Func on Channel C. Blocks waiting for a result. Returns an Error
1515 /// for void functions or an Expected<T> for functions returning a T.
1517 /// This function is for use in threaded code where another thread is
1518 /// handling responses and incoming calls.
1519 template <typename Func, typename... ArgTs,
1520 typename AltRetT = typename Func::ReturnType>
1521 typename detail::ResultTraits<AltRetT>::ErrorReturnType
1522 callB(const ArgTs &... Args) {
1523 if (auto FutureResOrErr = callNB<Func>(Args...))
1524 return FutureResOrErr->get();
1526 return FutureResOrErr.takeError();
1529 /// Handle incoming RPC calls.
1530 Error handlerLoop() {
1532 if (auto Err = this->handleOne())
1534 return Error::success();
1538 template <typename ChannelT, typename FunctionIdT = uint32_t,
1539 typename SequenceNumberT = uint32_t>
1540 class SingleThreadedRPCEndpoint
1541 : public detail::RPCEndpointBase<
1542 SingleThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1543 ChannelT, FunctionIdT, SequenceNumberT> {
1546 detail::RPCEndpointBase<
1547 SingleThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1548 ChannelT, FunctionIdT, SequenceNumberT>;
1551 SingleThreadedRPCEndpoint(ChannelT &C, bool LazyAutoNegotiation)
1552 : BaseClass(C, LazyAutoNegotiation) {}
1554 template <typename Func, typename HandlerT>
1555 void addHandler(HandlerT Handler) {
1556 return this->template addHandlerImpl<Func>(std::move(Handler));
1559 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1560 void addHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1562 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1565 template <typename Func, typename HandlerT>
1566 void addAsyncHandler(HandlerT Handler) {
1567 return this->template addAsyncHandlerImpl<Func>(std::move(Handler));
1570 /// Add a class-method as a handler.
1571 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1572 void addAsyncHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1573 addAsyncHandler<Func>(
1574 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1577 template <typename Func, typename... ArgTs,
1578 typename AltRetT = typename Func::ReturnType>
1579 typename detail::ResultTraits<AltRetT>::ErrorReturnType
1580 callB(const ArgTs &... Args) {
1581 bool ReceivedResponse = false;
1582 using ResultType = typename detail::ResultTraits<AltRetT>::ErrorReturnType;
1583 auto Result = detail::ResultTraits<AltRetT>::createBlankErrorReturnValue();
1585 // We have to 'Check' result (which we know is in a success state at this
1586 // point) so that it can be overwritten in the async handler.
1589 if (auto Err = this->template appendCallAsync<Func>(
1591 Result = std::move(R);
1592 ReceivedResponse = true;
1593 return Error::success();
1596 detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
1598 return std::move(Err);
1601 while (!ReceivedResponse) {
1602 if (auto Err = this->handleOne()) {
1603 detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
1605 return std::move(Err);
1613 /// Asynchronous dispatch for a function on an RPC endpoint.
1614 template <typename RPCClass, typename Func>
1615 class RPCAsyncDispatch {
1617 RPCAsyncDispatch(RPCClass &Endpoint) : Endpoint(Endpoint) {}
1619 template <typename HandlerT, typename... ArgTs>
1620 Error operator()(HandlerT Handler, const ArgTs &... Args) const {
1621 return Endpoint.template appendCallAsync<Func>(std::move(Handler), Args...);
1628 /// Construct an asynchronous dispatcher from an RPC endpoint and a Func.
1629 template <typename Func, typename RPCEndpointT>
1630 RPCAsyncDispatch<RPCEndpointT, Func> rpcAsyncDispatch(RPCEndpointT &Endpoint) {
1631 return RPCAsyncDispatch<RPCEndpointT, Func>(Endpoint);
1634 /// Allows a set of asynchrounous calls to be dispatched, and then
1635 /// waited on as a group.
1636 class ParallelCallGroup {
1639 ParallelCallGroup() = default;
1640 ParallelCallGroup(const ParallelCallGroup &) = delete;
1641 ParallelCallGroup &operator=(const ParallelCallGroup &) = delete;
1643 /// Make as asynchronous call.
1644 template <typename AsyncDispatcher, typename HandlerT, typename... ArgTs>
1645 Error call(const AsyncDispatcher &AsyncDispatch, HandlerT Handler,
1646 const ArgTs &... Args) {
1647 // Increment the count of outstanding calls. This has to happen before
1648 // we invoke the call, as the handler may (depending on scheduling)
1649 // be run immediately on another thread, and we don't want the decrement
1650 // in the wrapped handler below to run before the increment.
1652 std::unique_lock<std::mutex> Lock(M);
1653 ++NumOutstandingCalls;
1656 // Wrap the user handler in a lambda that will decrement the
1657 // outstanding calls count, then poke the condition variable.
1658 using ArgType = typename detail::ResponseHandlerArg<
1659 typename detail::HandlerTraits<HandlerT>::Type>::ArgType;
1660 // FIXME: Move handler into wrapped handler once we have C++14.
1661 auto WrappedHandler = [this, Handler](ArgType Arg) {
1662 auto Err = Handler(std::move(Arg));
1663 std::unique_lock<std::mutex> Lock(M);
1664 --NumOutstandingCalls;
1669 return AsyncDispatch(std::move(WrappedHandler), Args...);
1672 /// Blocks until all calls have been completed and their return value
1675 std::unique_lock<std::mutex> Lock(M);
1676 while (NumOutstandingCalls > 0)
1682 std::condition_variable CV;
1683 uint32_t NumOutstandingCalls = 0;
1686 /// Convenience class for grouping RPC Functions into APIs that can be
1687 /// negotiated as a block.
1689 template <typename... Funcs>
1693 /// Test whether this API contains Function F.
1694 template <typename F>
1697 static const bool value = false;
1700 /// Negotiate all functions in this API.
1701 template <typename RPCEndpoint>
1702 static Error negotiate(RPCEndpoint &R) {
1703 return Error::success();
1707 template <typename Func, typename... Funcs>
1708 class APICalls<Func, Funcs...> {
1711 template <typename F>
1714 static const bool value = std::is_same<F, Func>::value |
1715 APICalls<Funcs...>::template Contains<F>::value;
1718 template <typename RPCEndpoint>
1719 static Error negotiate(RPCEndpoint &R) {
1720 if (auto Err = R.template negotiateFunction<Func>())
1722 return APICalls<Funcs...>::negotiate(R);
1727 template <typename... InnerFuncs, typename... Funcs>
1728 class APICalls<APICalls<InnerFuncs...>, Funcs...> {
1731 template <typename F>
1734 static const bool value =
1735 APICalls<InnerFuncs...>::template Contains<F>::value |
1736 APICalls<Funcs...>::template Contains<F>::value;
1739 template <typename RPCEndpoint>
1740 static Error negotiate(RPCEndpoint &R) {
1741 if (auto Err = APICalls<InnerFuncs...>::negotiate(R))
1743 return APICalls<Funcs...>::negotiate(R);
1748 } // end namespace rpc
1749 } // end namespace orc
1750 } // end namespace llvm