Remove WorkThread::Result.

WorkThread::StartThread is now a more typical main function.
This commit is contained in:
Jacob Dufault 2017-12-28 09:18:54 -08:00
parent 8468ef09c3
commit 7939aec743
8 changed files with 143 additions and 160 deletions

View File

@ -187,9 +187,9 @@ void RunQueryDbThread(const std::string& bin_name,
SetCurrentThreadName("querydb"); SetCurrentThreadName("querydb");
while (true) { while (true) {
bool did_work = QueryDbMainLoop( bool did_work = QueryDbMainLoop(
config, &db, waiter, &project, &file_consumer_shared, config, &db, waiter, &project, &file_consumer_shared, &import_manager,
&import_manager, &timestamp_manager, &semantic_cache, &working_files, &timestamp_manager, &semantic_cache, &working_files, &clang_complete,
&clang_complete, &include_complete, global_code_complete_cache.get(), &include_complete, global_code_complete_cache.get(),
non_global_code_complete_cache.get(), signature_cache.get()); non_global_code_complete_cache.get(), signature_cache.get());
// Cleanup and free any unused memory. // Cleanup and free any unused memory.
@ -234,13 +234,14 @@ void LaunchStdinLoop(Config* config,
WorkThread::StartThread("stdin", [request_times]() { WorkThread::StartThread("stdin", [request_times]() {
auto* queue = QueueManager::instance(); auto* queue = QueueManager::instance();
while (true) {
std::unique_ptr<BaseIpcMessage> message = std::unique_ptr<BaseIpcMessage> message =
MessageRegistry::instance()->ReadMessageFromStdin( MessageRegistry::instance()->ReadMessageFromStdin(
g_log_stdin_stdout_to_stderr); g_log_stdin_stdout_to_stderr);
// Message parsing can fail if we don't recognize the method. // Message parsing can fail if we don't recognize the method.
if (!message) if (!message)
return WorkThread::Result::MoreWork; continue;
// Cache |method_id| so we can access it after moving |message|. // Cache |method_id| so we can access it after moving |message|.
IpcId method_id = message->method_id; IpcId method_id = message->method_id;
@ -292,8 +293,7 @@ void LaunchStdinLoop(Config* config,
} }
default: { default: {
LOG_S(ERROR) << "Unhandled IPC message " LOG_S(ERROR) << "Unhandled IPC message " << IpcIdToString(method_id);
<< IpcIdToString(method_id);
exit(1); exit(1);
} }
} }
@ -301,9 +301,8 @@ void LaunchStdinLoop(Config* config,
// If the message was to exit then querydb will take care of the actual // If the message was to exit then querydb will take care of the actual
// exit. Stop reading from stdin since it might be detached. // exit. Stop reading from stdin since it might be detached.
if (method_id == IpcId::Exit) if (method_id == IpcId::Exit)
return WorkThread::Result::ExitThread; break;
}
return WorkThread::Result::MoreWork;
}); });
} }
@ -312,11 +311,11 @@ void LaunchStdoutThread(std::unordered_map<IpcId, Timer>* request_times,
WorkThread::StartThread("stdout", [=]() { WorkThread::StartThread("stdout", [=]() {
auto* queue = QueueManager::instance(); auto* queue = QueueManager::instance();
while (true) {
std::vector<Stdout_Request> messages = queue->for_stdout.DequeueAll(); std::vector<Stdout_Request> messages = queue->for_stdout.DequeueAll();
if (messages.empty()) { if (messages.empty()) {
waiter->Wait({&queue->for_stdout}); waiter->Wait({&queue->for_stdout});
return queue->HasWork() ? WorkThread::Result::MoreWork continue;
: WorkThread::Result::NoWork;
} }
for (auto& message : messages) { for (auto& message : messages) {
@ -338,8 +337,7 @@ void LaunchStdoutThread(std::unordered_map<IpcId, Timer>* request_times,
std::cout << message.content; std::cout << message.content;
std::cout.flush(); std::cout.flush();
} }
}
return WorkThread::Result::MoreWork;
}); });
} }

View File

@ -20,7 +20,7 @@ bool QueryDb_ImportMain(Config* config,
SemanticHighlightSymbolCache* semantic_cache, SemanticHighlightSymbolCache* semantic_cache,
WorkingFiles* working_files); WorkingFiles* working_files);
WorkThread::Result IndexMain(Config* config, void IndexMain(Config* config,
FileConsumer::SharedState* file_consumer_shared, FileConsumer::SharedState* file_consumer_shared,
TimestampManager* timestamp_manager, TimestampManager* timestamp_manager,
ImportManager* import_manager, ImportManager* import_manager,

View File

@ -434,7 +434,7 @@ bool IndexMergeIndexUpdates() {
} }
} }
WorkThread::Result IndexMain(Config* config, void IndexMain(Config* config,
FileConsumer::SharedState* file_consumer_shared, FileConsumer::SharedState* file_consumer_shared,
TimestampManager* timestamp_manager, TimestampManager* timestamp_manager,
ImportManager* import_manager, ImportManager* import_manager,
@ -442,13 +442,14 @@ WorkThread::Result IndexMain(Config* config,
Project* project, Project* project,
WorkingFiles* working_files, WorkingFiles* working_files,
MultiQueueWaiter* waiter) { MultiQueueWaiter* waiter) {
// Build one index per-indexer, as building the index acquires a global lock.
ClangIndex index;
while (true) {
status->num_active_threads++; status->num_active_threads++;
EmitProgress(config); EmitProgress(config);
// Build one index per-indexer, as building the index acquires a global lock.
ClangIndex index;
// TODO: process all off IndexMain_DoIndex before calling // TODO: process all off IndexMain_DoIndex before calling
// IndexMain_DoCreateIndexUpdate for better icache behavior. We need to have // IndexMain_DoCreateIndexUpdate for better icache behavior. We need to have
// some threads spinning on both though otherwise memory usage will get bad. // some threads spinning on both though otherwise memory usage will get bad.
@ -481,9 +482,7 @@ WorkThread::Result IndexMain(Config* config,
waiter->Wait({&queue->index_request, &queue->on_id_mapped, waiter->Wait({&queue->index_request, &queue->on_id_mapped,
&queue->load_previous_index, &queue->on_indexed}); &queue->load_previous_index, &queue->on_indexed});
} }
}
return queue->HasWork() ? WorkThread::Result::MoreWork
: WorkThread::Result::NoWork;
} }
bool QueryDb_ImportMain(Config* config, bool QueryDb_ImportMain(Config* config,

View File

@ -136,8 +136,6 @@ void IncludeComplete::Rescan() {
timer.ResetAndPrint("[perf] Scanning for includes"); timer.ResetAndPrint("[perf] Scanning for includes");
is_scanning = false; is_scanning = false;
return WorkThread::Result::ExitThread;
}); });
} }

View File

@ -4,17 +4,14 @@
#include <loguru.hpp> #include <loguru.hpp>
namespace { namespace {
struct Ipc_CqueryWait struct Ipc_CqueryWait : public IpcMessage<Ipc_CqueryWait> {
: public IpcMessage<Ipc_CqueryWait> {
static constexpr IpcId kIpcId = IpcId::CqueryWait; static constexpr IpcId kIpcId = IpcId::CqueryWait;
}; };
MAKE_REFLECT_EMPTY_STRUCT(Ipc_CqueryWait); MAKE_REFLECT_EMPTY_STRUCT(Ipc_CqueryWait);
REGISTER_IPC_MESSAGE(Ipc_CqueryWait); REGISTER_IPC_MESSAGE(Ipc_CqueryWait);
struct CqueryWaitHandler : MessageHandler { struct CqueryWaitHandler : MessageHandler {
IpcId GetId() const override { IpcId GetId() const override { return IpcId::CqueryWait; }
return IpcId::CqueryWait;
}
void Run(std::unique_ptr<BaseIpcMessage> request) override { void Run(std::unique_ptr<BaseIpcMessage> request) override {
// TODO: use status message system here, then run querydb as normal? Maybe // TODO: use status message system here, then run querydb as normal? Maybe
// this cannot be a normal message, ie, it needs to be re-entrant. // this cannot be a normal message, ie, it needs to be re-entrant.

View File

@ -173,7 +173,7 @@ struct InitializeHandler : BaseMessageHandler<Ipc_InitializeRequest> {
LOG_S(INFO) << "Starting " << config->indexerCount << " indexers"; LOG_S(INFO) << "Starting " << config->indexerCount << " indexers";
for (int i = 0; i < config->indexerCount; ++i) { for (int i = 0; i < config->indexerCount; ++i) {
WorkThread::StartThread("indexer" + std::to_string(i), [=]() { WorkThread::StartThread("indexer" + std::to_string(i), [=]() {
return IndexMain(config, file_consumer_shared, timestamp_manager, IndexMain(config, file_consumer_shared, timestamp_manager,
import_manager, import_pipeline_status, project, import_manager, import_pipeline_status, project,
working_files, waiter); working_files, waiter);
}); });

View File

@ -4,15 +4,9 @@
// static // static
void WorkThread::StartThread(const std::string& thread_name, void WorkThread::StartThread(const std::string& thread_name,
const std::function<Result()>& entry_point) { std::function<void()> entry_point) {
new std::thread([thread_name, entry_point]() { new std::thread([thread_name, entry_point]() {
SetCurrentThreadName(thread_name); SetCurrentThreadName(thread_name);
entry_point();
// Main loop.
while (true) {
Result result = entry_point();
if (result == Result::ExitThread)
break;
}
}); });
} }

View File

@ -9,13 +9,10 @@
// Helper methods for starting threads that do some work. Enables test code to // Helper methods for starting threads that do some work. Enables test code to
// wait for all work to complete. // wait for all work to complete.
struct WorkThread { struct WorkThread {
// FIXME: remove result, have entry_point run a while(true) loop.
enum class Result { MoreWork, NoWork, ExitThread };
// Launch a new thread. |entry_point| will be called continously. It should // Launch a new thread. |entry_point| will be called continously. It should
// return true if it there is still known work to be done. // return true if it there is still known work to be done.
static void StartThread(const std::string& thread_name, static void StartThread(const std::string& thread_name,
const std::function<Result()>& entry_point); std::function<void()> entry_point);
// Static-only class. // Static-only class.
WorkThread() = delete; WorkThread() = delete;