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());
125 if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
126 load_resource(*managed->resource);
128 managed->last_used = frame;
129 if(max_retain_frames && !next_unload)
130 next_unload = frame+max_retain_frames+1;
133 void ResourceManager::remove_resource(Resource &r)
135 ManagedResource &managed = get_item(resources, &r);
136 ManagedResource::State state = managed.state;
137 if(state==ManagedResource::LOAD_QUEUED)
139 LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
143 else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
144 thread.remove_resource(managed);
145 remove_existing(resources, &r);
148 void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
150 get_item(resources, &r).add_watcher(w);
153 void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
155 get_item(resources, &r).remove_watcher(w);
158 void ResourceManager::tick()
162 bool do_unload = (frame>=next_unload);
166 if(thread.needs_work() && !queue.empty())
171 if(max_retain_frames && frame>=next_unload)
176 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
177 if(i->second.state==ManagedResource::LOADED)
178 next_unload = min(next_unload, i->second.last_used);
179 next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
187 void ResourceManager::dispatch_work()
189 queue.sort(age_order);
191 if(queue.front()->last_used+10<frame)
193 for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
194 (*i)->state = ManagedResource::NOT_LOADED;
199 while(thread.needs_work() && !queue.empty())
201 ManagedResource *managed = queue.front();
203 thread.add_resource(*managed);
207 void ResourceManager::unload_by_age()
209 unsigned unload_limit = frame-max_retain_frames;
211 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
212 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
216 void ResourceManager::unload_by_size()
218 unsigned unload_limit = frame-min_retain_frames;
220 while(get_total_data_size()>size_limit)
222 ManagedResource *best = 0;
223 UInt64 best_impact = 0;
224 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
225 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
227 UInt64 impact = i->second.data_size*(frame-i->second.last_used);
228 if(!best || impact>best_impact)
231 best_impact = impact;
242 UInt64 ResourceManager::get_total_data_size() const
245 for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
246 if(i->second.state==ManagedResource::LOADED)
247 total += i->second.data_size;
251 bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
253 return mr1->last_used>mr2->last_used;
257 ResourceManager::ResourceLocation::ResourceLocation():
261 ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
267 ResourceManager::ManagedResource::ManagedResource(Resource &r):
276 void ResourceManager::ManagedResource::start_loading()
278 io = location.collection->open_raw(location.name);
279 loader = resource->load(*io);
284 throw logic_error("no loader created");
289 bool ResourceManager::ManagedResource::process(bool sync)
291 while(state!=LOAD_FINISHED && loader->needs_sync()==sync)
292 if(loader->process())
293 state = LOAD_FINISHED;
295 return state==LOAD_FINISHED;
298 void ResourceManager::ManagedResource::finish_loading(bool successful)
308 data_size = resource->get_data_size();
310 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
311 (*i)->resource_loaded(*resource);
320 void ResourceManager::ManagedResource::finish_loading()
322 finish_loading(state==LOAD_FINISHED);
325 void ResourceManager::ManagedResource::unload()
330 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
331 (*i)->resource_unloaded(*resource);
334 void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
336 if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
337 watchers.push_back(&w);
340 void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
342 vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
343 if(end!=watchers.end())
344 watchers.erase(end, watchers.end());
348 ResourceManager::LoadingThread::LoadingThread():
357 void ResourceManager::LoadingThread::main()
359 bool wait_for_work = false;
365 if(ManagedResource *managed = front(async_queue))
369 managed->process(false);
371 catch(const exception &e)
373 MutexLock lock(queue_mutex);
374 error_queue.push_back(resource_load_error(managed->location.name, e));
375 managed->state = ManagedResource::LOAD_ERROR;
378 MutexLock lock(queue_mutex);
379 sync_queue.splice(sync_queue.end(), async_queue, async_queue.begin());
380 wait_for_work = async_queue.empty();
383 wait_for_work = true;
387 ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
389 MutexLock lock(queue_mutex);
393 return queue.front();
396 void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
400 MutexLock lock(queue_mutex);
401 if(r.loader->needs_sync())
402 sync_queue.push_back(&r);
405 bool was_empty = async_queue.empty();
406 async_queue.push_back(&r);
414 void ResourceManager::LoadingThread::remove_resource(ManagedResource &r)
416 while(!try_remove_resource(r))
417 Time::sleep(Time::msec);
422 bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
424 MutexLock lock(queue_mutex);
426 LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
427 if(i==async_queue.end())
429 i = find(sync_queue.begin(), sync_queue.end(), &r);
430 if(i!=sync_queue.end())
433 else if(i==async_queue.begin())
436 async_queue.erase(i);
441 bool ResourceManager::LoadingThread::sync()
444 MutexLock lock(queue_mutex);
446 if(!error_queue.empty())
448 resource_load_error err = error_queue.front();
449 error_queue.pop_front();
453 unsigned async_size = async_queue.size();
454 if(async_size==0 && size==capacity)
456 else if(async_size>2 && capacity>2)
460 bool any_finished = false;
461 while(ManagedResource *managed = front(sync_queue))
463 if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
465 managed->finish_loading();
469 MutexLock lock(queue_mutex);
470 sync_queue.pop_front();
474 MutexLock lock(queue_mutex);
475 bool was_empty = async_queue.empty();
476 async_queue.splice(async_queue.end(), sync_queue, sync_queue.begin());
485 void ResourceManager::LoadingThread::terminate()