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 string &err):
15 runtime_error(format("%s: %s", name, err))
18 resource_load_error::resource_load_error(const string &name, const exception &exc):
19 runtime_error(format("%s: %s: %s", name, Debug::demangle(typeid(exc).name()), exc.what()))
23 ResourceManager::ResourceManager():
24 policy(LOAD_ON_DEMAND),
28 min_retain_frames(30),
33 ResourceManager::~ResourceManager()
37 while(!resources.empty())
38 resources.begin()->second.resource->set_manager(0);
41 void ResourceManager::set_loading_policy(LoadingPolicy p)
46 void ResourceManager::set_async_loads(bool a)
51 void ResourceManager::set_size_limit(UInt64 s)
56 void ResourceManager::set_min_retain_frames(unsigned f)
58 min_retain_frames = f;
61 void ResourceManager::set_max_retain_frames(unsigned f)
63 max_retain_frames = f;
66 void ResourceManager::add_resource(Resource &r)
68 insert_unique(resources, &r, ManagedResource(r));
71 void *ResourceManager::get_data_for_resource(const Resource &r)
73 return &get_item(resources, &r);
76 void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
78 set_resource_location(r, ResourceLocation(c, n));
81 void ResourceManager::set_resource_location(Resource &r, const ResourceLocation &l)
83 ManagedResource &managed = get_item(resources, &r);
86 if(policy==LOAD_IMMEDIATELY)
90 const ResourceManager::ResourceLocation *ResourceManager::get_resource_location(const Resource &r) const
92 const ManagedResource &managed = get_item(resources, &r);
93 return managed.location.collection ? &managed.location : 0;
96 void ResourceManager::load_resource(Resource &r)
98 ManagedResource &managed = get_item(resources, &r);
99 if(!managed.location.collection)
100 throw runtime_error("no location");
102 if(managed.state!=ManagedResource::NOT_LOADED)
107 managed.state = ManagedResource::LOAD_QUEUED;
108 queue.push_back(&managed);
112 managed.start_loading();
113 while(!managed.loader->process()) ;
114 managed.finish_loading(true);
118 bool ResourceManager::is_resource_loaded(const Resource &r) const
120 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
121 return managed ? managed->state==ManagedResource::LOADED : false;
124 void ResourceManager::resource_used(const Resource &r)
126 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
129 if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
130 load_resource(*managed->resource);
132 managed->last_used = frame;
133 if(max_retain_frames && !next_unload)
134 next_unload = frame+max_retain_frames+1;
137 void ResourceManager::remove_resource(Resource &r)
139 ManagedResource &managed = get_item(resources, &r);
140 ManagedResource::State state = managed.state;
141 if(state==ManagedResource::LOAD_QUEUED)
143 LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
147 else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
148 thread.remove_resource(managed);
149 remove_existing(resources, &r);
152 void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
154 get_item(resources, &r).add_watcher(w);
157 void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
159 get_item(resources, &r).remove_watcher(w);
162 void ResourceManager::tick()
166 bool do_unload = (frame>=next_unload);
170 if(thread.needs_work() && !queue.empty())
175 if(max_retain_frames && frame>=next_unload)
180 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
181 if(i->second.state==ManagedResource::LOADED)
182 next_unload = min(next_unload, i->second.last_used);
183 next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
191 void ResourceManager::dispatch_work()
193 queue.sort(age_order);
195 if(queue.front()->last_used+10<frame)
197 for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
198 (*i)->state = ManagedResource::NOT_LOADED;
203 while(thread.needs_work() && !queue.empty())
205 ManagedResource *managed = queue.front();
207 thread.add_resource(*managed);
211 void ResourceManager::unload_by_age()
213 unsigned unload_limit = frame-max_retain_frames;
215 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
216 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
220 void ResourceManager::unload_by_size()
222 unsigned unload_limit = frame-min_retain_frames;
224 while(get_total_data_size()>size_limit)
226 ManagedResource *best = 0;
227 UInt64 best_impact = 0;
228 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
229 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
231 UInt64 impact = i->second.data_size*(frame-i->second.last_used);
232 if(!best || impact>best_impact)
235 best_impact = impact;
246 UInt64 ResourceManager::get_total_data_size() const
249 for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
250 if(i->second.state==ManagedResource::LOADED)
251 total += i->second.data_size;
255 bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
257 return mr1->last_used>mr2->last_used;
261 ResourceManager::ResourceLocation::ResourceLocation():
265 ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
271 ResourceManager::ManagedResource::ManagedResource(Resource &r):
280 void ResourceManager::ManagedResource::start_loading()
282 io = location.collection->open_raw(location.name);
284 throw resource_load_error(location.name, "open failed");
286 loader = resource->load(*io);
291 throw logic_error("no loader created");
296 bool ResourceManager::ManagedResource::process(bool sync)
298 while(state!=LOAD_FINISHED && loader->needs_sync()==sync)
299 if(loader->process())
300 state = LOAD_FINISHED;
302 return state==LOAD_FINISHED;
305 void ResourceManager::ManagedResource::finish_loading(bool successful)
315 data_size = resource->get_data_size();
317 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
318 (*i)->resource_loaded(*resource);
327 void ResourceManager::ManagedResource::finish_loading()
329 finish_loading(state==LOAD_FINISHED);
332 void ResourceManager::ManagedResource::unload()
337 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
338 (*i)->resource_unloaded(*resource);
341 void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
343 if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
344 watchers.push_back(&w);
347 void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
349 vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
350 if(end!=watchers.end())
351 watchers.erase(end, watchers.end());
355 ResourceManager::LoadingThread::LoadingThread():
364 void ResourceManager::LoadingThread::main()
366 bool wait_for_work = false;
372 if(ManagedResource *managed = front(async_queue))
376 managed->process(false);
378 catch(const exception &e)
380 MutexLock lock(queue_mutex);
381 error_queue.push_back(resource_load_error(managed->location.name, e));
382 managed->state = ManagedResource::LOAD_ERROR;
385 MutexLock lock(queue_mutex);
386 sync_queue.splice(sync_queue.end(), async_queue, async_queue.begin());
387 wait_for_work = async_queue.empty();
390 wait_for_work = true;
394 ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
396 MutexLock lock(queue_mutex);
400 return queue.front();
403 void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
407 MutexLock lock(queue_mutex);
408 if(r.loader->needs_sync())
409 sync_queue.push_back(&r);
412 bool was_empty = async_queue.empty();
413 async_queue.push_back(&r);
421 void ResourceManager::LoadingThread::remove_resource(ManagedResource &r)
423 while(!try_remove_resource(r))
424 Time::sleep(Time::msec);
429 bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
431 MutexLock lock(queue_mutex);
433 LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
434 if(i==async_queue.end())
436 i = find(sync_queue.begin(), sync_queue.end(), &r);
437 if(i!=sync_queue.end())
440 else if(i==async_queue.begin())
443 async_queue.erase(i);
448 bool ResourceManager::LoadingThread::sync()
451 MutexLock lock(queue_mutex);
453 if(!error_queue.empty())
455 resource_load_error err = error_queue.front();
456 error_queue.pop_front();
460 unsigned async_size = async_queue.size();
461 if(async_size==0 && size==capacity)
463 else if(async_size>2 && capacity>2)
467 bool any_finished = false;
468 while(ManagedResource *managed = front(sync_queue))
470 if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
472 managed->finish_loading();
476 MutexLock lock(queue_mutex);
477 sync_queue.pop_front();
481 MutexLock lock(queue_mutex);
482 bool was_empty = async_queue.empty();
483 async_queue.splice(async_queue.end(), sync_queue, sync_queue.begin());
492 void ResourceManager::LoadingThread::terminate()