void ResourceManager::add_resource(Resource &r)
{
+ MutexLock lock(map_mutex);
insert_unique(resources, &r, ManagedResource(r));
}
-void *ResourceManager::get_data_for_resource(const Resource &r)
+const ResourceManager::ManagedResource &ResourceManager::get_managed_resource(const Resource &r) const
{
- return &get_item(resources, &r);
+ MutexLock lock(map_mutex);
+ return get_item(resources, &r);
+}
+
+ResourceManager::ManagedResource &ResourceManager::get_managed_resource(const Resource &r)
+{
+ MutexLock lock(map_mutex);
+ return get_item(resources, &r);
}
void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
void ResourceManager::set_resource_location(Resource &r, const ResourceLocation &l)
{
- ManagedResource &managed = get_item(resources, &r);
- managed.location = l;
+ {
+ MutexLock lock(map_mutex);
+ ManagedResource &managed = get_item(resources, &r);
+ managed.location = l;
+ }
if(policy==LOAD_IMMEDIATELY)
load_resource(r);
const ResourceManager::ResourceLocation *ResourceManager::get_resource_location(const Resource &r) const
{
- const ManagedResource &managed = get_item(resources, &r);
+ const ManagedResource &managed = get_managed_resource(r);
return managed.location.collection ? &managed.location : 0;
}
void ResourceManager::load_resource(Resource &r)
{
- ManagedResource &managed = get_item(resources, &r);
+ ManagedResource &managed = get_managed_resource(r);
if(!managed.location.collection)
throw runtime_error("no location");
void ResourceManager::remove_resource(Resource &r)
{
- ManagedResource &managed = get_item(resources, &r);
+ ManagedResource &managed = get_managed_resource(r);
ManagedResource::State state = managed.state;
if(state==ManagedResource::LOAD_QUEUED)
{
}
else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
thread.remove_resource(managed);
+
+ MutexLock lock(map_mutex);
remove_existing(resources, &r);
}
void ResourceManager::watch_resource(const Resource &r, ResourceWatcher &w)
{
- get_item(resources, &r).add_watcher(w);
+ get_managed_resource(r).add_watcher(w);
}
void ResourceManager::unwatch_resource(const Resource &r, ResourceWatcher &w)
{
- get_item(resources, &r).remove_watcher(w);
+ get_managed_resource(r).remove_watcher(w);
}
void ResourceManager::tick()
if(do_unload)
{
+ MutexLock lock(map_mutex);
if(max_retain_frames && frame>=next_unload)
{
unload_by_age();
{
unsigned unload_limit = frame-min_retain_frames;
- while(get_total_data_size()>size_limit)
+ while(get_total_data_size_()>size_limit)
{
ManagedResource *best = 0;
UInt64 best_impact = 0;
}
}
-UInt64 ResourceManager::get_total_data_size() const
+UInt64 ResourceManager::get_total_data_size_() const
{
UInt64 total = 0;
for(ResourceMap::const_iterator i=resources.begin(); i!=resources.end(); ++i)
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;
LoadingPolicy policy;
bool async_loads;
+ mutable Mutex map_mutex;
ResourceMap resources;
LoadQueue queue;
UInt64 size_limit;
void set_max_retain_frames(unsigned);
void add_resource(Resource &);
- void *get_data_for_resource(const Resource &);
+private:
+ const ManagedResource &get_managed_resource(const Resource &) const;
+ ManagedResource &get_managed_resource(const Resource &);
+public:
+ void *get_data_for_resource(const Resource &r) { return &get_managed_resource(r); }
void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
void set_resource_location(Resource &, const ResourceLocation &);
const ResourceLocation *get_resource_location(const Resource &) const;
void dispatch_work();
void unload_by_age();
void unload_by_size();
+ UInt64 get_total_data_size_() const;
public:
UInt64 get_total_data_size() const;