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 MutexLock lock(map_mutex);
70 insert_unique(resources, &r, ManagedResource(r));
73 const ResourceManager::ManagedResource &ResourceManager::get_managed_resource(const Resource &r) const
75 MutexLock lock(map_mutex);
76 return get_item(resources, &r);
79 ResourceManager::ManagedResource &ResourceManager::get_managed_resource(const Resource &r)
81 MutexLock lock(map_mutex);
82 return get_item(resources, &r);
85 void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
87 set_resource_location(r, ResourceLocation(c, n));
90 void ResourceManager::set_resource_location(Resource &r, const ResourceLocation &l)
93 MutexLock lock(map_mutex);
94 ManagedResource &managed = get_item(resources, &r);
98 if(policy==LOAD_IMMEDIATELY)
102 const ResourceManager::ResourceLocation *ResourceManager::get_resource_location(const Resource &r) const
104 const ManagedResource &managed = get_managed_resource(r);
105 return managed.location.collection ? &managed.location : 0;
108 void ResourceManager::load_resource(Resource &r)
110 ManagedResource &managed = get_managed_resource(r);
111 if(!managed.location.collection)
112 throw runtime_error("no location");
114 if(managed.state!=ManagedResource::NOT_LOADED)
119 managed.state = ManagedResource::LOAD_QUEUED;
120 LoadQueue::iterator i;
121 for(i=queue.begin(); (i!=queue.end() && (*i)->load_priority>=managed.load_priority); ++i) ;
122 queue.insert(i, &managed);
126 managed.start_loading();
127 while(!managed.loader->process()) ;
128 managed.finish_loading(true);
132 bool ResourceManager::is_resource_loaded(const Resource &r) const
134 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
135 return managed ? managed->state==ManagedResource::LOADED : false;
138 void ResourceManager::resource_used(const Resource &r)
140 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
143 if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
144 load_resource(*managed->resource);
146 managed->last_used = frame;
147 if(max_retain_frames && !next_unload)
148 next_unload = frame+max_retain_frames+1;
151 void ResourceManager::remove_resource(Resource &r)
153 ManagedResource &managed = get_managed_resource(r);
154 ManagedResource::State state = managed.state;
155 if(state==ManagedResource::LOAD_QUEUED)
157 LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
161 else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
162 thread.remove_resource(managed);
164 MutexLock lock(map_mutex);
165 remove_existing(resources, &r);
168 void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
170 get_managed_resource(r).add_watcher(w);
173 void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
175 get_managed_resource(r).remove_watcher(w);
178 void ResourceManager::tick()
182 bool do_unload = (frame>=next_unload);
186 if(thread.needs_work() && !queue.empty())
191 MutexLock lock(map_mutex);
192 if(max_retain_frames && frame>=next_unload)
197 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
198 if(i->second.state==ManagedResource::LOADED)
199 next_unload = min(next_unload, i->second.last_used);
200 next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
208 void ResourceManager::dispatch_work()
210 queue.sort(age_order);
212 if(queue.front()->last_used+10<frame)
214 for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
215 (*i)->state = ManagedResource::NOT_LOADED;
220 while(thread.needs_work() && !queue.empty())
222 ManagedResource *managed = queue.front();
224 thread.add_resource(*managed);
228 void ResourceManager::unload_by_age()
230 unsigned unload_limit = frame-max_retain_frames;
232 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
233 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
237 void ResourceManager::unload_by_size()
239 unsigned unload_limit = frame-min_retain_frames;
241 while(get_total_data_size_()>size_limit)
243 ManagedResource *best = 0;
244 UInt64 best_impact = 0;
245 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
246 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
248 UInt64 impact = i->second.data_size*(frame-i->second.last_used);
249 if(!best || impact>best_impact)
252 best_impact = impact;
263 UInt64 ResourceManager::get_total_data_size_() const
266 for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
267 if(i->second.state==ManagedResource::LOADED)
268 total += i->second.data_size;
272 UInt64 ResourceManager::get_total_data_size() const
274 MutexLock lock(map_mutex);
275 return get_total_data_size_();
278 bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
280 return mr1->last_used>mr2->last_used;
284 ResourceManager::ResourceLocation::ResourceLocation():
288 ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
294 ResourceManager::ManagedResource::ManagedResource(Resource &r):
296 load_priority(r.get_load_priority()),
304 void ResourceManager::ManagedResource::start_loading()
306 io = location.collection->open_raw(location.name);
308 throw resource_load_error(location.name, "open failed");
310 const Resources *res = dynamic_cast<Resources *>(location.collection);
311 loader = resource->load(*io, res);
316 throw logic_error("no loader created");
321 bool ResourceManager::ManagedResource::process(bool sync)
323 while(state!=LOAD_FINISHED && loader->needs_sync()==sync)
324 if(loader->process())
325 state = LOAD_FINISHED;
327 return state==LOAD_FINISHED;
330 void ResourceManager::ManagedResource::finish_loading(bool successful)
340 data_size = resource->get_data_size();
342 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
343 (*i)->resource_loaded(*resource);
352 void ResourceManager::ManagedResource::finish_loading()
354 finish_loading(state==LOAD_FINISHED);
357 void ResourceManager::ManagedResource::unload()
362 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
363 (*i)->resource_unloaded(*resource);
366 void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
368 if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
369 watchers.push_back(&w);
372 void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
374 vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
375 if(end!=watchers.end())
376 watchers.erase(end, watchers.end());
380 ResourceManager::LoadingThread::LoadingThread():
389 void ResourceManager::LoadingThread::main()
391 bool wait_for_work = false;
397 if(ManagedResource *managed = front(async_queue))
401 managed->process(false);
403 catch(const exception &e)
405 MutexLock lock(queue_mutex);
406 error_queue.push_back(resource_load_error(managed->location.name, e));
407 managed->state = ManagedResource::LOAD_ERROR;
410 MutexLock lock(queue_mutex);
411 sync_queue.splice(sync_queue.end(), async_queue, async_queue.begin());
412 wait_for_work = async_queue.empty();
415 wait_for_work = true;
419 ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
421 MutexLock lock(queue_mutex);
425 return queue.front();
428 void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
432 MutexLock lock(queue_mutex);
433 if(r.loader->needs_sync())
434 sync_queue.push_back(&r);
437 bool was_empty = async_queue.empty();
438 async_queue.push_back(&r);
446 void ResourceManager::LoadingThread::remove_resource(ManagedResource &r)
448 while(!try_remove_resource(r))
449 Time::sleep(Time::msec);
454 bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
456 MutexLock lock(queue_mutex);
458 LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
459 if(i==async_queue.end())
461 i = find(sync_queue.begin(), sync_queue.end(), &r);
462 if(i!=sync_queue.end())
468 else if(i==async_queue.begin())
472 async_queue.erase(i);
479 bool ResourceManager::LoadingThread::sync()
482 MutexLock lock(queue_mutex);
484 if(!error_queue.empty())
486 resource_load_error err = error_queue.front();
487 error_queue.pop_front();
491 unsigned async_size = async_queue.size();
492 if(async_size==0 && size==capacity)
494 else if(async_size>2 && capacity>2)
498 bool any_finished = false;
499 while(ManagedResource *managed = front(sync_queue))
501 if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
503 managed->finish_loading();
507 MutexLock lock(queue_mutex);
508 sync_queue.pop_front();
512 MutexLock lock(queue_mutex);
513 bool was_empty = async_queue.empty();
514 async_queue.splice(async_queue.end(), sync_queue, sync_queue.begin());
523 void ResourceManager::LoadingThread::terminate()