@@ -17,66 +17,59 @@ namespace orc_rt {
1717Session::~Session () { waitForShutdown (); }
1818
1919void 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
3835void 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
6054void 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