1 | //===-- SystemLifetimeManager.cpp -----------------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Initialization/SystemLifetimeManager.h" |
10 | |
11 | #include "lldb/Core/Debugger.h" |
12 | #include "lldb/Initialization/SystemInitializer.h" |
13 | |
14 | #include <utility> |
15 | |
16 | using namespace lldb_private; |
17 | |
18 | SystemLifetimeManager::SystemLifetimeManager() : m_mutex() {} |
19 | |
20 | SystemLifetimeManager::~SystemLifetimeManager() { |
21 | assert(!m_initialized && |
22 | "SystemLifetimeManager destroyed without calling Terminate!"); |
23 | } |
24 | |
25 | llvm::Error SystemLifetimeManager::Initialize( |
26 | std::unique_ptr<SystemInitializer> initializer, |
27 | LoadPluginCallbackType plugin_callback) { |
28 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
29 | if (!m_initialized) { |
30 | assert(!m_initializer && "Attempting to call " |
31 | "SystemLifetimeManager::Initialize() when it is " |
32 | "already initialized"); |
33 | m_initialized = true; |
34 | m_initializer = std::move(initializer); |
35 | |
36 | if (auto e = m_initializer->Initialize()) |
37 | return e; |
38 | |
39 | Debugger::Initialize(load_plugin_callback: plugin_callback); |
40 | } |
41 | |
42 | return llvm::Error::success(); |
43 | } |
44 | |
45 | void SystemLifetimeManager::Terminate() { |
46 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
47 | |
48 | if (m_initialized) { |
49 | Debugger::Terminate(); |
50 | m_initializer->Terminate(); |
51 | |
52 | m_initializer.reset(); |
53 | m_initialized = false; |
54 | } |
55 | } |
56 |