X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fresourcemanager.h;h=afa2c2d073e45b32b25332a91a3be9c7c8568461;hp=f5ccac3bbf9a212eefe8611a865bb25a059737b2;hb=cfd4d36c2b6b6095ada3aef8082e5d409a233a21;hpb=3c09b67afba6d560eab5817016a98268e1544bc4 diff --git a/source/resourcemanager.h b/source/resourcemanager.h index f5ccac3b..afa2c2d0 100644 --- a/source/resourcemanager.h +++ b/source/resourcemanager.h @@ -13,6 +13,15 @@ namespace GL { class ResourceWatcher; +class resource_load_error: public std::runtime_error +{ +public: + resource_load_error(const std::string &, const std::string &); + resource_load_error(const std::string &, const std::exception &); + virtual ~resource_load_error() throw() { } +}; + + class ResourceManager { public: @@ -23,15 +32,34 @@ public: LOAD_MANUALLY }; + struct ResourceLocation + { + DataFile::Collection *collection; + std::string name; + + ResourceLocation(); + ResourceLocation(DataFile::Collection &, const std::string &); + }; + private: struct ManagedResource { + enum State + { + NOT_LOADED, + LOAD_QUEUED, + LOADING, + LOAD_FINISHED, + LOADED, + LOAD_ERROR + }; + Resource *resource; - DataFile::Collection *collection; - std::string name; + ResourceLocation location; + bool load_priority; IO::Seekable *io; Resource::AsyncLoader *loader; - bool loaded; + State state; unsigned last_used; UInt64 data_size; std::vector watchers; @@ -39,6 +67,8 @@ private: ManagedResource(Resource &); void start_loading(); + bool process(bool); + void finish_loading(bool); void finish_loading(); void unload(); @@ -46,46 +76,51 @@ private: void remove_watcher(ResourceWatcher &); }; + typedef std::list LoadQueue; + class LoadingThread: public Thread { - public: - enum State - { - IDLE, - SYNC_PENDING, - BUSY, - LOAD_FINISHED, - TERMINATING - }; - private: - ResourceManager &manager; Semaphore sem; - ManagedResource *volatile resource; - volatile State state; + Mutex queue_mutex; + LoadQueue async_queue; + LoadQueue sync_queue; + unsigned capacity; + unsigned size; + std::list error_queue; + Mutex data_size_mutex; + UInt64 loaded_data_size; + volatile bool done; public: - LoadingThread(ResourceManager &); + LoadingThread(); private: virtual void main(); + ManagedResource *front(LoadQueue &); + public: - void set_resource(ManagedResource *); - ManagedResource *get_resource() const { return resource; } - void sync(); - State get_state() const { return state; } + void add_resource(ManagedResource &); + void remove_resource(ManagedResource &); + private: + bool try_remove_resource(ManagedResource &); + public: + bool sync(); + bool needs_work() const { return size ResourceMap; - typedef std::list LoadQueue; LoadingPolicy policy; bool async_loads; + mutable Mutex map_mutex; ResourceMap resources; LoadQueue queue; + UInt64 total_data_size; UInt64 size_limit; unsigned frame; unsigned min_retain_frames; @@ -104,9 +139,16 @@ public: void set_max_retain_frames(unsigned); void add_resource(Resource &); - void *get_data_for_resource(const Resource &); +private: + const ManagedResource &get_managed_resource(const Resource &) const; + ManagedResource &get_managed_resource(const Resource &); +public: + void *get_data_for_resource(const Resource &r) { return &get_managed_resource(r); } void set_resource_location(Resource &, DataFile::Collection &, const std::string &); + void set_resource_location(Resource &, const ResourceLocation &); + const ResourceLocation *get_resource_location(const Resource &) const; void load_resource(Resource &); + bool is_resource_loaded(const Resource &) const; void resource_used(const Resource &); void remove_resource(Resource &); @@ -114,7 +156,15 @@ public: void unwatch_resource(const Resource &, ResourceWatcher &); void tick(); - UInt64 get_total_data_size() const; +private: + void dispatch_work(); + void unload_by_age(); + void unload_by_size(); +public: + UInt64 get_total_data_size() const { return total_data_size; } + +private: + static bool age_order(ManagedResource *, ManagedResource *); }; } // namespace GL