mirror of
https://github.com/MaskRay/ccls.git
synced 2025-01-31 09:50:26 +00:00
Set thread names. Fix initialization race condition when not using shared memory.
This commit is contained in:
parent
741e357053
commit
9d8c027cfb
@ -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<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>> threaded_queue_for_client_;
|
||||
std::unique_ptr<ThreadedQueue<std::unique_ptr<BaseIpcMessage>>> 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<std::unique_ptr<BaseIpcMessage>> 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));
|
||||
|
@ -27,6 +27,7 @@ std::unique_ptr<PlatformSharedMemory> 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<std::string> GetPlatformClangArguments();
|
@ -26,6 +26,8 @@
|
||||
#include <sys/stat.h> /* For mode constants */
|
||||
#include <fcntl.h> /* For O_* constants */
|
||||
|
||||
#include <sys/prctl.h>
|
||||
|
||||
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<std::string> GetPlatformClangArguments() {
|
||||
// TODO: use install config variable for path?
|
||||
return {
|
||||
|
@ -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<std::string> GetPlatformClangArguments() {
|
||||
return {
|
||||
"-fms-compatibility",
|
||||
|
Loading…
Reference in New Issue
Block a user