addToken checks if already in cache.

This commit is contained in:
Federico Ponchio 2011-03-14 11:35:43 +00:00
parent 81bca52d1b
commit 2b9a2a7249
3 changed files with 37 additions and 19 deletions

View File

@ -42,7 +42,7 @@ class Cache: public Provider<Token> {
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<Token> {
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;

View File

@ -1,6 +1,7 @@
#ifndef GCACHE_CONTROLLER_H
#define GCACHE_CONTROLLER_H
#include <QDebug>
#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;
}
};

View File

@ -25,7 +25,7 @@
#ifndef CACHE_DOOR_H
#define CACHE_DOOR_H
#include <QDebug>
/*
@ -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;
};