xref: /src/contrib/llvm-project/lldb/source/Initialization/SystemLifetimeManager.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1cfca06d7SDimitry Andric //===-- SystemLifetimeManager.cpp -----------------------------------------===//
25e95aa85SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65e95aa85SEd Maste //
75e95aa85SEd Maste //===----------------------------------------------------------------------===//
85e95aa85SEd Maste 
95e95aa85SEd Maste #include "lldb/Initialization/SystemLifetimeManager.h"
105e95aa85SEd Maste 
115e95aa85SEd Maste #include "lldb/Core/Debugger.h"
125e95aa85SEd Maste #include "lldb/Initialization/SystemInitializer.h"
135e95aa85SEd Maste 
145e95aa85SEd Maste #include <utility>
155e95aa85SEd Maste 
165e95aa85SEd Maste using namespace lldb_private;
175e95aa85SEd Maste 
SystemLifetimeManager()18344a3780SDimitry Andric SystemLifetimeManager::SystemLifetimeManager() : m_mutex() {}
1914f1b3e8SDimitry Andric 
~SystemLifetimeManager()2014f1b3e8SDimitry Andric SystemLifetimeManager::~SystemLifetimeManager() {
2114f1b3e8SDimitry Andric   assert(!m_initialized &&
2214f1b3e8SDimitry Andric          "SystemLifetimeManager destroyed without calling Terminate!");
235e95aa85SEd Maste }
245e95aa85SEd Maste 
Initialize(std::unique_ptr<SystemInitializer> initializer,LoadPluginCallbackType plugin_callback)2594994d37SDimitry Andric llvm::Error SystemLifetimeManager::Initialize(
2614f1b3e8SDimitry Andric     std::unique_ptr<SystemInitializer> initializer,
275f29bb8aSDimitry Andric     LoadPluginCallbackType plugin_callback) {
28f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
2914f1b3e8SDimitry Andric   if (!m_initialized) {
3014f1b3e8SDimitry Andric     assert(!m_initializer && "Attempting to call "
3114f1b3e8SDimitry Andric                              "SystemLifetimeManager::Initialize() when it is "
3214f1b3e8SDimitry Andric                              "already initialized");
335e95aa85SEd Maste     m_initialized = true;
345e95aa85SEd Maste     m_initializer = std::move(initializer);
355e95aa85SEd Maste 
365f29bb8aSDimitry Andric     if (auto e = m_initializer->Initialize())
3794994d37SDimitry Andric       return e;
3894994d37SDimitry Andric 
395e95aa85SEd Maste     Debugger::Initialize(plugin_callback);
405e95aa85SEd Maste   }
4194994d37SDimitry Andric 
4294994d37SDimitry Andric   return llvm::Error::success();
435e95aa85SEd Maste }
445e95aa85SEd Maste 
Terminate()4514f1b3e8SDimitry Andric void SystemLifetimeManager::Terminate() {
46f3fbd1c0SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(m_mutex);
475e95aa85SEd Maste 
4814f1b3e8SDimitry Andric   if (m_initialized) {
495e95aa85SEd Maste     Debugger::Terminate();
505e95aa85SEd Maste     m_initializer->Terminate();
515e95aa85SEd Maste 
525e95aa85SEd Maste     m_initializer.reset();
535e95aa85SEd Maste     m_initialized = false;
545e95aa85SEd Maste   }
555e95aa85SEd Maste }
56