private:
struct ManagedResource
{
+ enum State
+ {
+ NOT_LOADED,
+ LOAD_QUEUED,
+ LOADING,
+ LOAD_FINISHED,
+ LOADED
+ };
+
Resource *resource;
DataFile::Collection *collection;
std::string name;
IO::Seekable *io;
Resource::AsyncLoader *loader;
- bool loaded;
+ State state;
unsigned last_used;
UInt64 data_size;
std::vector<ResourceWatcher *> watchers;
ManagedResource(Resource &);
void start_loading();
+ bool process(bool);
+ void finish_loading(bool);
void finish_loading();
void unload();
void remove_watcher(ResourceWatcher &);
};
+ typedef std::list<ManagedResource *> LoadQueue;
+
class LoadingThread: public Thread
{
- public:
- enum State
- {
- IDLE,
- SYNC_PENDING,
- BUSY,
- LOAD_FINISHED,
- TERMINATING
- };
-
private:
- ResourceManager &manager;
Semaphore sem;
- ManagedResource *volatile resource;
- volatile State state;
+ Mutex queue_mutex;
+ LoadQueue async_queue;
+ LoadQueue sync_queue;
+ unsigned capacity;
+ unsigned size;
+ volatile bool done;
public:
- LoadingThread(ResourceManager &);
+ LoadingThread();
private:
virtual void main();
+ ManagedResource *front(LoadQueue &);
+
+ public:
+ void add_resource(ManagedResource &);
+ void remove_resource(ManagedResource &);
+ private:
+ bool try_remove_resource(ManagedResource &);
public:
- void set_resource(ManagedResource *);
- ManagedResource *get_resource() const { return resource; }
- void sync();
- State get_state() const { return state; }
+ bool sync();
+ bool needs_work() const { return size<capacity; }
void terminate();
};
typedef std::map<const Resource *, ManagedResource> ResourceMap;
- typedef std::list<ManagedResource *> LoadQueue;
LoadingPolicy policy;
bool async_loads;
void unwatch_resource(const Resource &, ResourceWatcher &);
void tick();
+private:
+ void dispatch_work();
+ void unload_by_age();
+ void unload_by_size();
+public:
UInt64 get_total_data_size() const;
+
+private:
+ static bool age_order(ManagedResource *, ManagedResource *);
};
} // namespace GL