From 11e859db83860ea0dcae8a38eed5b967ee6736e5 Mon Sep 17 00:00:00 2001 From: ponchio Date: Sat, 19 Nov 2011 00:39:14 +0000 Subject: [PATCH] added isChanged for polling changes in cache --- wrap/gcache/cache.h | 21 +++++++++++++++------ wrap/gcache/controller.h | 18 +++++++++++++----- wrap/gcache/door.h | 23 ++++++++++++++++++++--- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/wrap/gcache/cache.h b/wrap/gcache/cache.h index 812bbecd..c266524f 100644 --- a/wrap/gcache/cache.h +++ b/wrap/gcache/cache.h @@ -24,7 +24,7 @@ class Cache: public Provider { public: bool final; //true if this is the last cache (the one we use the data from) bool quit; //graceful exit - bool waiting; + bool changed; ///data is fetched from here Provider *input; @@ -36,7 +36,7 @@ class Cache: public Provider { public: 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() {} void setInputCache(Provider *p) { input = p; } @@ -44,7 +44,12 @@ class Cache: public Provider { quint64 size() { return s_curr; } void setCapacity(quint64 c) { s_max = c; } ///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. void flush() { @@ -105,9 +110,11 @@ class Cache: public Provider { ///return the space used in the cache by the loaded resource virtual int size(Token *token) = 0; ///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 - virtual int drop(Token *token) = 0; + virtual int drop(Token *token) = 0; + /// + virtual Token *ready() { return NULL; } ///called in as first thing in run() virtual void begin() {} @@ -127,8 +134,10 @@ class Cache: public Provider { 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 + } } flush(); this->quit = false; //in case someone wants to restart; diff --git a/wrap/gcache/controller.h b/wrap/gcache/controller.h index dcac7b01..23b6a7db 100644 --- a/wrap/gcache/controller.h +++ b/wrap/gcache/controller.h @@ -124,7 +124,6 @@ class Controller { for(unsigned int i = 0; i < caches.size()-1; i++) caches[i]->check_queue.unlock(); - /* provider.heap_lock.unlock(); for(unsigned int i = 0; i < caches.size(); i++) caches[i]->heap_lock.unlock(); */ @@ -138,12 +137,21 @@ class Controller { provider.heap.clear(); resume(); } - - bool isWaiting() { + bool isChanged() { + bool c = false; 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; } }; diff --git a/wrap/gcache/door.h b/wrap/gcache/door.h index 7dfbca1d..c095d42b 100644 --- a/wrap/gcache/door.h +++ b/wrap/gcache/door.h @@ -27,12 +27,15 @@ #include +//#define USE_SEMAPHORES +#ifdef USE_SEMAPHORES -/* //a door needs to be open for the thread to continue, //if it is open the thread enter and closes the door //this mess is to avoid [if(!open.available()) open.release(1)] + #include + class QDoor { private: QSemaphore _open; @@ -53,11 +56,24 @@ class QDoor { _close.release(1); //and close door behind else _open.release(1); //and leave door opened + } 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 #include @@ -112,5 +128,6 @@ class QDoor { bool waiting; }; +#endif //ifdef USE_SEMAPHORES -#endif +#endif //CACHE_DOOR_H