From fab86f81620dd5a0eab8f82ba39d4fceaf6477d8 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 11 Apr 2025 20:53:40 -0400 Subject: [PATCH] watching fun --- .idea/editor.xml | 243 +++++++++++++++++++ .idea/inspectionProfiles/Project_Default.xml | 6 + CMakeLists.txt | 2 +- include/blt/gp/fwdecl.h | 4 - include/blt/gp/sync.h | 28 +++ lib/blt | 2 +- src/program.cpp | 8 + src/sync.cpp | 73 +++++- 8 files changed, 359 insertions(+), 7 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/editor.xml b/.idea/editor.xml index b465c40..8abdf90 100644 --- a/.idea/editor.xml +++ b/.idea/editor.xml @@ -1,7 +1,250 @@ + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..8bf9b9f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index c459ccd..26bcd99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ macro(compile_options target_name) sanitizers(${target_name}) endmacro() -project(blt-gp VERSION 0.5.0) +project(blt-gp VERSION 0.5.1) include(CTest) diff --git a/include/blt/gp/fwdecl.h b/include/blt/gp/fwdecl.h index 66cef46..40ee4ad 100644 --- a/include/blt/gp/fwdecl.h +++ b/include/blt/gp/fwdecl.h @@ -23,10 +23,6 @@ #include #include #include -#include -#include -#include -#include #include #include diff --git a/include/blt/gp/sync.h b/include/blt/gp/sync.h index cc987ed..8988b6e 100644 --- a/include/blt/gp/sync.h +++ b/include/blt/gp/sync.h @@ -19,9 +19,37 @@ #ifndef BLT_GP_SYNC_H #define BLT_GP_SYNC_H +#include +#include + namespace blt::gp { + class sync_t + { + public: + explicit sync_t(gp_program& program); + void trigger(u64 current_time); + + sync_t& with_timer(u64 seconds) + { + m_timer_seconds = seconds; + return *this; + } + + sync_t& every_generations(u64 generations) + { + m_generations = generations; + return *this; + } + + ~sync_t(); + + private: + gp_program* m_program; + std::optional m_timer_seconds; + std::optional m_generations; + }; } #endif //BLT_GP_SYNC_H diff --git a/lib/blt b/lib/blt index 78c219c..b6fc170 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit 78c219cc67f1fe6b3c7076e2c727f8f4cfffd859 +Subproject commit b6fc1703995195af611532cdffda52c86993e4ce diff --git a/src/program.cpp b/src/program.cpp index 2a0d6dd..7ac6018 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -56,6 +56,14 @@ namespace blt::gp return allocator; } + void gp_program::save_generation(fs::writer_t& writer) + { + } + + void gp_program::load_generation(fs::reader_t& reader) + { + } + void gp_program::save_state(fs::writer_t& writer) { } diff --git a/src/sync.cpp b/src/sync.cpp index 88ab06d..62a78de 100644 --- a/src/sync.cpp +++ b/src/sync.cpp @@ -16,8 +16,79 @@ * along with this program. If not, see . */ #include +#include +#include +#include namespace blt::gp { + struct global_sync_state_t + { + std::vector syncs; + std::mutex mutex; + std::thread* thread = nullptr; + std::atomic_bool should_run = true; + std::condition_variable condition_variable; -} \ No newline at end of file + void add(sync_t* sync) + { + if (thread == nullptr) + { + thread = new std::thread([this]() + { + while (should_run) + { + std::unique_lock lock(mutex); + condition_variable.wait_for(lock, std::chrono::milliseconds(100)); + const auto current_time = system::getCurrentTimeMilliseconds(); + for (const auto& sync : syncs) + sync->trigger(current_time); + } + }); + } + std::scoped_lock lock(mutex); + syncs.push_back(sync); + } + + void remove(const sync_t* sync) + { + if (thread == nullptr) + { + BLT_WARN("Tried to remove sync from global sync state, but no thread was running"); + return; + } + std::scoped_lock lock(mutex); + const auto iter = std::find(syncs.begin(), syncs.end(), sync); + std::iter_swap(iter, syncs.end() - 1); + syncs.pop_back(); + if (syncs.empty()) + { + should_run = false; + condition_variable.notify_all(); + thread->join(); + delete thread; + thread = nullptr; + } + } + }; + + global_sync_state_t& get_state() + { + static global_sync_state_t state; + return state; + } + + sync_t::sync_t(gp_program& program): m_program(&program) + { + get_state().add(this); + } + + void sync_t::trigger(u64 current_time) + { + } + + sync_t::~sync_t() + { + get_state().remove(this); + } +}