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 ManagedResource &managed = get_item(resources, &r);
75 managed.collection = &c;
78 if(policy==LOAD_IMMEDIATELY)
82 void ResourceManager::load_resource(Resource &r)
84 ManagedResource &managed = get_item(resources, &r);
85 if(!managed.collection)
86 throw runtime_error("no location");
88 if(managed.state!=ManagedResource::NOT_LOADED)
93 managed.state = ManagedResource::LOAD_QUEUED;
94 queue.push_back(&managed);
98 managed.start_loading();
99 while(!managed.loader->process()) ;
100 managed.finish_loading(true);
104 void ResourceManager::resource_used(const Resource &r)
106 ManagedResource *managed = reinterpret_cast<ManagedResource *>(r.get_manager_data());
107 if(managed->state==ManagedResource::NOT_LOADED && policy!=LOAD_MANUALLY)
108 load_resource(*managed->resource);
110 managed->last_used = frame;
111 if(max_retain_frames && !next_unload)
112 next_unload = frame+max_retain_frames+1;
115 void ResourceManager::remove_resource(Resource &r)
117 ManagedResource &managed = get_item(resources, &r);
118 ManagedResource::State state = managed.state;
119 if(state==ManagedResource::LOAD_QUEUED)
121 LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
125 else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
126 thread.remove_resource(managed);
127 remove_existing(resources, &r);
130 void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
132 get_item(resources, &r).add_watcher(w);
135 void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
137 get_item(resources, &r).remove_watcher(w);
140 void ResourceManager::tick()
144 bool do_unload = (frame>=next_unload);
148 if(thread.needs_work() && !queue.empty())
153 if(max_retain_frames && frame>=next_unload)
158 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
159 if(i->second.state==ManagedResource::LOADED)
160 next_unload = min(next_unload, i->second.last_used);
161 next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
169 void ResourceManager::dispatch_work()
171 queue.sort(age_order);
173 if(queue.front()->last_used+10<frame)
175 for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
176 (*i)->state = ManagedResource::NOT_LOADED;
181 while(thread.needs_work() && !queue.empty())
183 ManagedResource *managed = queue.front();
185 thread.add_resource(*managed);
189 void ResourceManager::unload_by_age()
191 unsigned unload_limit = frame-max_retain_frames;
193 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
194 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
198 void ResourceManager::unload_by_size()
200 unsigned unload_limit = frame-min_retain_frames;
202 while(get_total_data_size()>size_limit)
204 ManagedResource *best = 0;
205 UInt64 best_impact = 0;
206 for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
207 if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
209 UInt64 impact = i->second.data_size*(frame-i->second.last_used);
210 if(!best || impact>best_impact)
213 best_impact = impact;
224 UInt64 ResourceManager::get_total_data_size() const
227 for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
228 if(i->second.state==ManagedResource::LOADED)
229 total += i->second.data_size;
233 bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
235 return mr1->last_used>mr2->last_used;
239 ResourceManager::ManagedResource::ManagedResource(Resource &r):
249 void ResourceManager::ManagedResource::start_loading()
251 io = collection->open_raw(name);
252 loader = resource->load(*io);
257 throw logic_error("no loader created");
262 bool ResourceManager::ManagedResource::process(bool sync)
264 while(state!=LOAD_FINISHED && loader->needs_sync()==sync)
265 if(loader->process())
266 state = LOAD_FINISHED;
268 return state==LOAD_FINISHED;
271 void ResourceManager::ManagedResource::finish_loading(bool successful)
281 data_size = resource->get_data_size();
283 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
284 (*i)->resource_loaded(*resource);
293 void ResourceManager::ManagedResource::finish_loading()
295 finish_loading(state==LOAD_FINISHED);
298 void ResourceManager::ManagedResource::unload()
303 for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
304 (*i)->resource_unloaded(*resource);
307 void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
309 if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
310 watchers.push_back(&w);
313 void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
315 vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
316 if(end!=watchers.end())
317 watchers.erase(end, watchers.end());
321 ResourceManager::LoadingThread::LoadingThread():
330 void ResourceManager::LoadingThread::main()
332 bool wait_for_work = false;
338 if(ManagedResource *managed = front(async_queue))
342 managed->process(false);
344 catch(const exception &e)
346 MutexLock lock(queue_mutex);
347 error_queue.push_back(resource_load_error(managed->name, e));
348 managed->state = ManagedResource::LOAD_ERROR;
351 MutexLock lock(queue_mutex);
352 sync_queue.splice(sync_queue.end(), async_queue, async_queue.begin());
353 wait_for_work = async_queue.empty();
356 wait_for_work = true;
360 ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
362 MutexLock lock(queue_mutex);
366 return queue.front();
369 void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
373 MutexLock lock(queue_mutex);
374 if(r.loader->needs_sync())
375 sync_queue.push_back(&r);
378 bool was_empty = async_queue.empty();
379 async_queue.push_back(&r);
387 void ResourceManager::LoadingThread::remove_resource(ManagedResource &r)
389 while(!try_remove_resource(r))
390 Time::sleep(Time::msec);
395 bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
397 MutexLock lock(queue_mutex);
399 LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
400 if(i==async_queue.end())
402 i = find(sync_queue.begin(), sync_queue.end(), &r);
403 if(i!=sync_queue.end())
406 else if(i==async_queue.begin())
409 async_queue.erase(i);
414 bool ResourceManager::LoadingThread::sync()
417 MutexLock lock(queue_mutex);
419 if(!error_queue.empty())
421 resource_load_error err = error_queue.front();
422 error_queue.pop_front();
426 unsigned async_size = async_queue.size();
427 if(async_size==0 && size==capacity)
429 else if(async_size>2 && capacity>2)
433 bool any_finished = false;
434 while(ManagedResource *managed = front(sync_queue))
436 if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
438 managed->finish_loading();
442 MutexLock lock(queue_mutex);
443 sync_queue.pop_front();
447 MutexLock lock(queue_mutex);
448 bool was_empty = async_queue.empty();
449 async_queue.splice(async_queue.end(), sync_queue, sync_queue.begin());
458 void ResourceManager::LoadingThread::terminate()