| 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/Initialization/SystemInitializer.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | |
| 15 | using namespace lldb_private; |
| 16 | |
| 17 | SystemLifetimeManager::SystemLifetimeManager() : m_mutex() {} |
| 18 | |
| 19 | SystemLifetimeManager::~SystemLifetimeManager() { |
| 20 | assert(!m_initialized && |
| 21 | "SystemLifetimeManager destroyed without calling Terminate!"); |
| 22 | } |
| 23 | |
| 24 | llvm::Error SystemLifetimeManager::Initialize( |
| 25 | std::unique_ptr<SystemInitializer> initializer) { |
| 26 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 27 | if (!m_initialized) { |
| 28 | assert(!m_initializer && "Attempting to call " |
| 29 | "SystemLifetimeManager::Initialize() when it is " |
| 30 | "already initialized"); |
| 31 | m_initialized = true; |
| 32 | m_initializer = std::move(initializer); |
| 33 | |
| 34 | if (auto e = m_initializer->Initialize()) |
| 35 | return e; |
| 36 | } |
| 37 | |
| 38 | return llvm::Error::success(); |
| 39 | } |
| 40 | |
| 41 | void SystemLifetimeManager::Terminate() { |
| 42 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 43 | |
| 44 | if (m_initialized) { |
| 45 | m_initializer->Terminate(); |
| 46 | |
| 47 | m_initializer.reset(); |
| 48 | m_initialized = false; |
| 49 | } |
| 50 | } |
| 51 |
