mirror of
				https://github.com/MaskRay/ccls.git
				synced 2025-11-03 22:04:24 +00:00 
			
		
		
		
	Use pthread if defined(__unix__) || defined(__APPLE__)
This commit is contained in:
		
							parent
							
								
									8d49b44154
								
							
						
					
					
						commit
						29f05d96fb
					
				@ -221,7 +221,9 @@ bool Parse(CompilerInstance &Clang) {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CompletionPreloadMain(CompletionManager *manager) {
 | 
			
		||||
void *CompletionPreloadMain(void *manager_) {
 | 
			
		||||
  auto *manager = static_cast<CompletionManager*>(manager_);
 | 
			
		||||
  set_thread_name("comp-preload");
 | 
			
		||||
  while (true) {
 | 
			
		||||
    auto request = manager->preload_requests_.Dequeue();
 | 
			
		||||
 | 
			
		||||
@ -246,9 +248,12 @@ void CompletionPreloadMain(CompletionManager *manager) {
 | 
			
		||||
      manager->DiagnosticsUpdate(request.path, debounce);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CompletionMain(CompletionManager *manager) {
 | 
			
		||||
void *CompletionMain(void *manager_) {
 | 
			
		||||
  auto *manager = static_cast<CompletionManager *>(manager_);
 | 
			
		||||
  set_thread_name("comp");
 | 
			
		||||
  while (true) {
 | 
			
		||||
    // Fetching the completion request blocks until we have a request.
 | 
			
		||||
    std::unique_ptr<CompletionManager::CompletionRequest> request =
 | 
			
		||||
@ -296,6 +301,7 @@ void CompletionMain(CompletionManager *manager) {
 | 
			
		||||
 | 
			
		||||
    request->on_complete(&Clang->getCodeCompletionConsumer());
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
llvm::StringRef diagLeveltoString(DiagnosticsEngine::Level Lvl) {
 | 
			
		||||
@ -326,7 +332,9 @@ void printDiag(llvm::raw_string_ostream &OS, const DiagBase &d) {
 | 
			
		||||
  OS << diagLeveltoString(d.level) << ": " << d.message;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DiagnosticMain(CompletionManager *manager) {
 | 
			
		||||
void *DiagnosticMain(void *manager_) {
 | 
			
		||||
  auto *manager = static_cast<CompletionManager*>(manager_);
 | 
			
		||||
  set_thread_name("diag");
 | 
			
		||||
  while (true) {
 | 
			
		||||
    CompletionManager::DiagnosticRequest request =
 | 
			
		||||
        manager->diagnostic_request_.Dequeue();
 | 
			
		||||
@ -422,6 +430,7 @@ void DiagnosticMain(CompletionManager *manager) {
 | 
			
		||||
    }
 | 
			
		||||
    manager->on_diagnostic_(path, ls_diags);
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace
 | 
			
		||||
@ -470,21 +479,9 @@ CompletionManager::CompletionManager(Project *project,
 | 
			
		||||
      preloads(kMaxPreloadedSessions),
 | 
			
		||||
      sessions(kMaxCompletionSessions),
 | 
			
		||||
      PCH(std::make_shared<PCHContainerOperations>()) {
 | 
			
		||||
  std::thread([&]() {
 | 
			
		||||
    set_thread_name("comp");
 | 
			
		||||
    ccls::CompletionMain(this);
 | 
			
		||||
  })
 | 
			
		||||
      .detach();
 | 
			
		||||
  std::thread([&]() {
 | 
			
		||||
    set_thread_name("comp-preload");
 | 
			
		||||
    ccls::CompletionPreloadMain(this);
 | 
			
		||||
  })
 | 
			
		||||
      .detach();
 | 
			
		||||
  std::thread([&]() {
 | 
			
		||||
    set_thread_name("diag");
 | 
			
		||||
    ccls::DiagnosticMain(this);
 | 
			
		||||
  })
 | 
			
		||||
      .detach();
 | 
			
		||||
  SpawnThread(ccls::CompletionMain, this);
 | 
			
		||||
  SpawnThread(ccls::CompletionPreloadMain, this);
 | 
			
		||||
  SpawnThread(ccls::DiagnosticMain, this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CompletionManager::DiagnosticsUpdate(const std::string &path,
 | 
			
		||||
 | 
			
		||||
@ -406,6 +406,19 @@ struct Out_InitializeResponse : public lsOutMessage<Out_InitializeResponse> {
 | 
			
		||||
MAKE_REFLECT_STRUCT(Out_InitializeResponse::InitializeResult, capabilities);
 | 
			
		||||
MAKE_REFLECT_STRUCT(Out_InitializeResponse, jsonrpc, id, result);
 | 
			
		||||
 | 
			
		||||
void *Indexer(void *arg_) {
 | 
			
		||||
  MessageHandler *h;
 | 
			
		||||
  int idx;
 | 
			
		||||
  auto *arg = static_cast<std::pair<MessageHandler *, int> *>(arg_);
 | 
			
		||||
  std::tie(h, idx) = *arg;
 | 
			
		||||
  delete arg;
 | 
			
		||||
  std::string name = "indexer" + std::to_string(idx);
 | 
			
		||||
  set_thread_name(name.c_str());
 | 
			
		||||
  pipeline::Indexer_Main(h->clang_complete, h->vfs, h->project,
 | 
			
		||||
                         h->working_files);
 | 
			
		||||
  return nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Handler_Initialize : BaseMessageHandler<In_InitializeRequest> {
 | 
			
		||||
  MethodType GetMethodType() const override { return kMethodType; }
 | 
			
		||||
 | 
			
		||||
@ -483,7 +496,6 @@ struct Handler_Initialize : BaseMessageHandler<In_InitializeRequest> {
 | 
			
		||||
 | 
			
		||||
    idx::Init();
 | 
			
		||||
 | 
			
		||||
    // Open up / load the project.
 | 
			
		||||
    project->Load(project_path);
 | 
			
		||||
 | 
			
		||||
    // Start indexer threads. Start this after loading the project, as that
 | 
			
		||||
@ -493,14 +505,8 @@ struct Handler_Initialize : BaseMessageHandler<In_InitializeRequest> {
 | 
			
		||||
      g_config->index.threads = std::thread::hardware_concurrency();
 | 
			
		||||
 | 
			
		||||
    LOG_S(INFO) << "start " << g_config->index.threads << " indexers";
 | 
			
		||||
    for (int i = 0; i < g_config->index.threads; i++) {
 | 
			
		||||
      std::thread([=]() {
 | 
			
		||||
        std::string name = "indexer" + std::to_string(i);
 | 
			
		||||
        set_thread_name(name.c_str());
 | 
			
		||||
        pipeline::Indexer_Main(clang_complete, vfs, project, working_files);
 | 
			
		||||
      })
 | 
			
		||||
          .detach();
 | 
			
		||||
    }
 | 
			
		||||
    for (int i = 0; i < g_config->index.threads; i++)
 | 
			
		||||
      SpawnThread(Indexer, new std::pair<MessageHandler *, int>{this, i});
 | 
			
		||||
 | 
			
		||||
    // Start scanning include directories before dispatching project
 | 
			
		||||
    // files, because that takes a long time.
 | 
			
		||||
 | 
			
		||||
@ -29,3 +29,5 @@ void TraceMe();
 | 
			
		||||
 | 
			
		||||
std::string GetExternalCommandOutput(const std::vector<std::string> &command,
 | 
			
		||||
                                     std::string_view input);
 | 
			
		||||
 | 
			
		||||
void SpawnThread(void *(*fn)(void *), void *arg);
 | 
			
		||||
 | 
			
		||||
@ -28,7 +28,9 @@ limitations under the License.
 | 
			
		||||
#include <dirent.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <sys/resource.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <sys/types.h> // required for stat.h
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
@ -169,4 +171,19 @@ std::string GetExternalCommandOutput(const std::vector<std::string> &command,
 | 
			
		||||
  return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SpawnThread(void *(*fn)(void *), void *arg) {
 | 
			
		||||
  pthread_t thd;
 | 
			
		||||
  pthread_attr_t attr;
 | 
			
		||||
  struct rlimit rlim;
 | 
			
		||||
  size_t stack_size = 4 * 1024 * 1024;
 | 
			
		||||
  if (getrlimit(RLIMIT_STACK, &rlim) == 0 &&
 | 
			
		||||
      rlim.rlim_cur != RLIM_INFINITY)
 | 
			
		||||
    stack_size = rlim.rlim_cur;
 | 
			
		||||
  pthread_attr_init(&attr);
 | 
			
		||||
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 | 
			
		||||
  pthread_attr_setstacksize(&attr, stack_size);
 | 
			
		||||
  pthread_create(&thd, &attr, fn, arg);
 | 
			
		||||
  pthread_attr_destroy(&attr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
@ -27,8 +27,8 @@ limitations under the License.
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <cassert>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <thread>
 | 
			
		||||
 | 
			
		||||
std::string NormalizePath(const std::string &path) {
 | 
			
		||||
  DWORD retval = 0;
 | 
			
		||||
@ -61,4 +61,8 @@ std::string GetExternalCommandOutput(const std::vector<std::string> &command,
 | 
			
		||||
  return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SpawnThread(void *(*fn)(void *), void *arg) {
 | 
			
		||||
  std::thread(fn, arg).detach();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user