void SerialExecutor::add(Func func) {
  {
    std::lock_guard<std::mutex> lock(mutex_);
    queue_.push(std::move(func));
  }
  parent_->add([keepAlive = getKeepAliveToken(this)] { keepAlive->run(); });
}
void SerialExecutor::addWithPriority(Func func, int8_t priority) {
  {
    std::lock_guard<std::mutex> lock(mutex_);
    queue_.push(std::move(func));
  }
  parent_->addWithPriority(
      [keepAlive = getKeepAliveToken(this)] { keepAlive->run(); }, priority);
}
Esempio n. 3
0
uint32_t PooledRequestChannel::sendRequestImpl(
    RpcKind rpcKind,
    RpcOptions& options,
    std::unique_ptr<RequestCallback> cob,
    std::unique_ptr<ContextStack> ctx,
    std::unique_ptr<folly::IOBuf> buf,
    std::shared_ptr<transport::THeader> header) {
  auto executor = executor_.lock();
  if (!executor) {
    throw std::logic_error("IO executor already destroyed.");
  }
  auto evb = executor->getEventBase();

  evb->runInEventBaseThread([this,
                             keepAlive = getKeepAliveToken(evb),
                             options = std::move(options),
                             rpcKind,
                             cob = std::move(cob),
                             ctx = std::move(ctx),
                             buf = std::move(buf),
                             header = std::move(header)]() mutable {
    switch (rpcKind) {
      case RpcKind::SINGLE_REQUEST_NO_RESPONSE:
        impl(*keepAlive)
            .sendOnewayRequest(
                options,
                std::move(cob),
                std::move(ctx),
                std::move(buf),
                std::move(header));
        break;
      case RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE:
        impl(*keepAlive)
            .sendRequest(
                options,
                std::move(cob),
                std::move(ctx),
                std::move(buf),
                std::move(header));
        break;
      case RpcKind::SINGLE_REQUEST_STREAMING_RESPONSE:
        impl(*keepAlive)
            .sendStreamRequest(
                options,
                std::move(cob),
                std::move(ctx),
                std::move(buf),
                std::move(header));
        break;
      default:
        folly::assume_unreachable();
        break;
    };
  });
  return 0;
}
Esempio n. 4
0
uint32_t PooledRequestChannel::sendStreamRequest(
    RpcOptions& options,
    std::unique_ptr<RequestCallback> cob,
    std::unique_ptr<ContextStack> ctx,
    std::unique_ptr<folly::IOBuf> buf,
    std::shared_ptr<transport::THeader> header) {
  if (!dynamic_cast<SemiFutureCallback*>(cob.get())) {
    cob = std::make_unique<ExecutorRequestCallback>(
        std::move(cob), getKeepAliveToken(callbackExecutor_));
  }
  sendRequestImpl(
      RpcKind::SINGLE_REQUEST_STREAMING_RESPONSE,
      options,
      std::move(cob),
      std::move(ctx),
      std::move(buf),
      std::move(header));
  return 0;
}
Esempio n. 5
0
VirtualEventBase::VirtualEventBase(EventBase& evb)
    : evb_(getKeepAliveToken(evb)) {}
SerialExecutor::UniquePtr SerialExecutor::createUnique(
    std::shared_ptr<Executor> parent) {
  auto executor = new SerialExecutor(getKeepAliveToken(parent.get()));
  return {executor, Deleter{std::move(parent)}};
}