2025-10-09 14:27:10 +11:00
|
|
|
//===- Session.cpp --------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// Contains the implementation of the Session class and related APIs.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "orc-rt/Session.h"
|
|
|
|
|
|
|
|
|
|
namespace orc_rt {
|
|
|
|
|
|
|
|
|
|
Session::~Session() { waitForShutdown(); }
|
|
|
|
|
|
|
|
|
|
void Session::shutdown(OnShutdownCompleteFn OnShutdownComplete) {
|
|
|
|
|
std::vector<std::unique_ptr<ResourceManager>> ToShutdown;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::scoped_lock<std::mutex> Lock(M);
|
2025-11-19 09:50:46 +11:00
|
|
|
ShutdownCallbacks.push_back(std::move(OnShutdownComplete));
|
|
|
|
|
|
|
|
|
|
// If somebody else has already called shutdown then there's nothing further
|
|
|
|
|
// for us to do here.
|
|
|
|
|
if (State >= SessionState::ShuttingDown)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
State = SessionState::ShuttingDown;
|
2025-10-09 14:27:10 +11:00
|
|
|
std::swap(ResourceMgrs, ToShutdown);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 09:50:46 +11:00
|
|
|
shutdownNext(Error::success(), std::move(ToShutdown));
|
2025-10-09 14:27:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::waitForShutdown() {
|
2025-11-19 09:50:46 +11:00
|
|
|
shutdown([]() {});
|
|
|
|
|
std::unique_lock<std::mutex> Lock(M);
|
|
|
|
|
StateCV.wait(Lock, [&]() { return State == SessionState::Shutdown; });
|
2025-10-09 14:27:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::shutdownNext(
|
2025-11-19 09:50:46 +11:00
|
|
|
Error Err, std::vector<std::unique_ptr<ResourceManager>> RemainingRMs) {
|
2025-10-09 14:27:10 +11:00
|
|
|
if (Err)
|
|
|
|
|
reportError(std::move(Err));
|
|
|
|
|
|
|
|
|
|
if (RemainingRMs.empty())
|
2025-11-19 09:50:46 +11:00
|
|
|
return shutdownComplete();
|
2025-10-09 14:27:10 +11:00
|
|
|
|
|
|
|
|
auto NextRM = std::move(RemainingRMs.back());
|
|
|
|
|
RemainingRMs.pop_back();
|
2025-11-19 09:50:46 +11:00
|
|
|
NextRM->shutdown(
|
|
|
|
|
[this, RemainingRMs = std::move(RemainingRMs)](Error Err) mutable {
|
|
|
|
|
shutdownNext(std::move(Err), std::move(RemainingRMs));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::shutdownComplete() {
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<TaskDispatcher> TmpDispatcher;
|
|
|
|
|
std::vector<OnShutdownCompleteFn> TmpShutdownCallbacks;
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> Lock(M);
|
|
|
|
|
TmpDispatcher = std::move(Dispatcher);
|
|
|
|
|
TmpShutdownCallbacks = std::move(ShutdownCallbacks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TmpDispatcher->shutdown();
|
|
|
|
|
|
|
|
|
|
for (auto &OnShutdownComplete : TmpShutdownCallbacks)
|
|
|
|
|
OnShutdownComplete();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> Lock(M);
|
|
|
|
|
State = SessionState::Shutdown;
|
|
|
|
|
}
|
|
|
|
|
StateCV.notify_all();
|
2025-10-09 14:27:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace orc_rt
|