aboutsummaryrefslogtreecommitdiff
path: root/orc-rt/lib/executor/Session.cpp
blob: 599bc8705f3978e8648dfd984d9cfa259d2e71a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//===- 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"

#include <future>

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);
    std::swap(ResourceMgrs, ToShutdown);
  }

  shutdownNext(std::move(OnShutdownComplete), Error::success(),
               std::move(ToShutdown));
}

void Session::waitForShutdown() {
  std::promise<void> P;
  auto F = P.get_future();

  shutdown([P = std::move(P)]() mutable { P.set_value(); });

  F.wait();
}

void Session::shutdownNext(
    OnShutdownCompleteFn OnComplete, Error Err,
    std::vector<std::unique_ptr<ResourceManager>> RemainingRMs) {
  if (Err)
    reportError(std::move(Err));

  if (RemainingRMs.empty())
    return OnComplete();

  auto NextRM = std::move(RemainingRMs.back());
  RemainingRMs.pop_back();
  NextRM->shutdown([this, RemainingRMs = std::move(RemainingRMs),
                    OnComplete = std::move(OnComplete)](Error Err) mutable {
    shutdownNext(std::move(OnComplete), std::move(Err),
                 std::move(RemainingRMs));
  });
}

} // namespace orc_rt