]> git.tdb.fi Git - libs/gl.git/commitdiff
Delay opening the resource file until loading actually starts
authorMikko Rasa <tdb@tdb.fi>
Mon, 29 Sep 2014 16:40:48 +0000 (19:40 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 29 Sep 2014 16:40:48 +0000 (19:40 +0300)
Opening it when the load is queued can cause large numbers of open files
to be created at once and dissipate slowly as the queue is processed.

source/resourcemanager.cpp
source/resourcemanager.h

index cdd032264345dae5d81d11bf17f10e3da9ed09d3..c9cbcbb713c7126ba54025405e3ec4ccc088424f 100644 (file)
@@ -78,15 +78,17 @@ void ResourceManager::load_resource(Resource &r)
        if(!managed.collection)
                throw runtime_error("no location");
 
-       if(managed.loader)
+       if(managed.state!=ManagedResource::NOT_LOADED)
                return;
 
-       managed.start_loading();
-
        if(async_loads)
+       {
+               managed.state = ManagedResource::LOAD_QUEUED;
                queue.push_back(&managed);
+       }
        else
        {
+               managed.start_loading();
                while(!managed.loader->process()) ;
                managed.finish_loading();
        }
@@ -95,7 +97,7 @@ void ResourceManager::load_resource(Resource &r)
 void ResourceManager::resource_used(const Resource &r)
 {
        ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
-       if(!managed->loaded && !managed->loader && policy!=LOAD_MANUALLY)
+       if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
                load_resource(*managed->resource);
 
        managed->last_used = frame;
@@ -145,7 +147,7 @@ void ResourceManager::tick()
                next_unload = 0;
 
                for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
-                       if(i->second.loaded)
+                       if(i->second.state==ManagedResource::LOADED)
                        {
                                if(i->second.last_used<=unload_limit)
                                        i->second.unload();
@@ -165,7 +167,7 @@ void ResourceManager::tick()
                        ManagedResource *best = 0;
                        UInt64 best_impact = 0;
                        for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
-                               if(i->second.loaded && i->second.last_used<unload_limit)
+                               if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
                                {
                                        UInt64 impact = i->second.data_size*(frame-i->second.last_used);
                                        if(!best || impact>best_impact)
@@ -187,7 +189,7 @@ UInt64 ResourceManager::get_total_data_size() const
 {
        UInt64 total = 0;
        for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
-               if(i->second.loaded)
+               if(i->second.state==ManagedResource::LOADED)
                        total += i->second.data_size;
        return total;
 }
@@ -198,7 +200,7 @@ ResourceManager::ManagedResource::ManagedResource(Resource &r):
        collection(0),
        io(0),
        loader(0),
-       loaded(false),
+       state(NOT_LOADED),
        last_used(0),
        data_size(0)
 { }
@@ -213,13 +215,14 @@ void ResourceManager::ManagedResource::start_loading()
                io = 0;
                throw logic_error("no loader created");
        }
+       state = LOADING;
 }
 
 void ResourceManager::ManagedResource::finish_loading()
 {
        delete loader;
        loader = 0;
-       loaded = true;
+       state = LOADED;
        delete io;
        io = 0;
        data_size = resource->get_data_size();
@@ -231,7 +234,7 @@ void ResourceManager::ManagedResource::finish_loading()
 void ResourceManager::ManagedResource::unload()
 {
        resource->unload();
-       loaded = false;
+       state = NOT_LOADED;
 
        for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
                (*i)->resource_unloaded(*resource);
@@ -292,6 +295,7 @@ void ResourceManager::LoadingThread::set_resource(ManagedResource *r)
        }
 
        resource = r;
+       resource->start_loading();
        state = BUSY;
        sem.signal();
 }
index f5ccac3bbf9a212eefe8611a865bb25a059737b2..244c67ec29cc5acda96ea303bddda0f7583cf1ee 100644 (file)
@@ -26,12 +26,20 @@ public:
 private:
        struct ManagedResource
        {
+               enum ResourceState
+               {
+                       NOT_LOADED,
+                       LOAD_QUEUED,
+                       LOADING,
+                       LOADED
+               };
+
                Resource *resource;
                DataFile::Collection *collection;
                std::string name;
                IO::Seekable *io;
                Resource::AsyncLoader *loader;
-               bool loaded;
+               ResourceState state;
                unsigned last_used;
                UInt64 data_size;
                std::vector<ResourceWatcher *> watchers;