From 5faf9d1f6b54d89113eb0b0aac5ccb798e630fe8 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Thu, 23 Feb 2017 01:23:23 -0800 Subject: [PATCH] wip --- .gitmodules | 3 + indexer.cpp | 2 +- query.cc | 52 +-------------- query.h | 21 ++++++ task.cc | 111 +++++++++++++++++++++++++++++++ third_party/tiny-process-library | 1 + 6 files changed, 139 insertions(+), 51 deletions(-) create mode 100644 query.h create mode 100644 task.cc create mode 160000 third_party/tiny-process-library diff --git a/.gitmodules b/.gitmodules index b8c34ed0..787770b6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "third_party/rapidjson"] path = third_party/rapidjson url = https://github.com/miloyip/rapidjson +[submodule "third_party/tiny-process-library"] + path = third_party/tiny-process-library + url = https://github.com/eidheim/tiny-process-library/ diff --git a/indexer.cpp b/indexer.cpp index 3178b85e..87115b27 100644 --- a/indexer.cpp +++ b/indexer.cpp @@ -913,7 +913,7 @@ void WriteToFile(const std::string& filename, const std::string& content) { file << content; } -int main(int argc, char** argv) { +int main3(int argc, char** argv) { // TODO: Assert that we need to be on clang >= 3.9.1 /* diff --git a/query.cc b/query.cc index 37b53c2d..65ce5606 100644 --- a/query.cc +++ b/query.cc @@ -1,3 +1,5 @@ +#include "query.h" + #include #include #include @@ -54,37 +56,6 @@ struct QueryableDatabase { - -// Task running in a separate process, parsing a file into something we can -// import. -struct ParseTask {}; -// Completed parse task that wants to import content into the global database. -// Runs in main process, primary thread. Stops all other threads. -struct IndexImportTask {}; -// Completed parse task that wants to update content previously imported into -// the global database. Runs in main process, primary thread. Stops all other -// threads. -// -// Note that this task just contains a set of operations to apply to the global -// database. The operations come from a diff based on the previously indexed -// state in comparison to the newly indexed state. -// -// TODO: We may be able to run multiple freshen and import tasks in parallel if -// we restrict what ranges of the db they may change. -struct IndexFreshenTask {}; -// Task running a query against the global database. Run in main process, -// separate thread. -struct QueryTask {}; - - -// NOTE: When something enters a value into master db, it will have to have a -// ref count, since multiple parsings could enter it (unless we require -// that it be defined in that declaration unit!) -struct TaskManager { - -}; - - struct Query { }; @@ -109,12 +80,6 @@ struct DocumentDiff { -// NOTE: If updating this enum, make sure to also update the parser and the -// help text. -enum class PreferredSymbolLocation { - Declaration, - Definition -}; bool ParsePreferredSymbolLocation(const std::string& content, PreferredSymbolLocation* obj) { #define PARSE_AS(name, string) \ @@ -130,19 +95,6 @@ bool ParsePreferredSymbolLocation(const std::string& content, PreferredSymbolLoc #undef PARSE_AS } -// NOTE: If updating this enum, make sure to also update the parser and the -// help text. -enum class Command { - Callees, - Callers, - FindAllUsages, - FindInterestingUsages, - GotoReferenced, - Hierarchy, - Outline, - Search -}; - bool ParseCommand(const std::string& content, Command* obj) { #define PARSE_AS(name, string) \ if (content == #string) { \ diff --git a/query.h b/query.h new file mode 100644 index 00000000..39368922 --- /dev/null +++ b/query.h @@ -0,0 +1,21 @@ +#pragma once + +// NOTE: If updating this enum, make sure to also update the parser and the +// help text. +enum class Command { + Callees, + Callers, + FindAllUsages, + FindInterestingUsages, + GotoReferenced, + Hierarchy, + Outline, + Search +}; + +// NOTE: If updating this enum, make sure to also update the parser and the +// help text. +enum class PreferredSymbolLocation { + Declaration, + Definition +}; \ No newline at end of file diff --git a/task.cc b/task.cc new file mode 100644 index 00000000..431f6425 --- /dev/null +++ b/task.cc @@ -0,0 +1,111 @@ +#include +#include +#include +#include + +#include "indexer.h" +#include "query.h" + +#include "third_party/tiny-process-library/process.hpp" + +struct BaseTask { + int priority = 0; + bool writes_to_index = false; +}; + +// Task running in a separate process, parsing a file into something we can +// import. +struct IndexCreateTask : public BaseTask { + IndexCreateTask() { + writes_to_index = true; + } +}; + +// Completed parse task that wants to import content into the global database. +// Runs in main process, primary thread. Stops all other threads. +struct IndexImportTask : public BaseTask { + IndexImportTask() { + writes_to_index = true; + } +}; + +// Completed parse task that wants to update content previously imported into +// the global database. Runs in main process, primary thread. Stops all other +// threads. +// +// Note that this task just contains a set of operations to apply to the global +// database. The operations come from a diff based on the previously indexed +// state in comparison to the newly indexed state. +// +// TODO: We may be able to run multiple freshen and import tasks in parallel if +// we restrict what ranges of the db they may change. +struct IndexFreshenTask : public BaseTask { + IndexFreshenTask() { + writes_to_index = true; + } +}; + +// Task running a query against the global database. Run in main process, +// separate thread. +struct QueryTask : public BaseTask { + QueryTask() { + writes_to_index = false; + } + + Command query; + Location location; + std::string argument; +}; + + +// NOTE: When something enters a value into master db, it will have to have a +// ref count, since multiple parsings could enter it (unless we require +// that it be defined in that declaration unit!) +struct TaskManager { + // Tasks that are currently executing. + std::vector running; + std::vector pending; + + // Available threads. + std::vector threads; + std::condition_variable wakeup_thread; + std::mutex mutex; + + TaskManager(int num_threads); +}; + +static void ThreadMain(int id, std::condition_variable* waiter, std::mutex* mutex) { + std::unique_lock lock(*mutex); + waiter->wait(lock); + + std::cout << id << ": running in thread main" << std::endl; +} + +TaskManager::TaskManager(int num_threads) { + for (int i = 0; i < num_threads; ++i) { + threads.push_back(std::thread(&ThreadMain, i, &wakeup_thread, &mutex)); + } +} + +void Pump(TaskManager* tm) { + //tm->threads[0]. +} + +int main(int argc, char** argv) { + TaskManager tm(10); + + // TODO: looks like we will have to write shared memory support. + + // TODO: We signal thread to pick data, thread signals data pick is done. + // Repeat until we encounter a writer, wait for all threads to signal + // they are done. + // TODO: Let's use a thread safe queue/vector/etc instead. + tm.wakeup_thread.notify_one(); + tm.wakeup_thread.notify_one(); + + for (std::thread& thread : tm.threads) + thread.join(); + + std::cin.get(); + return 0; +} \ No newline at end of file diff --git a/third_party/tiny-process-library b/third_party/tiny-process-library new file mode 160000 index 00000000..876ce117 --- /dev/null +++ b/third_party/tiny-process-library @@ -0,0 +1 @@ +Subproject commit 876ce117e7c92458bd1322f39935f28419d87b20