Updated to use the threading classes defined in wrap/system/multithreading

This commit is contained in:
Paolo Cignoni 2011-12-14 17:42:23 +00:00
parent 2aa0988989
commit e1c9f212da
4 changed files with 31 additions and 31 deletions

View File

@ -6,8 +6,8 @@
#include <limits.h>
#include <vector>
#include <list>
#include <wrap/system/multithreading/mt.h>
#include <QThread>
#include "provider.h"
using namespace std;
@ -91,7 +91,7 @@ public:
std::vector<Token *> tokens;
{
int count = 0;
QMutexLocker locker(&(this->heap_lock));
mt::mutexlocker locker(&(this->heap_lock));
for(int k = 0; k < this->heap.size(); k++) {
Token *token = &this->heap[k];
if(functor(token)) { //drop it
@ -107,7 +107,7 @@ public:
this->heap_dirty = true;
}
{
QMutexLocker locker(&(input->heap_lock));
mt::mutexlocker locker(&(input->heap_lock));
for(unsigned int i = 0; i < tokens.size(); i++) {
input->heap.push(tokens[i]);
}
@ -165,7 +165,7 @@ protected:
//1 we need to make room (capacity < current)
if(size() > capacity()) {
QMutexLocker locker(&(this->heap_lock));
mt::mutexlocker locker(&(this->heap_lock));
//2 we have some element not in the upper caches (heap.size() > 0
if(this->heap.size()) {
@ -194,7 +194,7 @@ protected:
if(remove) {
{
QMutexLocker input_locker(&(input->heap_lock));
mt::mutexlocker input_locker(&(input->heap_lock));
int size = drop(remove);
assert(size >= 0);
s_curr -= size;
@ -214,7 +214,7 @@ protected:
empty heap is bad: we cannot drop anything to make room, and cache above has nothing to get.
this should not happen if we set correct cache sizes, but if it happens.... */
{
QMutexLocker locker(&(this->heap_lock));
mt::mutexlocker locker(&(this->heap_lock));
this->rebuild();
if(size() > capacity() && this->heap.size() > 0) {
last = &(this->heap.min()); //no room, set last so we might check for a swap.
@ -222,7 +222,7 @@ protected:
}
{
QMutexLocker input_locker(&(input->heap_lock));
mt::mutexlocker input_locker(&(input->heap_lock));
input->rebuild(); //if dirty rebuild
if(input->heap.size()) { //we need something in input to tranfer.
Token &first = input->heap.max();
@ -240,7 +240,7 @@ protected:
if(size >= 0) { //success
s_curr += size;
{
QMutexLocker locker(&(this->heap_lock));
mt::mutexlocker locker(&(this->heap_lock));
if(final)
insert->count.ref(); //now lock is 0 and can be locked
@ -250,7 +250,7 @@ protected:
return true;
} else { //failed transfer put it back, we will keep trying to transfer it...
QMutexLocker input_locker(&(input->heap_lock));
mt::mutexlocker input_locker(&(input->heap_lock));
input->heap.push(insert);
return false;
}
@ -263,7 +263,7 @@ protected:
/*
template<typename Token>
class Transfer: public QThread {
class Transfer: public mt::thread {
public:
Transfer(Cache<Token> *_cache): cache(_cache) {}
private:

View File

@ -60,7 +60,7 @@ class Controller {
///if more tokens than m present in the provider, lowest priority ones will be removed
void setMaxTokens(int m) {
QMutexLocker l(&provider.heap_lock);
mt::mutexlocker l(&provider.heap_lock);
provider.max_tokens = m;
}
@ -68,7 +68,7 @@ class Controller {
void updatePriorities() {
if(tokens.size()) {
QMutexLocker l(&provider.heap_lock);
mt::mutexlocker l(&provider.heap_lock);
for(unsigned int i = 0; i < tokens.size(); i++)
provider.heap.push(tokens[i]);
tokens.clear();

View File

@ -25,12 +25,13 @@
#ifndef CACHE_DOOR_H
#define CACHE_DOOR_H
#include <QDebug>
#include <QMutex>
#include <QSemaphore>
#include <QAtomicInt>
#include <QWaitCondition>
#include <wrap/system/multithreading/mt.h>
#ifdef QT_CORE_LIB
#include <QWaitCondition>
#endif
#include <QAtomicInt>
#define METHOD_2
@ -38,8 +39,8 @@
class QDoor {
private:
QSemaphore door;
QMutex room; //lock when entering. unlock when exiting
mt::semaphore door;
mt::mutex room; //lock when entering. unlock when exiting
QAtomicInt key; //keep tracks of door status
public:
@ -77,11 +78,11 @@ class QDoor {
class QDoor {
private:
QSemaphore _open;
QSemaphore _close;
mt::semaphore _open;
mt::semaphore _close;
public:
QMutex room;
mt::mutex room;
QDoor(): _open(0), _close(1) {} //this means closed
void open() {
@ -175,7 +176,7 @@ class QDoor {
m.unlock();
}
private:
QMutex m;
mt::mutex m;
QWaitCondition c;
bool doorOpen;
bool waiting;

View File

@ -1,8 +1,7 @@
#ifndef GCACHE_PROVIDER_H
#define GCACHE_PROVIDER_H
#include <QMutex>
#include <wrap/system/multithreading/mt.h>
#include "dheap.h"
#include "door.h"
@ -19,7 +18,7 @@
*/
template <typename Token>
class Provider: public QThread {
class Provider: public mt::thread {
public:
///holds the resources in this cache but not in the cache above
PtrDHeap<Token> heap;
@ -28,9 +27,9 @@ class Provider: public QThread {
///signals we need to rebuild heap.
bool heap_dirty;
///lock this before manipulating heap.
QMutex heap_lock;
mt::mutex heap_lock;
///used to sincronize priorities update
QMutex priority_lock;
mt::mutex priority_lock;
///signals (to next cache!) priorities have changed or something is available
QDoor check_queue;
@ -39,7 +38,7 @@ class Provider: public QThread {
/// [should be protected, do not use]
void pushPriorities() {
QMutexLocker locker(&priority_lock);
mt::mutexlocker locker(&priority_lock);
for(int i = 0; i < heap.size(); i++)
heap[i].pushPriority();
heap_dirty = true;
@ -50,7 +49,7 @@ class Provider: public QThread {
if(!this->heap_dirty) return;
{
QMutexLocker locker(&priority_lock);
mt::mutexlocker locker(&priority_lock);
for(int i = 0; i < this->heap.size(); i++)
this->heap[i].pullPriority();
this->heap_dirty = false;
@ -70,7 +69,7 @@ class Provider: public QThread {
///ensure no locked item are to be removed [should be protected, do not use]
template <class FUNCTOR> void flush(FUNCTOR functor) {
int count = 0;
QMutexLocker locker(&(this->heap_lock));
mt::mutexlocker locker(&(this->heap_lock));
for(int k = 0; k < this->heap.size(); k++) {
Token *token = &this->heap[k];
if(functor(token)) { //drop it