From e1c8cd4430015a307dbcb32894a030d5a13aee67 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 14 Oct 2021 16:59:27 +0200 Subject: Remove async image loader threads and instead check if the curl download process has finished --- include/AsyncImageLoader.hpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'include/AsyncImageLoader.hpp') diff --git a/include/AsyncImageLoader.hpp b/include/AsyncImageLoader.hpp index 6258b02..77fd5b4 100644 --- a/include/AsyncImageLoader.hpp +++ b/include/AsyncImageLoader.hpp @@ -26,11 +26,19 @@ namespace QuickMedia { size_t counter = 0; }; + struct ThumbnailLoadData { + Path path; + Path thumbnail_path; + bool local; + std::shared_ptr thumbnail_data; + sf::Vector2i resize_target_size; + }; + // If |symlink_if_no_resize| is false then a copy is made from |thumbnail_path| to |thumbnail_path_resized| instead of a symlink if |thumbnail_path| is not larger than |resize_target_size|. // One example of why you might not want a symlink is if |thumbnail_path| is a temporary file. bool create_thumbnail(const Path &thumbnail_path, const Path &thumbnail_path_resized, sf::Vector2i resize_target_size, ContentType content_type, bool symlink_if_no_resize); - constexpr int NUM_IMAGE_LOAD_THREADS = 4; + constexpr int NUM_IMAGE_LOAD_PARALLEL = 4; class AsyncImageLoader { public: @@ -51,14 +59,6 @@ namespace QuickMedia { AsyncImageLoader(AsyncImageLoader &other) = delete; AsyncImageLoader& operator=(AsyncImageLoader &other) = delete; - struct ThumbnailLoadData { - Path path; - Path thumbnail_path; - bool local; - std::shared_ptr thumbnail_data; - sf::Vector2i resize_target_size; - }; - // set |resize_target_size| to {0, 0} to disable resizing. // Note: this method is not thread-safe void load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, std::shared_ptr thumbnail_data); @@ -66,10 +66,19 @@ namespace QuickMedia { // Returns -1 if all threads are busy int get_free_load_index() const; private: - bool loading_image[NUM_IMAGE_LOAD_THREADS]; + struct Download { + ReadProgram read_program; + int64_t download_start = 0; + Path thumbnail_path; + std::shared_ptr thumbnail_data; + sf::Vector2i resize_target_size; + std::string url; + }; + + std::mutex download_mutex; // TODO: Use curl single-threaded multi-download feature instead - AsyncTask download_image_thread[NUM_IMAGE_LOAD_THREADS]; - AsyncTask load_image_thread; + Download downloads[NUM_IMAGE_LOAD_PARALLEL]; + AsyncTask load_thread; MessageQueue image_load_queue; std::unordered_map> thumbnails; size_t counter = 0; -- cgit v1.2.3