Skip to content

Commit b8be4b0

Browse files
lhamesgithub-actions[bot]
authored andcommitted
Automerge: [orc-rt] Simplify Session shutdown. (#168664)
Moves all Session member variables dedicated to shutdown into a new ShutdownInfo struct, and uses the presence / absence of this struct as the flag to indicate that we've entered the "shutting down" state. This simplifies the implementation of the shutdown process.
2 parents 948ba6a + be1a504 commit b8be4b0

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

orc-rt/include/orc-rt/Session.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,22 @@ class Session {
7575
}
7676

7777
private:
78-
void shutdownNext(Error Err,
79-
std::vector<std::unique_ptr<ResourceManager>> RemainingRMs);
80-
78+
struct ShutdownInfo {
79+
bool Complete = false;
80+
std::condition_variable CompleteCV;
81+
std::vector<std::unique_ptr<ResourceManager>> ResourceMgrs;
82+
std::vector<OnShutdownCompleteFn> OnCompletes;
83+
};
84+
85+
void shutdownNext(Error Err);
8186
void shutdownComplete();
8287

8388
std::unique_ptr<TaskDispatcher> Dispatcher;
8489
ErrorReporterFn ReportError;
8590

86-
enum class SessionState { Running, ShuttingDown, Shutdown };
87-
8891
std::mutex M;
89-
SessionState State = SessionState::Running;
90-
std::condition_variable StateCV;
9192
std::vector<std::unique_ptr<ResourceManager>> ResourceMgrs;
92-
std::vector<OnShutdownCompleteFn> ShutdownCallbacks;
93+
std::unique_ptr<ShutdownInfo> SI;
9394
};
9495

9596
inline orc_rt_SessionRef wrap(Session *S) noexcept {

orc-rt/lib/executor/Session.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,59 @@ namespace orc_rt {
1717
Session::~Session() { waitForShutdown(); }
1818

1919
void Session::shutdown(OnShutdownCompleteFn OnShutdownComplete) {
20-
std::vector<std::unique_ptr<ResourceManager>> ToShutdown;
21-
2220
{
2321
std::scoped_lock<std::mutex> Lock(M);
24-
ShutdownCallbacks.push_back(std::move(OnShutdownComplete));
25-
26-
// If somebody else has already called shutdown then there's nothing further
27-
// for us to do here.
28-
if (State >= SessionState::ShuttingDown)
22+
if (SI) {
23+
SI->OnCompletes.push_back(std::move(OnShutdownComplete));
2924
return;
25+
}
3026

31-
State = SessionState::ShuttingDown;
32-
std::swap(ResourceMgrs, ToShutdown);
27+
SI = std::make_unique<ShutdownInfo>();
28+
SI->OnCompletes.push_back(std::move(OnShutdownComplete));
29+
std::swap(SI->ResourceMgrs, ResourceMgrs);
3330
}
3431

35-
shutdownNext(Error::success(), std::move(ToShutdown));
32+
shutdownNext(Error::success());
3633
}
3734

3835
void Session::waitForShutdown() {
3936
shutdown([]() {});
4037
std::unique_lock<std::mutex> Lock(M);
41-
StateCV.wait(Lock, [&]() { return State == SessionState::Shutdown; });
38+
SI->CompleteCV.wait(Lock, [&]() { return SI->Complete; });
4239
}
4340

44-
void Session::shutdownNext(
45-
Error Err, std::vector<std::unique_ptr<ResourceManager>> RemainingRMs) {
41+
void Session::shutdownNext(Error Err) {
4642
if (Err)
4743
reportError(std::move(Err));
4844

49-
if (RemainingRMs.empty())
45+
if (SI->ResourceMgrs.empty())
5046
return shutdownComplete();
5147

52-
auto NextRM = std::move(RemainingRMs.back());
53-
RemainingRMs.pop_back();
54-
NextRM->shutdown(
55-
[this, RemainingRMs = std::move(RemainingRMs)](Error Err) mutable {
56-
shutdownNext(std::move(Err), std::move(RemainingRMs));
57-
});
48+
// Get the next ResourceManager to shut down.
49+
auto NextRM = std::move(SI->ResourceMgrs.back());
50+
SI->ResourceMgrs.pop_back();
51+
NextRM->shutdown([this](Error Err) { shutdownNext(std::move(Err)); });
5852
}
5953

6054
void Session::shutdownComplete() {
6155

6256
std::unique_ptr<TaskDispatcher> TmpDispatcher;
63-
std::vector<OnShutdownCompleteFn> TmpShutdownCallbacks;
6457
{
6558
std::lock_guard<std::mutex> Lock(M);
6659
TmpDispatcher = std::move(Dispatcher);
67-
TmpShutdownCallbacks = std::move(ShutdownCallbacks);
6860
}
6961

7062
TmpDispatcher->shutdown();
7163

72-
for (auto &OnShutdownComplete : TmpShutdownCallbacks)
64+
for (auto &OnShutdownComplete : SI->OnCompletes)
7365
OnShutdownComplete();
7466

7567
{
7668
std::lock_guard<std::mutex> Lock(M);
77-
State = SessionState::Shutdown;
69+
SI->Complete = true;
7870
}
79-
StateCV.notify_all();
71+
72+
SI->CompleteCV.notify_all();
8073
}
8174

8275
} // namespace orc_rt

0 commit comments

Comments
 (0)