diff --git a/src/command_line.cc b/src/command_line.cc index 0dc470ae..02ef1ab6 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -88,10 +88,11 @@ struct IpcManager { static IpcManager* instance_; static IpcManager* instance() { - if (!instance_) - instance_ = new IpcManager(); return instance_; } + static void CreateInstance() { + instance_ = new IpcManager(); + } std::unique_ptr>> threaded_queue_for_client_; std::unique_ptr>> threaded_queue_for_server_; @@ -933,6 +934,7 @@ void IndexMain( Index_DoIdMapQueue* queue_do_id_map, Index_OnIdMappedQueue* queue_on_id_mapped, Index_OnIndexedQueue* queue_on_indexed) { + SetCurrentThreadName("indexer"); while (true) { // TODO: process all off IndexMain_DoIndex before calling IndexMain_DoCreateIndexUpdate for @@ -1029,7 +1031,7 @@ void QueryDbMainLoop( switch (message->method_id) { case IpcId::Quit: { - std::cerr << "Got quit message (exiting)" << std::endl; + std::cerr << "[querydb] Got quit message (exiting)" << std::endl; exit(0); break; } @@ -1136,9 +1138,8 @@ void QueryDbMainLoop( response.result.items = completion_manager->CodeComplete(msg->params); Timer timer; - response.Write(std::cout); + ipc->SendOutMessageToClient(response); timer.ResetAndPrint("Writing completion results"); - //SendOutMessageToClient(language_client, response); break; } @@ -1508,6 +1509,7 @@ void QueryDbMainLoop( } void QueryDbMain() { + SetCurrentThreadName("querydb"); //std::cerr << "Running QueryDb" << std::endl; // Create queues. @@ -1607,6 +1609,7 @@ void QueryDbMain() { // // |ipc| is connected to a server. void LanguageServerStdinLoop() { + SetCurrentThreadName("stdin"); IpcManager* ipc = IpcManager::instance(); while (true) { @@ -1798,8 +1801,13 @@ bool IsQueryDbProcessRunning() { // Check if we got an IsAlive message back. std::vector> messages = ipc->GetMessages(IpcManager::Destination::Client); for (auto& message : messages) { - if (IpcId::IsAlive == message->method_id) + if (IpcId::IsAlive == message->method_id) { return true; + } + else { + std::cerr << "[setup] Unhandled IPC message " << IpcIdToString(message->method_id) << std::endl; + exit(1); + } } // No response back. Clear out server messages so server doesn't respond to stale request. @@ -1809,6 +1817,8 @@ bool IsQueryDbProcessRunning() { } void LanguageServerMain() { + SetCurrentThreadName("server"); + bool has_server = IsQueryDbProcessRunning(); // No server is running. Start it in-process. If the user wants to run the @@ -1876,6 +1886,8 @@ void LanguageServerMain() { int main(int argc, char** argv) { + IpcManager::CreateInstance(); + //bool loop = true; //while (loop) // std::this_thread::sleep_for(std::chrono::milliseconds(10)); diff --git a/src/platform.h b/src/platform.h index 0202fd01..9bd5700c 100644 --- a/src/platform.h +++ b/src/platform.h @@ -27,6 +27,7 @@ std::unique_ptr CreatePlatformSharedMemory( void PlatformInit(); std::string GetWorkingDirectory(); std::string NormalizePath(const std::string& path); +void SetCurrentThreadName(const std::string& thread_name); // Returns any clang arguments that are specific to the current platform. std::vector GetPlatformClangArguments(); \ No newline at end of file diff --git a/src/platform_linux.cc b/src/platform_linux.cc index 620339db..414c585e 100644 --- a/src/platform_linux.cc +++ b/src/platform_linux.cc @@ -26,6 +26,8 @@ #include /* For mode constants */ #include /* For O_* constants */ +#include + struct PlatformMutexLinux : public PlatformMutex { sem_t* sem_ = nullptr; @@ -138,6 +140,10 @@ std::string NormalizePath(const std::string& path) { return name; } +void SetCurrentThreadName(const std::string& thread_name) { + prctl(PR_SET_NAME, thread_name.c_str(), 0, 0, 0); +} + std::vector GetPlatformClangArguments() { // TODO: use install config variable for path? return { diff --git a/src/platform_win.cc b/src/platform_win.cc index be6ceea7..f8857844 100644 --- a/src/platform_win.cc +++ b/src/platform_win.cc @@ -145,6 +145,31 @@ std::string NormalizePath(const std::string& path) { return result; } + +// See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx +const DWORD MS_VC_EXCEPTION = 0x406D1388; +#pragma pack(push,8) +typedef struct tagTHREADNAME_INFO +{ + DWORD dwType; // Must be 0x1000. + LPCSTR szName; // Pointer to name (in user addr space). + DWORD dwThreadID; // Thread ID (-1=caller thread). + DWORD dwFlags; // Reserved for future use, must be zero. +} THREADNAME_INFO; +#pragma pack(pop) +void SetCurrentThreadName(const std::string& thread_name) { + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = thread_name.c_str(); + info.dwThreadID = -1; + info.dwFlags = 0; + + __try { + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info); + } + __except (EXCEPTION_EXECUTE_HANDLER) {} +} + std::vector GetPlatformClangArguments() { return { "-fms-compatibility",