Skip to content

Commit

Permalink
rpp toying to create background threads for processing
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstink committed Dec 29, 2024
1 parent 5c48e3b commit 1ef7b50
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 42 deletions.
1 change: 1 addition & 0 deletions symmetri/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(APP_SOURCES
draw_menu.cpp
draw_ui.cpp
draw_about.cpp
draw_popups.cpp
simulation_menu.cpp
model.cpp
load_file.cpp
Expand Down
2 changes: 1 addition & 1 deletion symmetri/gui/draw_about.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void draw_about(const model::ViewModel&) {
AlignForWidth(width);

if (ImGui::Button("Ok")) {
removeAboutView();
removeView(&draw_about);
}
ImGui::SameLine();
ImGui::InvisibleButton("Fixed", ImVec2(100.0f, 1.0f)); // Fixed size
Expand Down
20 changes: 20 additions & 0 deletions symmetri/gui/draw_popups.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "draw_popups.h"

#include "reducers.h"

const auto no_move_draw_resize =
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize;

void draw_confirmation_popup(const model::ViewModel&) {
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));

ImGui::Begin("About", NULL, no_move_draw_resize);
ImGui::Text("Are you sure you want to save to ");

if (ImGui::Button("Ok")) {
removeView(&draw_confirmation_popup);
}
ImGui::End();
}
3 changes: 3 additions & 0 deletions symmetri/gui/draw_popups.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#include "model.h"
void draw_confirmation_popup(const model::ViewModel& vm);
3 changes: 2 additions & 1 deletion symmetri/gui/menu_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "imgui.h"
#include "imfilebrowser.h"
// clang-format on
#include "draw_about.h"
#include "load_file.h"
#include "reducers.h"
#include "rxdispatch.h"
Expand Down Expand Up @@ -50,7 +51,7 @@ void draw_menu_bar(const model::ViewModel &vm) {

if (ImGui::BeginMenu("Help")) {
if (ImGui::MenuItem("About")) {
addAboutView();
addView(&draw_about);
}
ImGui::EndMenu();
}
Expand Down
2 changes: 0 additions & 2 deletions symmetri/gui/metal_main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ int main(int, char **) {
io.Fonts->AddFontFromFileTTF(
"/Users/thomashorstink/Projects/Symmetri/symmetri/gui/imgui/misc/fonts/CascadiaCode.ttf", 15);

// ImGui::GetStyle().ScaleAllSizes(1.0f);

io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls

Expand Down
10 changes: 1 addition & 9 deletions symmetri/gui/reactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
#include "model.h"
#include "rpp/rpp.hpp"
#include "rxdispatch.h"

namespace rximgui {
rpp::subjects::publish_subject<int> framebus;
auto frameout = framebus.get_observer();
auto sendframe = []() { frameout.on_next(1); };
auto frames = framebus.get_observable();
inline rpp::schedulers::run_loop rl{};

} // namespace rximgui
#include "rximgui.h"

auto go() {
using namespace rximgui;
Expand Down
15 changes: 0 additions & 15 deletions symmetri/gui/reducers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <ranges>
#include <tuple>

#include "draw_about.h"
#include "draw_context_menu.h"
#include "imgui_internal.h"
#include "petri.h"
Expand Down Expand Up @@ -350,17 +349,3 @@ void updateTransitionOutputColor(size_t transition_idx, symmetri::Token color) {
return m;
});
}

void addAboutView() {
rxdispatch::push([=](model::Model&& m) {
m.data->drawables.push_back(&draw_about);
return m;
});
}

void removeAboutView() {
rxdispatch::push([=](model::Model&& m) {
std::erase(m.data->drawables, &draw_about);
return m;
});
}
33 changes: 30 additions & 3 deletions symmetri/gui/reducers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <future>
#include <memory>

#include "imgui.h"
#include "rxdispatch.h"
#include "symmetri/colors.hpp"
Expand Down Expand Up @@ -56,6 +59,30 @@ void tryFire(size_t transition_idx);

void updateTransitionOutputColor(size_t transition_idx, symmetri::Token color);

void addAboutView();

void removeAboutView();
auto& addViewBlocking(auto&& v) {
auto accumulate_promise = std::make_shared<std::promise<void>>();
static auto fut = accumulate_promise->get_future();
rxdispatch::push([=]() {
return [=](model::Model&& m) {
m.data->drawables.push_back(std::move(v));
accumulate_promise->set_value();
return m;
};
});
return fut;
// return std::move(fut);
};

void addView(auto&& v) {
rxdispatch::push([=](model::Model&& m) {
m.data->drawables.push_back(std::move(v));
return m;
});
};

void removeView(auto&& v) {
rxdispatch::push([=](model::Model&& m) {
std::erase(m.data->drawables, std::move(v));
return m;
});
};
8 changes: 3 additions & 5 deletions symmetri/gui/rxdispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
#include "blockingconcurrentqueue.h"
namespace rxdispatch {

static moodycamel::BlockingConcurrentQueue<model::Reducer> reducer_queue{10};
static moodycamel::BlockingConcurrentQueue<Update> reducer_queue{10};

moodycamel::BlockingConcurrentQueue<model::Reducer>& getQueue() {
moodycamel::BlockingConcurrentQueue<Update>& getQueue() {
return reducer_queue;
}

void unsubscribe() {
push(model::Reducer([](auto&& m) { return m; }));
}

void push(model::Reducer&& r) {
reducer_queue.enqueue(std::forward<model::Reducer>(r));
}
void push(Update&& r) { reducer_queue.enqueue(std::forward<Update>(r)); }

} // namespace rxdispatch
39 changes: 33 additions & 6 deletions symmetri/gui/rxdispatch.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
#pragma once

#include <functional>
#include <iostream>
#include <variant>

#include "model.h"
#include "rpp/rpp.hpp"
#include "rximgui.h"

namespace rxdispatch {

using Update =
std::variant<model::Reducer, std::function<model::Reducer(void)>>;

void unsubscribe();
moodycamel::BlockingConcurrentQueue<model::Reducer>& getQueue();
void push(model::Reducer&& r);
moodycamel::BlockingConcurrentQueue<Update>& getQueue();
void push(Update&& r);

struct VisitPackage {
auto operator()(model::Reducer r) {
return rpp::source::just(rximgui::rl, std::move(r)).as_dynamic();
}
auto operator()(std::function<model::Reducer(void)> f) {
return (rpp::source::just(scheduler, std::move(f)) |
rpp::operators::map([](auto f) { return f(); }))
.as_dynamic();
}

private:
inline static rpp::schedulers::thread_pool scheduler{4};
};

inline auto get_events_observable() {
return rpp::source::create<model::Reducer>([](auto&& observer) {
model::Reducer f;
return rpp::source::create<Update>([](auto&& observer) {
Update f;
auto mainthreadid = std::this_thread::get_id();
while (not observer.is_disposed()) {
std::cout << "dispatch " << mainthreadid << std::endl;
getQueue().wait_dequeue(f);
observer.on_next(std::move(f));
observer.on_next(std::forward<Update>(f));
}
}) |
rpp::operators::subscribe_on(rpp::schedulers::thread_pool{3});
rpp::operators::subscribe_on(rpp::schedulers::new_thread{}) |
rpp::operators::flat_map(
[](auto value) { return std::visit(VisitPackage(), value); }) |
rpp::operators::tap([](auto&&) {
std::cout << "tap " << std::this_thread::get_id() << std::endl;
});
}

} // namespace rxdispatch
12 changes: 12 additions & 0 deletions symmetri/gui/rximgui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "rpp/rpp.hpp"

namespace rximgui {
static inline rpp::subjects::publish_subject<int> framebus;
static inline auto frameout = framebus.get_observer();
static inline auto sendframe = []() { frameout.on_next(1); };
static inline auto frames = framebus.get_observable();
static inline rpp::schedulers::run_loop rl{};

} // namespace rximgui

0 comments on commit 1ef7b50

Please sign in to comment.