fix checkbox icon update, add unique check on async tasks
This commit is contained in:
47
src/app.h
47
src/app.h
@@ -59,6 +59,20 @@ struct VRController
|
||||
virtual float get_trigger_value() const { return 1.f; }
|
||||
};
|
||||
|
||||
struct AppTask : public std::packaged_task<void()>
|
||||
{
|
||||
size_t task_id;
|
||||
#ifdef _DEBUG
|
||||
std::string name;
|
||||
#endif
|
||||
template<typename F> AppTask(F f) : std::packaged_task<void()>(f)
|
||||
{
|
||||
task_id = typeid(f).hash_code();
|
||||
#ifdef _DEBUG
|
||||
name = typeid(f).name();
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
class App
|
||||
{
|
||||
@@ -177,7 +191,6 @@ public:
|
||||
void vr_digital(const VRController& c, VRController::kButton b, VRController::kAction a, glm::vec2 axis);
|
||||
void vr_draw_ui();
|
||||
void async_start();
|
||||
void async_update();
|
||||
void async_redraw();
|
||||
void async_swap();
|
||||
void async_end();
|
||||
@@ -257,7 +270,7 @@ public:
|
||||
// RENDER THREAD
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static std::deque<std::packaged_task<void()>> render_tasklist;
|
||||
static std::deque<AppTask> render_tasklist;
|
||||
static std::mutex render_task_mutex;
|
||||
static std::condition_variable render_cv;
|
||||
static std::thread render_thread;
|
||||
@@ -275,10 +288,10 @@ public:
|
||||
// don't capture a reference to this ptr as the object may be destroyed
|
||||
// by the time the task is executed
|
||||
template<typename T, typename R = void>
|
||||
std::future<R> render_task_async(T task)
|
||||
std::future<R> render_task_async(T task, bool unique = false)
|
||||
{
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
AppTask pt(task);
|
||||
auto f = pt.get_future();
|
||||
if (is_render_thread())
|
||||
{
|
||||
pt();
|
||||
@@ -287,6 +300,10 @@ public:
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(render_task_mutex);
|
||||
// remove any previously queued task from the same lambda
|
||||
if (unique && !render_tasklist.empty())
|
||||
render_tasklist.erase(std::remove_if(render_tasklist.begin(), render_tasklist.end(),
|
||||
[id = pt.task_id](AppTask const& t){ return t.task_id == id; }), render_tasklist.end());
|
||||
render_tasklist.push_back(std::move(pt));
|
||||
}
|
||||
render_cv.notify_all();
|
||||
@@ -297,8 +314,8 @@ public:
|
||||
template<typename T, typename R = void>
|
||||
R render_task(T task)
|
||||
{
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
AppTask pt(task);
|
||||
auto f = pt.get_future();
|
||||
if (is_render_thread())
|
||||
{
|
||||
pt();
|
||||
@@ -323,7 +340,7 @@ public:
|
||||
// UI THREAD
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static std::deque<std::packaged_task<void()>> ui_tasklist;
|
||||
static std::deque<AppTask> ui_tasklist;
|
||||
static std::mutex ui_task_mutex;
|
||||
static std::condition_variable ui_cv;
|
||||
static std::thread ui_thread;
|
||||
@@ -341,10 +358,10 @@ public:
|
||||
// don't capture a reference to this ptr as the object may be destroyed
|
||||
// by the time the task is executed
|
||||
template<typename T, typename R = void>
|
||||
std::future<R> ui_task_async(T task)
|
||||
std::future<R> ui_task_async(T task, bool unique = false)
|
||||
{
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
AppTask pt(task);
|
||||
auto f = pt.get_future();
|
||||
if (is_ui_thread())
|
||||
{
|
||||
pt();
|
||||
@@ -353,6 +370,10 @@ public:
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ui_task_mutex);
|
||||
// remove any previously queued task from the same lambda
|
||||
if (unique && !ui_tasklist.empty())
|
||||
ui_tasklist.erase(std::remove_if(ui_tasklist.begin(), ui_tasklist.end(),
|
||||
[id = pt.task_id](AppTask const& t){ return t.task_id == id; }), ui_tasklist.end());
|
||||
ui_tasklist.push_back(std::move(pt));
|
||||
}
|
||||
ui_cv.notify_all();
|
||||
@@ -363,8 +384,8 @@ public:
|
||||
template<typename T, typename R = void>
|
||||
R ui_task(T task)
|
||||
{
|
||||
std::packaged_task<R()> pt(task);
|
||||
std::future<R> f = pt.get_future();
|
||||
AppTask pt(task);
|
||||
auto f = pt.get_future();
|
||||
if (is_ui_thread())
|
||||
{
|
||||
pt();
|
||||
|
||||
Reference in New Issue
Block a user