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 queue.push_back(&managed);
113 managed.start_loading();
114 while(!managed.loader->process()) ;
115 managed.finish_loading(true);
119 bool ResourceManager::is_resource_loaded(const Resource &r) const
121 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
122 return managed ? managed->state==ManagedResource::LOADED : false;
125 void ResourceManager::resource_used(const Resource &r)
127 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
130 if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
131 load_resource(*managed->resource);
133 managed->last_used = frame;
134 if(max_retain_frames && !next_unload)
135 next_unload = frame+max_retain_frames+1;
138 void ResourceManager::remove_resource(Resource &r)
140 ManagedResource &managed = get_item(resources, &r);
141 ManagedResource::State state = managed.state;
142 if(state==ManagedResource::LOAD_QUEUED)
144 LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
148 else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
149 thread.remove_resource(managed);
150 remove_existing(resources, &r);
153 void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
155 get_item(resources, &r).add_watcher(w);
158 void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
160 get_item(resources, &r).remove_watcher(w);
163 void ResourceManager::tick()
167 bool do_unload = (frame>=next_unload);
171 if(thread.needs_work() && !queue.empty())
176 if(max_retain_frames && frame>=next_unload)
181 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
182 if(i->second.state==ManagedResource::LOADED)
183 next_unload = min(next_unload, i->second.last_used);
184 next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
192 void ResourceManager::dispatch_work()
194 queue.sort(age_order);
196 if(queue.front()->last_used+10<frame)
198 for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
199 (*i)->state = ManagedResource::NOT_LOADED;
204 while(thread.needs_work() && !queue.empty())
206 ManagedResource *managed = queue.front();
208 thread.add_resource(*managed);
212 void ResourceManager::unload_by_age()
214 unsigned unload_limit = frame-max_retain_frames;
216 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
217 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
221 void ResourceManager::unload_by_size()
223 unsigned unload_limit = frame-min_retain_frames;
225 while(get_total_data_size()>size_limit)
227 ManagedResource *best = 0;
228 UInt64 best_impact = 0;
229 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
230 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
232 UInt64 impact = i->second.data_size*(frame-i->second.last_used);
233 if(!best || impact>best_impact)
236 best_impact = impact;
247 UInt64 ResourceManager::get_total_data_size() const
250 for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
251 if(i->second.state==ManagedResource::LOADED)
252 total += i->second.data_size;
256 bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
258 return mr1->last_used>mr2->last_used;
262 ResourceManager::ResourceLocation::ResourceLocation():
266 ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
272 ResourceManager::ManagedResource::ManagedResource(Resource &r):
281 void ResourceManager::ManagedResource::start_loading()
283 io = location.collection->open_raw(location.name);
285 throw resource_load_error(location.name, "open failed");
287 const Resources *res = dynamic_cast<Resources *>(location.collection);
288 loader = resource->load(*io, res);
293 throw logic_error("no loader created");
298 bool ResourceManager::ManagedResource::process(bool sync)
300 while(state!=LOAD_FINISHED && loader->needs_sync()==sync)
301 if(loader->process())
302 state = LOAD_FINISHED;
304 return state==LOAD_FINISHED;
307 void ResourceManager::ManagedResource::finish_loading(bool successful)
317 data_size = resource->get_data_size();
319 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
320 (*i)->resource_loaded(*resource);
329 void ResourceManager::ManagedResource::finish_loading()
331 finish_loading(state==LOAD_FINISHED);
334 void ResourceManager::ManagedResource::unload()
339 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
340 (*i)->resource_unloaded(*resource);
343 void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
345 if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
346 watchers.push_back(&w);
349 void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
351 vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
352 if(end!=watchers.end())
353 watchers.erase(end, watchers.end());
357 ResourceManager::LoadingThread::LoadingThread():
366 void ResourceManager::LoadingThread::main()
368 bool wait_for_work = false;
374 if(ManagedResource *managed = front(async_queue))
378 managed->process(false);
380 catch(const exception &e)
382 MutexLock lock(queue_mutex);
383 error_queue.push_back(resource_load_error(managed->location.name, e));
384 managed->state = ManagedResource::LOAD_ERROR;
387 MutexLock lock(queue_mutex);
388 sync_queue.splice(sync_queue.end(), async_queue, async_queue.begin());
389 wait_for_work = async_queue.empty();
392 wait_for_work = true;
396 ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
398 MutexLock lock(queue_mutex);
402 return queue.front();
405 void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
409 MutexLock lock(queue_mutex);
410 if(r.loader->needs_sync())
411 sync_queue.push_back(&r);
414 bool was_empty = async_queue.empty();
415 async_queue.push_back(&r);
423 void ResourceManager::LoadingThread::remove_resource(ManagedResource &r)
425 while(!try_remove_resource(r))
426 Time::sleep(Time::msec);
431 bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
433 MutexLock lock(queue_mutex);
435 LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
436 if(i==async_queue.end())
438 i = find(sync_queue.begin(), sync_queue.end(), &r);
439 if(i!=sync_queue.end())
442 else if(i==async_queue.begin())
445 async_queue.erase(i);
450 bool ResourceManager::LoadingThread::sync()
453 MutexLock lock(queue_mutex);
455 if(!error_queue.empty())
457 resource_load_error err = error_queue.front();
458 error_queue.pop_front();
462 unsigned async_size = async_queue.size();
463 if(async_size==0 && size==capacity)
465 else if(async_size>2 && capacity>2)
469 bool any_finished = false;
470 while(ManagedResource *managed = front(sync_queue))
472 if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
474 managed->finish_loading();
478 MutexLock lock(queue_mutex);
479 sync_queue.pop_front();
483 MutexLock lock(queue_mutex);
484 bool was_empty = async_queue.empty();
485 async_queue.splice(async_queue.end(), sync_queue, sync_queue.begin());
494 void ResourceManager::LoadingThread::terminate()