Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 1 | //===-- SystemLifetimeManager.cpp ------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Initialization/SystemLifetimeManager.h" |
| 10 | |
| 11 | #include "lldb/Core/Debugger.h" |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 12 | #include "lldb/Initialization/SystemInitializer.h" |
| 13 | |
| 14 | #include <utility> |
| 15 | |
| 16 | using namespace lldb_private; |
| 17 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | SystemLifetimeManager::SystemLifetimeManager() |
| 19 | : m_mutex(), m_initialized(false) {} |
| 20 | |
| 21 | SystemLifetimeManager::~SystemLifetimeManager() { |
| 22 | assert(!m_initialized && |
| 23 | "SystemLifetimeManager destroyed without calling Terminate!"); |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 26 | llvm::Error SystemLifetimeManager::Initialize( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | std::unique_ptr<SystemInitializer> initializer, |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 28 | const InitializerOptions &options, LoadPluginCallbackType plugin_callback) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 30 | if (!m_initialized) { |
| 31 | assert(!m_initializer && "Attempting to call " |
| 32 | "SystemLifetimeManager::Initialize() when it is " |
| 33 | "already initialized"); |
| 34 | m_initialized = true; |
| 35 | m_initializer = std::move(initializer); |
| 36 | |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 37 | if (auto e = m_initializer->Initialize(options)) |
| 38 | return e; |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | Debugger::Initialize(plugin_callback); |
| 41 | } |
Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 42 | |
| 43 | return llvm::Error::success(); |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | void SystemLifetimeManager::Terminate() { |
| 47 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | if (m_initialized) { |
| 50 | Debugger::Terminate(); |
| 51 | m_initializer->Terminate(); |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | m_initializer.reset(); |
| 54 | m_initialized = false; |
| 55 | } |
Zachary Turner | e6e2bb3 | 2015-03-31 21:03:22 +0000 | [diff] [blame] | 56 | } |