brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 1123a31 Raw
107 lines · cpp
1//===- Session.cpp --------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Contains the implementation of the Session class and related APIs.10//11//===----------------------------------------------------------------------===//12 13#include "orc-rt/Session.h"14 15namespace orc_rt {16 17Session::ControllerAccess::~ControllerAccess() = default;18 19Session::~Session() { waitForShutdown(); }20 21void Session::shutdown(OnShutdownCompleteFn OnShutdownComplete) {22  // Safe to call concurrently / redundantly.23  detachFromController();24 25  {26    std::scoped_lock<std::mutex> Lock(M);27    if (SI) {28      SI->OnCompletes.push_back(std::move(OnShutdownComplete));29      return;30    }31 32    SI = std::make_unique<ShutdownInfo>();33    SI->OnCompletes.push_back(std::move(OnShutdownComplete));34    std::swap(SI->ResourceMgrs, ResourceMgrs);35  }36 37  shutdownNext(Error::success());38}39 40void Session::waitForShutdown() {41  shutdown([]() {});42  std::unique_lock<std::mutex> Lock(M);43  SI->CompleteCV.wait(Lock, [&]() { return SI->Complete; });44}45 46void Session::addResourceManager(std::unique_ptr<ResourceManager> RM) {47  std::scoped_lock<std::mutex> Lock(M);48  assert(!SI && "addResourceManager called after shutdown");49  ResourceMgrs.push_back(std::move(RM));50}51 52void Session::setController(std::shared_ptr<ControllerAccess> CA) {53  assert(CA && "Cannot attach null controller");54  std::scoped_lock<std::mutex> Lock(M);55  assert(!this->CA && "Cannot re-attach controller");56  assert(!SI && "Cannot attach controller after shutdown");57  this->CA = std::move(CA);58}59 60void Session::detachFromController() {61  if (auto TmpCA = CA) {62    TmpCA->doDisconnect();63    CA = nullptr;64  }65}66 67void Session::shutdownNext(Error Err) {68  if (Err)69    reportError(std::move(Err));70 71  if (SI->ResourceMgrs.empty())72    return shutdownComplete();73 74  // Get the next ResourceManager to shut down.75  auto NextRM = std::move(SI->ResourceMgrs.back());76  SI->ResourceMgrs.pop_back();77  NextRM->shutdown([this](Error Err) { shutdownNext(std::move(Err)); });78}79 80void Session::shutdownComplete() {81 82  std::unique_ptr<TaskDispatcher> TmpDispatcher;83  {84    std::lock_guard<std::mutex> Lock(M);85    TmpDispatcher = std::move(Dispatcher);86  }87 88  TmpDispatcher->shutdown();89 90  for (auto &OnShutdownComplete : SI->OnCompletes)91    OnShutdownComplete();92 93  {94    std::lock_guard<std::mutex> Lock(M);95    SI->Complete = true;96  }97 98  SI->CompleteCV.notify_all();99}100 101void Session::wrapperReturn(orc_rt_SessionRef S, uint64_t CallId,102                            orc_rt_WrapperFunctionBuffer ResultBytes) {103  unwrap(S)->sendWrapperResult(CallId, ResultBytes);104}105 106} // namespace orc_rt107