From d73457cd64c1501b282a8ce689436dc0bfea280a Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sat, 22 Dec 2018 05:17:54 +0700 Subject: [PATCH] deferrer: active_safe_wait_promise --- ROADMAP.md | 4 ++-- headers/enduro2d/core/deferrer.hpp | 15 +++++++++++++++ headers/enduro2d/high/_high.hpp | 5 +++++ headers/enduro2d/high/library.inl | 9 ++------- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 1a0319c1..ea623c53 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -91,13 +91,13 @@ - ### `basic high` - - [ ] `library` + - [x] `library` ``` resource management, abstract loaders groups, dependencies, async loading ``` - - [ ] `resources` + - [x] `resources` ``` meshes, shaders, textures, materials ``` diff --git a/headers/enduro2d/core/deferrer.hpp b/headers/enduro2d/core/deferrer.hpp index 7e056353..5fad9da6 100644 --- a/headers/enduro2d/core/deferrer.hpp +++ b/headers/enduro2d/core/deferrer.hpp @@ -30,6 +30,9 @@ namespace e2d , typename... Args , typename R = stdex::jobber::async_invoke_result_t > stdex::promise do_in_worker_thread(F&& f, Args&&... args); + + template < typename T > + void active_safe_wait_promise(const stdex::promise& promise); private: stdex::jobber worker_; stdex::scheduler scheduler_; @@ -47,4 +50,16 @@ namespace e2d stdex::promise deferrer::do_in_worker_thread(F&& f, Args&&... args) { return worker_.async(std::forward(f), std::forward(args)...); } + + template < typename T > + void deferrer::active_safe_wait_promise(const stdex::promise& promise) { + const auto zero_us = time::to_chrono(make_microseconds(0)); + while ( promise.wait_for(zero_us) == stdex::promise_wait_status::timeout ) { + if ( !is_in_main_thread() || 0 == scheduler_.process_one_task().second ) { + if ( 0 == worker_.active_wait_one().second ) { + std::this_thread::yield(); + } + } + } + } } diff --git a/headers/enduro2d/high/_high.hpp b/headers/enduro2d/high/_high.hpp index 327fe92e..26c61794 100644 --- a/headers/enduro2d/high/_high.hpp +++ b/headers/enduro2d/high/_high.hpp @@ -13,6 +13,7 @@ namespace e2d class asset; class library; class starter; + class text_asset; class mesh_asset; class image_asset; @@ -21,6 +22,10 @@ namespace e2d class texture_asset; class material_asset; + template < typename Asset, typename Content > + class content_asset; + template < typename T > class asset_cache; + class asset_cache_base; } diff --git a/headers/enduro2d/high/library.inl b/headers/enduro2d/high/library.inl index bd2689e1..9f4c2df4 100644 --- a/headers/enduro2d/high/library.inl +++ b/headers/enduro2d/high/library.inl @@ -21,13 +21,8 @@ namespace e2d std::shared_ptr library::load_asset(str_view address) { auto p = load_asset_async(address); - if ( modules::is_initialized() && the().is_in_main_thread() ) { - const auto zero_us = time::to_chrono(make_microseconds(0)); - while ( p.wait_for(zero_us) == stdex::promise_wait_status::timeout ) { - if ( 0 == the().scheduler().process_one_task().second ) { - std::this_thread::yield(); - } - } + if ( modules::is_initialized() ) { + the().active_safe_wait_promise(p); } return p.get_or_default(nullptr);