diff --git a/src/noggit/AsyncLoader.cpp b/src/noggit/AsyncLoader.cpp index f9a29948..80a5691e 100755 --- a/src/noggit/AsyncLoader.cpp +++ b/src/noggit/AsyncLoader.cpp @@ -9,6 +9,14 @@ #include #include +AsyncLoader* AsyncLoader::instance; + +void AsyncLoader::setup(int threads) +{ + // make sure there's always at least one thread otherwise nothing can load + instance = new AsyncLoader(std::max(1, threads)); +} + bool AsyncLoader::is_loading() { std::lock_guard const lock (_guard); @@ -146,8 +154,8 @@ AsyncLoader::AsyncLoader(int numThreads) : _stop (false) { // use half of the available threads - unsigned int maxThreads = std::thread::hardware_concurrency() / 2; - numThreads = maxThreads > numThreads ? maxThreads : numThreads; + // unsigned int maxThreads = std::thread::hardware_concurrency() / 2; + // numThreads = maxThreads > numThreads ? maxThreads : numThreads; for (int i = 0; i < numThreads; ++i) { diff --git a/src/noggit/AsyncLoader.h b/src/noggit/AsyncLoader.h index 85638f3c..1e2d8120 100755 --- a/src/noggit/AsyncLoader.h +++ b/src/noggit/AsyncLoader.h @@ -14,11 +14,17 @@ class AsyncLoader { public: - static AsyncLoader& instance() - { - static AsyncLoader async_loader(2); - return async_loader; - } + // static AsyncLoader& instance() + // { + // static AsyncLoader async_loader(3); + // return async_loader; + // } + + // use regular pointer because unique_ptr was causing + // a significant performance hit + static AsyncLoader* instance; + + static void setup(int threads); //! Ownership is _not_ transferred. Call ensure_deletable to ensure //! that a previously enqueued object can be destroyed. diff --git a/src/noggit/AsyncObjectMultimap.hpp b/src/noggit/AsyncObjectMultimap.hpp index 3022908a..78f2a4e6 100755 --- a/src/noggit/AsyncObjectMultimap.hpp +++ b/src/noggit/AsyncObjectMultimap.hpp @@ -70,7 +70,7 @@ namespace Noggit }() ); - AsyncLoader::instance().queue_for_load(static_cast(obj)); + AsyncLoader::instance->queue_for_load(static_cast(obj)); return obj; } @@ -95,7 +95,7 @@ namespace Noggit // always make sure an async object can be deleted before deleting it if (!obj->finishedLoading()) { - AsyncLoader::instance().ensure_deletable(obj); + AsyncLoader::instance->ensure_deletable(obj); } { diff --git a/src/noggit/MapView.cpp b/src/noggit/MapView.cpp index 0423b8e8..50d2f2c4 100755 --- a/src/noggit/MapView.cpp +++ b/src/noggit/MapView.cpp @@ -3711,7 +3711,7 @@ MapView::~MapView() _world.reset(); - AsyncLoader::instance().reset_object_fail(); + AsyncLoader::instance->reset_object_fail(); Noggit::Ui::selected_texture::texture.reset(); @@ -5800,7 +5800,7 @@ void MapView::save(save_mode mode) // Save minimap creator model filters minimapTool->saveFiltersToJSON(); - if (AsyncLoader::instance().important_object_failed_loading()) + if (AsyncLoader::instance->important_object_failed_loading()) { save = false; QPushButton *yes, *no; @@ -5873,7 +5873,7 @@ void MapView::save(save_mode mode) NOGGIT_ACTION_MGR->purge(); - AsyncLoader::instance().reset_object_fail(); + AsyncLoader::instance->reset_object_fail(); _main_window->statusBar()->showMessage("Map saved", 2000); diff --git a/src/noggit/application/NoggitApplication.cpp b/src/noggit/application/NoggitApplication.cpp index dd0a8ae4..005414a8 100755 --- a/src/noggit/application/NoggitApplication.cpp +++ b/src/noggit/application/NoggitApplication.cpp @@ -204,6 +204,9 @@ namespace Noggit::Application //All of the below should be Project Initalisation srand(::time(nullptr)); + // TODO : thread count setting + // AsyncLoader::setup(NoggitSettings.value("async_thread_count", 3).toInt()); + AsyncLoader::setup(3); } std::shared_ptr NoggitApplication::getConfiguration() diff --git a/src/noggit/map_index.cpp b/src/noggit/map_index.cpp index c89a5ecd..7c59a3c6 100755 --- a/src/noggit/map_index.cpp +++ b/src/noggit/map_index.cpp @@ -384,7 +384,7 @@ MapTile* MapIndex::loadTile(const TileIndex& tile, bool reloading, bool load_mod MapTile* adt = mTiles[tile.z][tile.x].tile.get(); - AsyncLoader::instance().queue_for_load(adt); + AsyncLoader::instance->queue_for_load(adt); _n_loaded_tiles++; return adt; @@ -437,7 +437,7 @@ void MapIndex::unloadTile(const TileIndex& tile) // otherwise it can be deleted before the log because it comes from the adt itself (see unloadTiles) Log << "Unloading Tile " << tile.x << "-" << tile.z << std::endl; - AsyncLoader::instance().ensure_deletable(mTiles[tile.z][tile.x].tile.get()); + AsyncLoader::instance->ensure_deletable(mTiles[tile.z][tile.x].tile.get()); mTiles[tile.z][tile.x].tile.reset(); _n_loaded_tiles--; } diff --git a/src/noggit/rendering/WorldRender.cpp b/src/noggit/rendering/WorldRender.cpp index f3f8ac3a..25501519 100755 --- a/src/noggit/rendering/WorldRender.cpp +++ b/src/noggit/rendering/WorldRender.cpp @@ -1701,7 +1701,7 @@ bool WorldRender::saveMinimap(TileIndex const& tile_idx, MinimapRenderSettings* unsigned counter = 0; constexpr unsigned TIMEOUT = 5000; - while (AsyncLoader::instance().is_loading() || !mTile->finishedLoading()) + while (AsyncLoader::instance->is_loading() || !mTile->finishedLoading()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); counter++; diff --git a/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.cpp b/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.cpp index 5b3ec88a..efa53a19 100755 --- a/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.cpp +++ b/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.cpp @@ -436,15 +436,15 @@ QPixmap* PreviewRenderer::renderToPixmap() tick(1.0f); draw(); - auto& async_loader = AsyncLoader::instance(); + auto async_loader = AsyncLoader::instance; - if (async_loader.is_loading()) + if (async_loader->is_loading()) { // wait for the loader to finish do { std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } while (async_loader.is_loading()); + } while (async_loader->is_loading()); // redraw gl.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);