X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fresourcemanager.h;h=5cc427f729cabaa4ec15eec927283d38297f4a68;hb=5f84c8caeb789571503b5491f941c8ec31b700fe;hp=60f61a421fe4b2b54685a2366cd339e9bfa9be2d;hpb=4d7f66ea28c788e12f700216b9c53af9e71b8390;p=libs%2Fgl.git diff --git a/source/resourcemanager.h b/source/resourcemanager.h index 60f61a42..5cc427f7 100644 --- a/source/resourcemanager.h +++ b/source/resourcemanager.h @@ -11,6 +11,16 @@ namespace Msp { namespace GL { +class ResourceWatcher; + +class resource_load_error: public std::runtime_error +{ +public: + resource_load_error(const std::string &, const std::exception &); + virtual ~resource_load_error() throw() { } +}; + + class ResourceManager { public: @@ -21,60 +31,84 @@ 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; IO::Seekable *io; Resource::AsyncLoader *loader; - bool loaded; + State state; unsigned last_used; UInt64 data_size; + std::vector watchers; ManagedResource(Resource &); void start_loading(); + bool process(bool); + void finish_loading(bool); void finish_loading(); void unload(); + + void add_watcher(ResourceWatcher &); + 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; + volatile bool done; public: - LoadingThread(ResourceManager &); + LoadingThread(); private: virtual void main(); + ManagedResource *front(LoadQueue &); + + public: + void add_resource(ManagedResource &); + void remove_resource(ManagedResource &); + private: + bool try_remove_resource(ManagedResource &); public: - void set_resource(ManagedResource *); - ManagedResource *get_resource() const { return resource; } - void sync(); - State get_state() const { return state; } + bool sync(); + bool needs_work() const { return size ResourceMap; - typedef std::list LoadQueue; LoadingPolicy policy; bool async_loads; @@ -100,12 +134,26 @@ public: void add_resource(Resource &); void *get_data_for_resource(const Resource &); 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 &); + void watch_resource(const Resource &, ResourceWatcher &); + void unwatch_resource(const Resource &, ResourceWatcher &); + void tick(); +private: + void dispatch_work(); + void unload_by_age(); + void unload_by_size(); +public: UInt64 get_total_data_size() const; + +private: + static bool age_order(ManagedResource *, ManagedResource *); }; } // namespace GL