From 2b9a2a72491c0172cfe1c0f835547d35e83f3d29 Mon Sep 17 00:00:00 2001 From: ponchio Date: Mon, 14 Mar 2011 11:35:43 +0000 Subject: [PATCH] addToken checks if already in cache. --- wrap/gcache/cache.h | 6 ++---- wrap/gcache/controller.h | 19 +++++++++++++++---- wrap/gcache/door.h | 31 ++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/wrap/gcache/cache.h b/wrap/gcache/cache.h index 6cecdee0..674f4a13 100644 --- a/wrap/gcache/cache.h +++ b/wrap/gcache/cache.h @@ -42,7 +42,7 @@ 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 waiting; } + bool isWaiting() { return input->check_queue.isWaiting(); } ///empty the cache. Make sure no resource is locked before calling this. void flush() { @@ -120,9 +120,7 @@ class Cache: public Provider { 2) make room until eliminating an element would leave space. */ begin(); while(!this->quit) { - waiting = true; - input->check_queue.enter(true); //wait for cache below to load someghing or priorities to change - waiting = false; + input->check_queue.enter(true); //wait for cache below to load something or priorities to change if(this->quit) break; diff --git a/wrap/gcache/controller.h b/wrap/gcache/controller.h index 812f37a5..96bf05c4 100644 --- a/wrap/gcache/controller.h +++ b/wrap/gcache/controller.h @@ -1,6 +1,7 @@ #ifndef GCACHE_CONTROLLER_H #define GCACHE_CONTROLLER_H +#include #include "cache.h" /** Allows to insert tokens, update priorities and generally control the cache. @@ -35,9 +36,12 @@ class Controller { caches.push_back(cache); } ///insert a token in the last provider (actual insertion is done on updatePriorities) - void addToken(Token *token) { - token->count = Token::CACHE; - tokens.push_back(token); + bool addToken(Token *token) { + if(token->count.testAndSetOrdered(Token::OUTSIDE, Token::CACHE)) { + tokens.push_back(token); + return true; + } + return false; } ///WARNING: migh stall for the time needed to drop tokens from cache. @@ -122,9 +126,16 @@ class Controller { } ///empty all caches void flush() { - for(unsigned int i = caches.size()-1; i >= 0; i--) + for(int i = (int)caches.size()-1; i >= 0; i--) caches[i]->flush(); } + bool isWaiting() { + for(int i = (int)caches.size() -1; i >= 0; i--) { + if(!caches[i]->input->check_queue.isWaiting()) return false; + } + qDebug() << "is waiting"; + return true; + } }; diff --git a/wrap/gcache/door.h b/wrap/gcache/door.h index df8d08c7..ed0e687c 100644 --- a/wrap/gcache/door.h +++ b/wrap/gcache/door.h @@ -25,7 +25,7 @@ #ifndef CACHE_DOOR_H #define CACHE_DOOR_H - +#include /* @@ -69,32 +69,41 @@ class QDoor { class QDoor { public: - QDoor(void) : doorOpen(false) {} + QDoor(void) : doorOpen(false), waiting(false) {} ///opens the door. Threads trying to enter will be awakened void open(void) { - this->m.lock(); - this->doorOpen = true; - this->m.unlock(); - this->c.wakeAll(); + m.lock(); + doorOpen = true; + m.unlock(); + c.wakeAll(); } ///attempt to enter the door. if the door is closed the thread will wait until the door is opened. /** if close is true, the door will be closed after the thread is awakened, this allows to have only one thread entering the door each time open() is called */ void enter(bool close = false) { - this->m.lock(); - while (!this->doorOpen) - this->c.wait(&(this->m)); + m.lock(); + waiting = true; + while (!doorOpen) + c.wait(&(m)); if(close) - this->doorOpen = false; - this->m.unlock(); + doorOpen = false; + waiting = false; + m.unlock(); + } + bool isWaiting() { + m.lock(); + bool w = waiting; + m.unlock(); + return w; } private: QMutex m; QWaitCondition c; bool doorOpen; + bool waiting; };