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::scoped_lock<std::mutex> Lock(M);
|
2025-11-19 16:36:30 +11:00
|
|
|
if (SI) {
|
|
|
|
|
SI->OnCompletes.push_back(std::move(OnShutdownComplete));
|
2025-11-19 09:50:46 +11:00
|
|
|
return;
|
2025-11-19 16:36:30 +11:00
|
|
|
}
|
2025-11-19 09:50:46 +11:00
|
|
|
|
2025-11-19 16:36:30 +11:00
|
|
|
SI = std::make_unique<ShutdownInfo>();
|
|
|
|
|
SI->OnCompletes.push_back(std::move(OnShutdownComplete));
|
|
|
|
|
std::swap(SI->ResourceMgrs, ResourceMgrs);
|
2025-10-09 14:27:10 +11:00
|
|
|
}
|
|
|
|
|
|
2025-11-19 16:36:30 +11:00
|
|
|
shutdownNext(Error::success());
|
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);
|
2025-11-19 16:36:30 +11:00
|
|
|
SI->CompleteCV.wait(Lock, [&]() { return SI->Complete; });
|
2025-10-09 14:27:10 +11:00
|
|
|
}
|
|
|
|
|
|
2025-11-19 16:36:30 +11:00
|
|
|
void Session::shutdownNext(Error Err) {
|
2025-10-09 14:27:10 +11:00
|
|
|
if (Err)
|
|
|
|
|
reportError(std::move(Err));
|
|
|
|
|
|
2025-11-19 16:36:30 +11:00
|
|
|
if (SI->ResourceMgrs.empty())
|
2025-11-19 09:50:46 +11:00
|
|
|
return shutdownComplete();
|
2025-10-09 14:27:10 +11:00
|
|
|
|
2025-11-19 16:36:30 +11:00
|
|
|
// Get the next ResourceManager to shut down.
|
|
|
|
|
auto NextRM = std::move(SI->ResourceMgrs.back());
|
|
|
|
|
SI->ResourceMgrs.pop_back();
|
|
|
|
|
NextRM->shutdown([this](Error Err) { shutdownNext(std::move(Err)); });
|
2025-11-19 09:50:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Session::shutdownComplete() {
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<TaskDispatcher> TmpDispatcher;
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> Lock(M);
|
|
|
|
|
TmpDispatcher = std::move(Dispatcher);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TmpDispatcher->shutdown();
|
|
|
|
|
|
2025-11-19 16:36:30 +11:00
|
|
|
for (auto &OnShutdownComplete : SI->OnCompletes)
|
2025-11-19 09:50:46 +11:00
|
|
|
OnShutdownComplete();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> Lock(M);
|
2025-11-19 16:36:30 +11:00
|
|
|
SI->Complete = true;
|
2025-11-19 09:50:46 +11:00
|
|
|
}
|
2025-11-19 16:36:30 +11:00
|
|
|
|
|
|
|
|
SI->CompleteCV.notify_all();
|
2025-10-09 14:27:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace orc_rt
|