Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lldb/include/lldb/Core/ProtocolServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ProtocolServer : public PluginInterface {

static ProtocolServer *GetOrCreate(llvm::StringRef name);

static llvm::Error Terminate();

static std::vector<llvm::StringRef> GetSupportedProtocols();

struct Connection {
Expand Down
34 changes: 27 additions & 7 deletions lldb/source/Core/ProtocolServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@

#include "lldb/Core/ProtocolServer.h"
#include "lldb/Core/PluginManager.h"
#include "llvm/Support/Error.h"

using namespace lldb_private;
using namespace lldb;

ProtocolServer *ProtocolServer::GetOrCreate(llvm::StringRef name) {
static std::mutex g_mutex;
static std::pair<llvm::StringMap<ProtocolServerUP> &, std::mutex &> Servers() {
static llvm::StringMap<ProtocolServerUP> g_protocol_server_instances;
static std::mutex g_mutex;
return {g_protocol_server_instances, g_mutex};
}

ProtocolServer *ProtocolServer::GetOrCreate(llvm::StringRef name) {
auto [protocol_server_instances, mutex] = Servers();

std::lock_guard<std::mutex> guard(g_mutex);
std::lock_guard<std::mutex> guard(mutex);

auto it = g_protocol_server_instances.find(name);
if (it != g_protocol_server_instances.end())
auto it = protocol_server_instances.find(name);
if (it != protocol_server_instances.end())
return it->second.get();

if (ProtocolServerCreateInstance create_callback =
PluginManager::GetProtocolCreateCallbackForPluginName(name)) {
auto pair =
g_protocol_server_instances.try_emplace(name, create_callback());
auto pair = protocol_server_instances.try_emplace(name, create_callback());
return pair.first->second.get();
}

Expand All @@ -45,3 +50,18 @@ std::vector<llvm::StringRef> ProtocolServer::GetSupportedProtocols() {

return supported_protocols;
}

llvm::Error ProtocolServer::Terminate() {
llvm::Error error = llvm::Error::success();

auto [protocol_server_instances, mutex] = Servers();
std::lock_guard<std::mutex> guard(mutex);
for (auto &instance : protocol_server_instances) {
if (llvm::Error instance_error = instance.second->Stop())
error = llvm::joinErrors(std::move(error), std::move(instance_error));
}

protocol_server_instances.clear();

return error;
}
2 changes: 2 additions & 0 deletions lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ void ProtocolServerMCP::Initialize() {
}

void ProtocolServerMCP::Terminate() {
if (llvm::Error error = ProtocolServer::Terminate())
LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(error), "{0}");
PluginManager::UnregisterPlugin(CreateInstance);
}

Expand Down
Loading