#include <msp/time/utils.h>
#include "resourcemanager.h"
#include "resources.h"
-#include "resourcewatcher.h"
+#include "resourceobserver.h"
using namespace std;
ResourceManager::ResourceManager():
policy(LOAD_ON_DEMAND),
async_loads(true),
+ total_data_size(0),
size_limit(0),
frame(0),
min_retain_frames(30),
managed.start_loading();
while(!managed.loader->process()) ;
managed.finish_loading(true);
+ total_data_size += managed.data_size;
}
}
else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
thread.remove_resource(managed);
+ for(vector<ResourceObserver *>::const_iterator i=managed.observers.begin(); i!=managed.observers.end(); ++i)
+ (*i)->resource_removed(r);
+
MutexLock lock(map_mutex);
remove_existing(resources, &r);
}
-void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
+void ResourceManager::observe_resource(const Resource &r, ResourceObserver &w)
{
- get_managed_resource(r).add_watcher(w);
+ get_managed_resource(r).add_observer(w);
}
-void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
+void ResourceManager::unobserve_resource(const Resource &r, ResourceObserver &w)
{
- get_managed_resource(r).remove_watcher(w);
+ get_managed_resource(r).remove_observer(w);
}
void ResourceManager::tick()
bool do_unload = (frame>=next_unload);
if(thread.sync())
+ {
+ total_data_size += thread.get_and_reset_loaded_data_size();
do_unload = true;
+ }
if(thread.needs_work() && !queue.empty())
dispatch_work();
for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
+ {
i->second.unload();
+ total_data_size -= i->second.data_size;
+ }
}
void ResourceManager::unload_by_size()
{
unsigned unload_limit = frame-min_retain_frames;
- while(get_total_data_size_()>size_limit)
+ while(total_data_size>size_limit)
{
ManagedResource *best = 0;
UInt64 best_impact = 0;
break;
best->unload();
+ total_data_size -= best->data_size;
}
}
-UInt64 ResourceManager::get_total_data_size_() const
-{
- UInt64 total = 0;
- for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
- if(i->second.state==ManagedResource::LOADED)
- total += i->second.data_size;
- return total;
-}
-
-UInt64 ResourceManager::get_total_data_size() const
-{
- MutexLock lock(map_mutex);
- return get_total_data_size_();
-}
-
bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
{
return mr1->last_used>mr2->last_used;
state = LOADED;
data_size = resource->get_data_size();
- for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
+ for(vector<ResourceObserver *>::const_iterator i=observers.begin(); i!=observers.end(); ++i)
(*i)->resource_loaded(*resource);
}
else
resource->unload();
state = NOT_LOADED;
- for(vector<ResourceWatcher *>::const_iterator i=watchers.begin(); i!=watchers.end(); ++i)
+ for(vector<ResourceObserver *>::const_iterator i=observers.begin(); i!=observers.end(); ++i)
(*i)->resource_unloaded(*resource);
}
-void ResourceManager::ManagedResource::add_watcher(ResourceWatcher &w)
+void ResourceManager::ManagedResource::add_observer(ResourceObserver &w)
{
- if(find(watchers.begin(), watchers.end(), &w)==watchers.end())
- watchers.push_back(&w);
+ if(find(observers.begin(), observers.end(), &w)==observers.end())
+ observers.push_back(&w);
}
-void ResourceManager::ManagedResource::remove_watcher(ResourceWatcher &w)
+void ResourceManager::ManagedResource::remove_observer(ResourceObserver &w)
{
- vector<ResourceWatcher *>::iterator end = remove(watchers.begin(), watchers.end(), &w);
- if(end!=watchers.end())
- watchers.erase(end, watchers.end());
+ vector<ResourceObserver *>::iterator end = remove(observers.begin(), observers.end(), &w);
+ if(end!=observers.end())
+ observers.erase(end, observers.end());
}
sem(1),
capacity(2),
size(0),
+ loaded_data_size(0),
done(false)
{
launch();
}
}
-ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &queue)
+ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &que)
{
MutexLock lock(queue_mutex);
- if(queue.empty())
+ if(que.empty())
return 0;
- return queue.front();
+ return que.front();
}
void ResourceManager::LoadingThread::add_resource(ManagedResource &r)
Time::sleep(Time::msec);
r.finish_loading();
+ if(r.state==ManagedResource::LOADED)
+ {
+ MutexLock lock(data_size_mutex);
+ loaded_data_size += r.data_size;
+ }
}
bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r)
if(managed->state==ManagedResource::LOAD_ERROR || managed->process(true))
{
managed->finish_loading();
+ if(managed->state==ManagedResource::LOADED)
+ {
+ MutexLock lock(data_size_mutex);
+ loaded_data_size += managed->data_size;
+ }
any_finished = true;
--size;
return any_finished;
}
+UInt64 ResourceManager::LoadingThread::get_and_reset_loaded_data_size()
+{
+ MutexLock lock(data_size_mutex);
+ UInt64 result = loaded_data_size;
+ loaded_data_size = 0;
+ return result;
+}
+
void ResourceManager::LoadingThread::terminate()
{
done = true;