3 #include <msp/debug/demangle.h>
4 #include <msp/strings/format.h>
5 #include <msp/time/utils.h>
6 #include "resourcemanager.h"
8 #include "resourcewatcher.h"
15 resource_load_error::resource_load_error(const string &name, const string &err):
16 runtime_error(format("%s: %s", name, err))
19 resource_load_error::resource_load_error(const string &name, const exception &exc):
20 runtime_error(format("%s: %s: %s", name, Debug::demangle(typeid(exc).name()), exc.what()))
24 ResourceManager::ResourceManager():
25 policy(LOAD_ON_DEMAND),
29 min_retain_frames(30),
34 ResourceManager::~ResourceManager()
38 while(!resources.empty())
39 resources.begin()->second.resource->set_manager(0);
42 void ResourceManager::set_loading_policy(LoadingPolicy p)
47 void ResourceManager::set_async_loads(bool a)
52 void ResourceManager::set_size_limit(UInt64 s)
57 void ResourceManager::set_min_retain_frames(unsigned f)
59 min_retain_frames = f;
62 void ResourceManager::set_max_retain_frames(unsigned f)
64 max_retain_frames = f;
67 void ResourceManager::add_resource(Resource &r)
69 insert_unique(resources, &r, ManagedResource(r));
72 void *ResourceManager::get_data_for_resource(const Resource &r)
74 return &get_item(resources, &r);
77 void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
79 set_resource_location(r, ResourceLocation(c, n));
82 void ResourceManager::set_resource_location(Resource &r, const ResourceLocation &l)
84 ManagedResource &managed = get_item(resources, &r);
87 if(policy==LOAD_IMMEDIATELY)
91 const ResourceManager::ResourceLocation *ResourceManager::get_resource_location(const Resource &r) const
93 const ManagedResource &managed = get_item(resources, &r);
94 return managed.location.collection ? &managed.location : 0;
97 void ResourceManager::load_resource(Resource &r)
99 ManagedResource &managed = get_item(resources, &r);
100 if(!managed.location.collection)
101 throw runtime_error("no location");
103 if(managed.state!=ManagedResource::NOT_LOADED)
108 managed.state = ManagedResource::LOAD_QUEUED;
109 LoadQueue::iterator i;
110 for(i=queue.begin(); (i!=queue.end() && (*i)->load_priority>=managed.load_priority); ++i) ;
111 queue.insert(i, &managed);
115 managed.start_loading();
116 while(!managed.loader->process()) ;
117 managed.finish_loading(true);
121 bool ResourceManager::is_resource_loaded(const Resource &r) const
123 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
124 return managed ? managed->state==ManagedResource::LOADED : false;
127 void ResourceManager::resource_used(const Resource &r)
129 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
132 if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
133 load_resource(*managed->resource);
135 managed->last_used = frame;
136 if(max_retain_frames && !next_unload)
137 next_unload = frame+max_retain_frames+1;
140 void ResourceManager::remove_resource(Resource &r)
142 ManagedResource &managed = get_item(resources, &r);
143 ManagedResource::State state = managed.state;
144 if(state==ManagedResource::LOAD_QUEUED)
146 LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
150 else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
151 thread.remove_resource(managed);
152 remove_existing(resources, &r);
155 void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
157 get_item(resources, &r).add_watcher(w);
160 void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
162 get_item(resources, &r).remove_watcher(w);
165 void ResourceManager::tick()
169 bool do_unload = (frame>=next_unload);
173 if(thread.needs_work() && !queue.empty())
178 if(max_retain_frames && frame>=next_unload)
183 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
184 if(i->second.state==ManagedResource::LOADED)
185 next_unload = min(next_unload, i->second.last_used);
186 next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
194 void ResourceManager::dispatch_work()
196 queue.sort(age_order);
198 if(queue.front()->last_used+10<frame)
200 for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
201 (*i)->state = ManagedResource::NOT_LOADED;
206 while(thread.needs_work() && !queue.empty())
208 ManagedResource *managed = queue.front();
210 thread.add_resource(*managed);
214 void ResourceManager::unload_by_age()
216 unsigned unload_limit = frame-max_retain_frames;
218 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
219 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
223 void ResourceManager::unload_by_size()
225 unsigned unload_limit = frame-min_retain_frames;
227 while(get_total_data_size()>size_limit)
229 ManagedResource *best = 0;
230 UInt64 best_impact = 0;
231 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
232 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
234 UInt64 impact = i->second.data_size*(frame-i->second.last_used);
235 if(!best || impact>best_impact)
238 best_impact = impact;
249 UInt64 ResourceManager::get_total_data_size() const
252 for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
253 if(i->second.state==ManagedResource::LOADED)
254 total += i->second.data_size;
258 bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
260 return mr1->last_used>mr2->last_used;
264 ResourceManager::ResourceLocation::ResourceLocation():
268 ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
274 ResourceManager::ManagedResource::ManagedResource(Resource &r):
276 load_priority(r.get_load_priority()),
284 void ResourceManager::ManagedResource::start_loading()
286 io = location.collection->open_raw(location.name);
288 throw resource_load_error(location.name, "open failed");
290 const Resources *res = dynamic_cast<Resources *>(location.collection);
291 loader = resource->load(*io, res);
296 throw logic_error("no loader created");
301 bool ResourceManager::ManagedResource::process(bool sync)
303 while(state!=LOAD_FINISHED && loader->needs_sync()==sync)
304 if(loader->process())
305 state = LOAD_FINISHED;
307 return state==LOAD_FINISHED;
310 void ResourceManager::ManagedResource::finish_loading(bool successful)
320 data_size = resource->get_data_size();
322 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
323 (*i)->resource_loaded(*resource);
332 void ResourceManager::ManagedResource::finish_loading()
334 finish_loading(state==LOAD_FINISHED);
337 void ResourceManager::ManagedResource::unload()
342 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
343 (*i)->resource_unloaded(*resource);
346 void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
348 if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
349 watchers.push_back(&w);
352 void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
354 vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
355 if(end!=watchers.end())
356 watchers.erase(end, watchers.end());
360 ResourceManager::LoadingThread::LoadingThread():
369 void ResourceManager::LoadingThread::main()
371 bool wait_for_work = false;
377 if(ManagedResource *managed = front(async_queue))
381 managed->process(false);
383 catch(const exception &e)
385 MutexLock lock(queue_mutex);
386 error_queue.push_back(resource_load_error(managed->location.name, e));
387 managed->state = ManagedResource::LOAD_ERROR;
390 MutexLock lock(queue_mutex);
391 sync_queue.splice(sync_queue.end(), async_queue, async_queue.begin());
392 wait_for_work = async_queue.empty();
395 wait_for_work = true;
399 ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
401 MutexLock lock(queue_mutex);
405 return queue.front();
408 void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
412 MutexLock lock(queue_mutex);
413 if(r.loader->needs_sync())
414 sync_queue.push_back(&r);
417 bool was_empty = async_queue.empty();
418 async_queue.push_back(&r);
426 void ResourceManager::LoadingThread::remove_resource(ManagedResource &r)
428 while(!try_remove_resource(r))
429 Time::sleep(Time::msec);
434 bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
436 MutexLock lock(queue_mutex);
438 LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
439 if(i==async_queue.end())
441 i = find(sync_queue.begin(), sync_queue.end(), &r);
442 if(i!=sync_queue.end())
445 else if(i==async_queue.begin())
448 async_queue.erase(i);
453 bool ResourceManager::LoadingThread::sync()
456 MutexLock lock(queue_mutex);
458 if(!error_queue.empty())
460 resource_load_error err = error_queue.front();
461 error_queue.pop_front();
465 unsigned async_size = async_queue.size();
466 if(async_size==0 && size==capacity)
468 else if(async_size>2 && capacity>2)
472 bool any_finished = false;
473 while(ManagedResource *managed = front(sync_queue))
475 if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
477 managed->finish_loading();
481 MutexLock lock(queue_mutex);
482 sync_queue.pop_front();
486 MutexLock lock(queue_mutex);
487 bool was_empty = async_queue.empty();
488 async_queue.splice(async_queue.end(), sync_queue, sync_queue.begin());
497 void ResourceManager::LoadingThread::terminate()