addToken checks if already in cache.
This commit is contained in:
parent
81bca52d1b
commit
2b9a2a7249
|
@ -42,7 +42,7 @@ 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 waiting; }
|
bool isWaiting() { return input->check_queue.isWaiting(); }
|
||||||
|
|
||||||
///empty the cache. Make sure no resource is locked before calling this.
|
///empty the cache. Make sure no resource is locked before calling this.
|
||||||
void flush() {
|
void flush() {
|
||||||
|
@ -120,9 +120,7 @@ class Cache: public Provider<Token> {
|
||||||
2) make room until eliminating an element would leave space. */
|
2) make room until eliminating an element would leave space. */
|
||||||
begin();
|
begin();
|
||||||
while(!this->quit) {
|
while(!this->quit) {
|
||||||
waiting = true;
|
input->check_queue.enter(true); //wait for cache below to load something or priorities to change
|
||||||
input->check_queue.enter(true); //wait for cache below to load someghing or priorities to change
|
|
||||||
waiting = false;
|
|
||||||
|
|
||||||
if(this->quit) break;
|
if(this->quit) break;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef GCACHE_CONTROLLER_H
|
#ifndef GCACHE_CONTROLLER_H
|
||||||
#define GCACHE_CONTROLLER_H
|
#define GCACHE_CONTROLLER_H
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
|
|
||||||
/** Allows to insert tokens, update priorities and generally control the cache.
|
/** Allows to insert tokens, update priorities and generally control the cache.
|
||||||
|
@ -35,9 +36,12 @@ class Controller {
|
||||||
caches.push_back(cache);
|
caches.push_back(cache);
|
||||||
}
|
}
|
||||||
///insert a token in the last provider (actual insertion is done on updatePriorities)
|
///insert a token in the last provider (actual insertion is done on updatePriorities)
|
||||||
void addToken(Token *token) {
|
bool addToken(Token *token) {
|
||||||
token->count = Token::CACHE;
|
if(token->count.testAndSetOrdered(Token::OUTSIDE, Token::CACHE)) {
|
||||||
tokens.push_back(token);
|
tokens.push_back(token);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
///WARNING: migh stall for the time needed to drop tokens from cache.
|
///WARNING: migh stall for the time needed to drop tokens from cache.
|
||||||
|
@ -122,9 +126,16 @@ class Controller {
|
||||||
}
|
}
|
||||||
///empty all caches
|
///empty all caches
|
||||||
void flush() {
|
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();
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
#ifndef CACHE_DOOR_H
|
#ifndef CACHE_DOOR_H
|
||||||
#define CACHE_DOOR_H
|
#define CACHE_DOOR_H
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -69,32 +69,41 @@ class QDoor {
|
||||||
class QDoor {
|
class QDoor {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
QDoor(void) : doorOpen(false) {}
|
QDoor(void) : doorOpen(false), waiting(false) {}
|
||||||
|
|
||||||
///opens the door. Threads trying to enter will be awakened
|
///opens the door. Threads trying to enter will be awakened
|
||||||
void open(void) {
|
void open(void) {
|
||||||
this->m.lock();
|
m.lock();
|
||||||
this->doorOpen = true;
|
doorOpen = true;
|
||||||
this->m.unlock();
|
m.unlock();
|
||||||
this->c.wakeAll();
|
c.wakeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
///attempt to enter the door. if the door is closed the thread will wait until the door is opened.
|
///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
|
/** 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 */
|
have only one thread entering the door each time open() is called */
|
||||||
void enter(bool close = false) {
|
void enter(bool close = false) {
|
||||||
this->m.lock();
|
m.lock();
|
||||||
while (!this->doorOpen)
|
waiting = true;
|
||||||
this->c.wait(&(this->m));
|
while (!doorOpen)
|
||||||
|
c.wait(&(m));
|
||||||
|
|
||||||
if(close)
|
if(close)
|
||||||
this->doorOpen = false;
|
doorOpen = false;
|
||||||
this->m.unlock();
|
waiting = false;
|
||||||
|
m.unlock();
|
||||||
|
}
|
||||||
|
bool isWaiting() {
|
||||||
|
m.lock();
|
||||||
|
bool w = waiting;
|
||||||
|
m.unlock();
|
||||||
|
return w;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
QMutex m;
|
QMutex m;
|
||||||
QWaitCondition c;
|
QWaitCondition c;
|
||||||
bool doorOpen;
|
bool doorOpen;
|
||||||
|
bool waiting;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue