11c3bbb01SEd Maste //===-- SystemLifetimeManager.cpp ------------------------------*- C++ -*-===//
21c3bbb01SEd Maste //
31c3bbb01SEd Maste //                     The LLVM Compiler Infrastructure
41c3bbb01SEd Maste //
51c3bbb01SEd Maste // This file is distributed under the University of Illinois Open Source
61c3bbb01SEd Maste // License. See LICENSE.TXT for details.
71c3bbb01SEd Maste //
81c3bbb01SEd Maste //===----------------------------------------------------------------------===//
91c3bbb01SEd Maste 
101c3bbb01SEd Maste #include "lldb/Initialization/SystemLifetimeManager.h"
111c3bbb01SEd Maste 
121c3bbb01SEd Maste #include "lldb/Core/Debugger.h"
131c3bbb01SEd Maste #include "lldb/Initialization/SystemInitializer.h"
141c3bbb01SEd Maste 
151c3bbb01SEd Maste #include <utility>
161c3bbb01SEd Maste 
171c3bbb01SEd Maste using namespace lldb_private;
181c3bbb01SEd Maste 
SystemLifetimeManager()19435933ddSDimitry Andric SystemLifetimeManager::SystemLifetimeManager()
20435933ddSDimitry Andric     : m_mutex(), m_initialized(false) {}
21435933ddSDimitry Andric 
~SystemLifetimeManager()22435933ddSDimitry Andric SystemLifetimeManager::~SystemLifetimeManager() {
23435933ddSDimitry Andric   assert(!m_initialized &&
24435933ddSDimitry Andric          "SystemLifetimeManager destroyed without calling Terminate!");
251c3bbb01SEd Maste }
261c3bbb01SEd Maste 
Initialize(std::unique_ptr<SystemInitializer> initializer,const InitializerOptions & options,LoadPluginCallbackType plugin_callback)27*b5893f02SDimitry Andric llvm::Error SystemLifetimeManager::Initialize(
28435933ddSDimitry Andric     std::unique_ptr<SystemInitializer> initializer,
29*b5893f02SDimitry Andric     const InitializerOptions &options, LoadPluginCallbackType plugin_callback) {
304bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
31435933ddSDimitry Andric   if (!m_initialized) {
32435933ddSDimitry Andric     assert(!m_initializer && "Attempting to call "
33435933ddSDimitry Andric                              "SystemLifetimeManager::Initialize() when it is "
34435933ddSDimitry Andric                              "already initialized");
351c3bbb01SEd Maste     m_initialized = true;
361c3bbb01SEd Maste     m_initializer = std::move(initializer);
371c3bbb01SEd Maste 
38*b5893f02SDimitry Andric     if (auto e = m_initializer->Initialize(options))
39*b5893f02SDimitry Andric       return e;
40*b5893f02SDimitry Andric 
411c3bbb01SEd Maste     Debugger::Initialize(plugin_callback);
421c3bbb01SEd Maste   }
43*b5893f02SDimitry Andric 
44*b5893f02SDimitry Andric   return llvm::Error::success();
451c3bbb01SEd Maste }
461c3bbb01SEd Maste 
Terminate()47435933ddSDimitry Andric void SystemLifetimeManager::Terminate() {
484bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_mutex);
491c3bbb01SEd Maste 
50435933ddSDimitry Andric   if (m_initialized) {
511c3bbb01SEd Maste     Debugger::Terminate();
521c3bbb01SEd Maste     m_initializer->Terminate();
531c3bbb01SEd Maste 
541c3bbb01SEd Maste     m_initializer.reset();
551c3bbb01SEd Maste     m_initialized = false;
561c3bbb01SEd Maste   }
571c3bbb01SEd Maste }
58