3 #include <msp/debug/demangle.h>
4 #include <msp/strings/format.h>
5 #include <msp/time/utils.h>
6 #include "resourcemanager.h"
7 #include "resourcewatcher.h"
14 resource_load_error::resource_load_error(const string &name, const exception &exc):
15 runtime_error(format("%s: %s: %s", name, Debug::demangle(typeid(exc).name()), exc.what()))
19 ResourceManager::ResourceManager():
20 policy(LOAD_ON_DEMAND),
24 min_retain_frames(30),
29 ResourceManager::~ResourceManager()
33 while(!resources.empty())
34 resources.begin()->second.resource->set_manager(0);
37 void ResourceManager::set_loading_policy(LoadingPolicy p)
42 void ResourceManager::set_async_loads(bool a)
47 void ResourceManager::set_size_limit(UInt64 s)
52 void ResourceManager::set_min_retain_frames(unsigned f)
54 min_retain_frames = f;
57 void ResourceManager::set_max_retain_frames(unsigned f)
59 max_retain_frames = f;
62 void ResourceManager::add_resource(Resource &r)
64 insert_unique(resources, &r, ManagedResource(r));
67 void *ResourceManager::get_data_for_resource(const Resource &r)
69 return &get_item(resources, &r);
72 void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
74 set_resource_location(r, ResourceLocation(c, n));
77 void ResourceManager::set_resource_location(Resource &r, const ResourceLocation &l)
79 ManagedResource &managed = get_item(resources, &r);
82 if(policy==LOAD_IMMEDIATELY)
86 const ResourceManager::ResourceLocation *ResourceManager::get_resource_location(const Resource &r) const
88 const ManagedResource &managed = get_item(resources, &r);
89 return managed.location.collection ? &managed.location : 0;
92 void ResourceManager::load_resource(Resource &r)
94 ManagedResource &managed = get_item(resources, &r);
95 if(!managed.location.collection)
96 throw runtime_error("no location");
98 if(managed.state!=ManagedResource::NOT_LOADED)
103 managed.state = ManagedResource::LOAD_QUEUED;
104 queue.push_back(&managed);
108 managed.start_loading();
109 while(!managed.loader->process()) ;
110 managed.finish_loading(true);
114 bool ResourceManager::is_resource_loaded(const Resource &r) const
116 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
117 return managed ? managed->state==ManagedResource::LOADED : false;
120 void ResourceManager::resource_used(const Resource &r)
122 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
123 if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
124 load_resource(*managed->resource);
126 managed->last_used = frame;
127 if(max_retain_frames && !next_unload)
128 next_unload = frame+max_retain_frames+1;
131 void ResourceManager::remove_resource(Resource &r)
133 ManagedResource &managed = get_item(resources, &r);
134 ManagedResource::State state = managed.state;
135 if(state==ManagedResource::LOAD_QUEUED)
137 LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
141 else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
142 thread.remove_resource(managed);
143 remove_existing(resources, &r);
146 void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
148 get_item(resources, &r).add_watcher(w);
151 void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
153 get_item(resources, &r).remove_watcher(w);
156 void ResourceManager::tick()
160 bool do_unload = (frame>=next_unload);
164 if(thread.needs_work() && !queue.empty())
169 if(max_retain_frames && frame>=next_unload)
174 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
175 if(i->second.state==ManagedResource::LOADED)
176 next_unload = min(next_unload, i->second.last_used);
177 next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
185 void ResourceManager::dispatch_work()
187 queue.sort(age_order);
189 if(queue.front()->last_used+10<frame)
191 for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
192 (*i)->state = ManagedResource::NOT_LOADED;
197 while(thread.needs_work() && !queue.empty())
199 ManagedResource *managed = queue.front();
201 thread.add_resource(*managed);
205 void ResourceManager::unload_by_age()
207 unsigned unload_limit = frame-max_retain_frames;
209 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
210 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
214 void ResourceManager::unload_by_size()
216 unsigned unload_limit = frame-min_retain_frames;
218 while(get_total_data_size()>size_limit)
220 ManagedResource *best = 0;
221 UInt64 best_impact = 0;
222 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
223 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
225 UInt64 impact = i->second.data_size*(frame-i->second.last_used);
226 if(!best || impact>best_impact)
229 best_impact = impact;
240 UInt64 ResourceManager::get_total_data_size() const
243 for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
244 if(i->second.state==ManagedResource::LOADED)
245 total += i->second.data_size;
249 bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
251 return mr1->last_used>mr2->last_used;
255 ResourceManager::ResourceLocation::ResourceLocation():
259 ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
265 ResourceManager::ManagedResource::ManagedResource(Resource &r):
274 void ResourceManager::ManagedResource::start_loading()
276 io = location.collection->open_raw(location.name);
277 loader = resource->load(*io);
282 throw logic_error("no loader created");
287 bool ResourceManager::ManagedResource::process(bool sync)
289 while(state!=LOAD_FINISHED && loader->needs_sync()==sync)
290 if(loader->process())
291 state = LOAD_FINISHED;
293 return state==LOAD_FINISHED;
296 void ResourceManager::ManagedResource::finish_loading(bool successful)
306 data_size = resource->get_data_size();
308 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
309 (*i)->resource_loaded(*resource);
318 void ResourceManager::ManagedResource::finish_loading()
320 finish_loading(state==LOAD_FINISHED);
323 void ResourceManager::ManagedResource::unload()
328 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
329 (*i)->resource_unloaded(*resource);
332 void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
334 if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
335 watchers.push_back(&w);
338 void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
340 vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
341 if(end!=watchers.end())
342 watchers.erase(end, watchers.end());
346 ResourceManager::LoadingThread::LoadingThread():
355 void ResourceManager::LoadingThread::main()
357 bool wait_for_work = false;
363 if(ManagedResource *managed = front(async_queue))
367 managed->process(false);
369 catch(const exception &e)
371 MutexLock lock(queue_mutex);
372 error_queue.push_back(resource_load_error(managed->location.name, e));
373 managed->state = ManagedResource::LOAD_ERROR;
376 MutexLock lock(queue_mutex);
377 sync_queue.splice(sync_queue.end(), async_queue, async_queue.begin());
378 wait_for_work = async_queue.empty();
381 wait_for_work = true;
385 ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
387 MutexLock lock(queue_mutex);
391 return queue.front();
394 void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
398 MutexLock lock(queue_mutex);
399 if(r.loader->needs_sync())
400 sync_queue.push_back(&r);
403 bool was_empty = async_queue.empty();
404 async_queue.push_back(&r);
412 void ResourceManager::LoadingThread::remove_resource(ManagedResource &r)
414 while(!try_remove_resource(r))
415 Time::sleep(Time::msec);
420 bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
422 MutexLock lock(queue_mutex);
424 LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
425 if(i==async_queue.end())
427 i = find(sync_queue.begin(), sync_queue.end(), &r);
428 if(i!=sync_queue.end())
431 else if(i==async_queue.begin())
434 async_queue.erase(i);
439 bool ResourceManager::LoadingThread::sync()
442 MutexLock lock(queue_mutex);
444 if(!error_queue.empty())
446 resource_load_error err = error_queue.front();
447 error_queue.pop_front();
451 unsigned async_size = async_queue.size();
452 if(async_size==0 && size==capacity)
454 else if(async_size>2 && capacity>2)
458 bool any_finished = false;
459 while(ManagedResource *managed = front(sync_queue))
461 if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
463 managed->finish_loading();
467 MutexLock lock(queue_mutex);
468 sync_queue.pop_front();
472 MutexLock lock(queue_mutex);
473 bool was_empty = async_queue.empty();
474 async_queue.splice(async_queue.end(), sync_queue, sync_queue.begin());
483 void ResourceManager::LoadingThread::terminate()