added isChanged for polling changes in cache

This commit is contained in:
Federico Ponchio 2011-11-19 00:39:14 +00:00
parent 6f804cbb20
commit 11e859db83
3 changed files with 48 additions and 14 deletions

View File

@ -24,7 +24,7 @@ class Cache: public Provider<Token> {
public: public:
bool final; //true if this is the last cache (the one we use the data from) bool final; //true if this is the last cache (the one we use the data from)
bool quit; //graceful exit bool quit; //graceful exit
bool waiting; bool changed;
///data is fetched from here ///data is fetched from here
Provider<Token> *input; Provider<Token> *input;
@ -36,7 +36,7 @@ class Cache: public Provider<Token> {
public: public:
Cache(quint64 _capacity = INT_MAX): Cache(quint64 _capacity = INT_MAX):
final(false), quit(false), waiting(false), input(NULL), s_max(_capacity), s_curr(0) {} final(false), quit(false), changed(false), input(NULL), s_max(_capacity), s_curr(0) {}
virtual ~Cache() {} virtual ~Cache() {}
void setInputCache(Provider<Token> *p) { input = p; } void setInputCache(Provider<Token> *p) { input = p; }
@ -44,7 +44,12 @@ class Cache: public Provider<Token> {
quint64 size() { return s_curr; } quint64 size() { return s_curr; }
void setCapacity(quint64 c) { s_max = c; } void setCapacity(quint64 c) { s_max = c; }
///return true if the cache is waiting for priority to change ///return true if the cache is waiting for priority to change
bool isWaiting() { return input->check_queue.isWaiting(); } bool isChanged() {
bool r = changed;
changed = false;
return r;
//return input->check_queue.isWaiting();
}
///empty the cache. Make sure no resource is locked before calling this. Require pause or stop before. ///empty the cache. Make sure no resource is locked before calling this. Require pause or stop before.
void flush() { void flush() {
@ -105,9 +110,11 @@ class Cache: public Provider<Token> {
///return the space used in the cache by the loaded resource ///return the space used in the cache by the loaded resource
virtual int size(Token *token) = 0; virtual int size(Token *token) = 0;
///returns amount of space used in cache -1 for failed transfer ///returns amount of space used in cache -1 for failed transfer
virtual int get(Token *token) = 0; virtual int get(Token *token) = 0;
///return amount removed ///return amount removed
virtual int drop(Token *token) = 0; virtual int drop(Token *token) = 0;
///
virtual Token *ready() { return NULL; }
///called in as first thing in run() ///called in as first thing in run()
virtual void begin() {} virtual void begin() {}
@ -127,8 +134,10 @@ class Cache: public Provider<Token> {
if(this->quit) break; if(this->quit) break;
if(unload() || load()) if(unload() || load()) {
changed = true; //some modification happened
input->check_queue.open(); //we signal ourselves to check again input->check_queue.open(); //we signal ourselves to check again
}
} }
flush(); flush();
this->quit = false; //in case someone wants to restart; this->quit = false; //in case someone wants to restart;

View File

@ -124,7 +124,6 @@ class Controller {
for(unsigned int i = 0; i < caches.size()-1; i++) for(unsigned int i = 0; i < caches.size()-1; i++)
caches[i]->check_queue.unlock(); caches[i]->check_queue.unlock();
/* provider.heap_lock.unlock(); /* provider.heap_lock.unlock();
for(unsigned int i = 0; i < caches.size(); i++) for(unsigned int i = 0; i < caches.size(); i++)
caches[i]->heap_lock.unlock(); */ caches[i]->heap_lock.unlock(); */
@ -138,12 +137,21 @@ class Controller {
provider.heap.clear(); provider.heap.clear();
resume(); resume();
} }
bool isChanged() {
bool isWaiting() { bool c = false;
for(int i = (int)caches.size() -1; i >= 0; i--) { for(int i = (int)caches.size() -1; i >= 0; i--) {
if(!caches[i]->input->check_queue.isWaiting()) return false; c |= caches[i]->isChanged();
} }
return true; return c;
}
bool isWaiting() {
bool waiting = true;
for(int i = (int)caches.size() -1; i >= 0; i--) {
//waiting &= caches[i]->isWaiting();
waiting &= caches[i]->input->check_queue.isWaiting();
}
qDebug() << "Waiting? " << waiting;
return waiting;
} }
}; };

View File

@ -27,12 +27,15 @@
#include <QDebug> #include <QDebug>
//#define USE_SEMAPHORES
#ifdef USE_SEMAPHORES
/*
//a door needs to be open for the thread to continue, //a door needs to be open for the thread to continue,
//if it is open the thread enter and closes the door //if it is open the thread enter and closes the door
//this mess is to avoid [if(!open.available()) open.release(1)] //this mess is to avoid [if(!open.available()) open.release(1)]
#include <QSemaphore> #include <QSemaphore>
class QDoor { class QDoor {
private: private:
QSemaphore _open; QSemaphore _open;
@ -53,11 +56,24 @@ class QDoor {
_close.release(1); //and close door behind _close.release(1); //and close door behind
else else
_open.release(1); //and leave door opened _open.release(1); //and leave door opened
} }
bool isOpen() { return _open.available() == 1; } bool isOpen() { return _open.available() == 1; }
void lock() {
//door might be open or closed, but we might happen just in the middle
//of someone opening, closing or entering it.
while(!_open.tryAcquire(1) && !_close.tryAcquire(1)) {}
//no resources left
}
void unlock() {
//unlock will not open the door, but allow someone to open it.
_close.release(1);
}
}; };
*/
#else
#include <QMutex> #include <QMutex>
#include <QWaitCondition> #include <QWaitCondition>
@ -112,5 +128,6 @@ class QDoor {
bool waiting; bool waiting;
}; };
#endif //ifdef USE_SEMAPHORES
#endif #endif //CACHE_DOOR_H