brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · 38668f3 Raw
68 lines · cpp
1//===-- ProtocolServer.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Core/ProtocolServer.h"10#include "lldb/Core/PluginManager.h"11#include "llvm/Support/Error.h"12 13using namespace lldb_private;14using namespace lldb;15 16static std::pair<llvm::StringMap<ProtocolServerUP> &, std::mutex &> Servers() {17  static llvm::StringMap<ProtocolServerUP> g_protocol_server_instances;18  static std::mutex g_mutex;19  return {g_protocol_server_instances, g_mutex};20}21 22ProtocolServer *ProtocolServer::GetOrCreate(llvm::StringRef name) {23  auto [protocol_server_instances, mutex] = Servers();24 25  std::lock_guard<std::mutex> guard(mutex);26 27  auto it = protocol_server_instances.find(name);28  if (it != protocol_server_instances.end())29    return it->second.get();30 31  if (ProtocolServerCreateInstance create_callback =32          PluginManager::GetProtocolCreateCallbackForPluginName(name)) {33    auto pair = protocol_server_instances.try_emplace(name, create_callback());34    return pair.first->second.get();35  }36 37  return nullptr;38}39 40std::vector<llvm::StringRef> ProtocolServer::GetSupportedProtocols() {41  std::vector<llvm::StringRef> supported_protocols;42  size_t i = 0;43 44  for (llvm::StringRef protocol_name =45           PluginManager::GetProtocolServerPluginNameAtIndex(i++);46       !protocol_name.empty();47       protocol_name = PluginManager::GetProtocolServerPluginNameAtIndex(i++)) {48    supported_protocols.push_back(protocol_name);49  }50 51  return supported_protocols;52}53 54llvm::Error ProtocolServer::Terminate() {55  llvm::Error error = llvm::Error::success();56 57  auto [protocol_server_instances, mutex] = Servers();58  std::lock_guard<std::mutex> guard(mutex);59  for (auto &instance : protocol_server_instances) {60    if (llvm::Error instance_error = instance.second->Stop())61      error = llvm::joinErrors(std::move(error), std::move(instance_error));62  }63 64  protocol_server_instances.clear();65 66  return error;67}68