added multithreading

This commit is contained in:
Federico Ponchio 2011-12-09 16:06:08 +00:00
parent f4010f0dce
commit 4400d8dd2d
24 changed files with 1719 additions and 615 deletions

View File

@ -31,8 +31,8 @@ public:
//if true the cache will exit at the first opportunity
bool quit;
///keeps track of changes (if 1 then something was loaded or dropped
QAtomicInt changed;
///callback for changed
QAtomicInt new_data;
///callback for new_data
void (*callback)(void *data);
///data is fetched from here
@ -49,7 +49,7 @@ protected:
public:
Cache(uint64_t _capacity = INT_MAX):
final(false), quit(false), changed(false), input(NULL), s_max(_capacity), s_curr(0) {}
final(false), quit(false), new_data(false), input(NULL), s_max(_capacity), s_curr(0) {}
virtual ~Cache() {}
void setInputCache(Provider<Token> *p) { input = p; }
@ -58,8 +58,8 @@ public:
void setCapacity(uint64_t c) { s_max = c; }
///return true if the cache is waiting for priority to change
bool isChanged() {
bool r = changed.testAndSetOrdered(1, 0); //if changed is 1, r is true
bool newData() {
bool r = new_data.testAndSetOrdered(1, 0); //if changed is 1, r is true
return r;
}
@ -145,7 +145,7 @@ protected:
if(this->quit) break;
if(unload() || load()) {
changed.testAndSetOrdered(0, 1); //if not changed, set as changed
new_data.testAndSetOrdered(0, 1); //if not changed, set as changed
input->check_queue.open(); //we signal ourselves to check again
}
input->check_queue.leave();

View File

@ -160,10 +160,10 @@ class Controller {
provider.heap.clear();
}
bool isChanged() {
bool newData() {
bool c = false;
for(int i = (int)caches.size() -1; i >= 0; i--) {
c |= caches[i]->isChanged();
c |= caches[i]->newData();
}
return c;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,20 @@
#include "cursorshapearea.h"
CursorShapeArea::CursorShapeArea(QDeclarativeItem *parent) :
QDeclarativeItem(parent)
{
}
Qt::CursorShape CursorShapeArea::cursorShape() const
{
return cursor().shape();
}
void CursorShapeArea::setCursorShape(Qt::CursorShape cursorShape)
{
if (cursor().shape() == cursorShape)
return;
setCursor(cursorShape);
emit cursorShapeChanged();
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,2 @@
#inclue "qgraphicsmatrix3d.h"

View File

@ -0,0 +1,16 @@
#include "../platform.h"
#ifndef GC_ATOMIC_INT_H
#define GC_ATOMIC_INT_H
#ifdef QT_CORE_LIB
#include <QAtomicInt>
typedef QAtomicInt GAtomincInt;
#elif defined(__GC_APPLE__) ??
# include "GCAtomicInt_apple.h"
#endif
#endif

View File

@ -0,0 +1,118 @@
#ifndef GC_ATOMIC_INT_APPLE_H
#define GC_ATOMIC_INT_APPLE_H
#include <libkern/OSAtomic.h>
//http://developer.apple.com/library/mac/#documentation/Darwin/Reference/KernelIOKitFramework/OSAtomic_h/index.html
class GCAtomicInt
{
public:
GCAtomicInt()
{
_q_value = 0;
}
GCAtomicInt( int value )
{
_q_value = value;
}
// atomic API
/**
Reads the current value of this QAtomicInt and then adds valueToAdd
to the current value, returning the original value.
Unfortunately, MacOSX does not provide with fetch-and-add functions,
only add-and-fetch. Therefore, we have to simulate them.
Implementation based on SDL:
//http://lists.libsdl.org/pipermail/commits-libsdl.org/2011-January/003568.html
*/
inline int fetchAndAddAcquire( int valueToAdd )
{
//T *originalValue = currentValue;
//currentValue += valueToAdd;
//return originalValue;
int originalValue;
do {
originalValue = _q_value;
} while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &_q_value));
return originalValue;
}
/**
Atomically increments the value of this GCAtomicInt.
Returns true if the new value is non-zero, false otherwise.*/
inline bool ref()
{
return OSAtomicIncrement32Barrier(&_q_value) != 0;
}
/*
Atomically decrements the value of this QAtomicInt.
Returns true if the new value is non-zero, false otherwise.*/
inline bool deref()
{
return OSAtomicDecrement32Barrier(&_q_value) != 0;
}
inline bool testAndSetOrdered(int expectedValue, int newValue)
{
//if (currentValue == expectedValue) {
// currentValue = newValue;
// return true;
// }
//return false;
return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &_q_value);
}
// Non-atomic API
inline bool operator==(int value) const
{
return _q_value == value;
}
inline bool operator!=(int value) const
{
return _q_value != value;
}
inline bool operator!() const
{
return _q_value == 0;
}
inline operator int() const
{
return _q_value;
}
inline GCAtomicInt &operator=(int value)
{
_q_value = value;
return *this;
}
inline bool operator>(int value) const
{
return _q_value > value;
}
inline bool operator<(int value) const
{
return _q_value < value;
}
private:
volatile int _q_value;
};
#endif

View File

@ -0,0 +1,18 @@
#ifndef MT_BASE_H
#define MT_BASE_H
#include <assert.h>
#define MT_PREVENT_COPY(CLASS_NAME) \
private: \
CLASS_NAME (const CLASS_NAME &); \
CLASS_NAME & operator = (const CLASS_NAME &);
#define MT_ASSERT assert
namespace mt
{
}
#endif // MT_BASE_H

View File

@ -0,0 +1,53 @@
#ifndef MT_CONDITION_H
#define MT_CONDITION_H
#include "base.h"
#include "mutex.h"
#include <pthread.h>
namespace mt
{
class condition
{
MT_PREVENT_COPY(condition)
public:
typedef condition this_type;
typedef void base_type;
condition(void)
{
pthread_cond_init(&(this->c), 0);
}
~condition(void)
{
pthread_cond_destroy(&(this->c));
}
void signal(void)
{
pthread_cond_signal(&(this->c));
}
void broadcast(void)
{
pthread_cond_broadcast(&(this->c));
}
void wait(mutex & m)
{
pthread_cond_wait(&(this->c), &(m.m));
}
private:
pthread_cond_t c;
};
}
#endif // MT_CONDITION_H

23
wrap/system/multithreading/mt.h Executable file
View File

@ -0,0 +1,23 @@
#ifndef MT_MT_H
#define MT_MT_H
#ifdef QT_CORE_LIB
#include <QSemaphore>
tyepdef QSemaphore mt::Semaphore;
#else
#include "base.h"
#include "mutex.h"
#include "rw_lock.h"
#include "semaphore.h"
#include "thread.h"
#include "scoped_mutex_lock.h"
#include "scoped_read_lock.h"
#include "scoped_write_lock.h"
#endif
#endif // MT_MT_H

View File

@ -0,0 +1,51 @@
#ifndef MT_MUTEX_H
#define MT_MUTEX_H
#include "base.h"
#include <pthread.h>
namespace mt
{
class condition;
class mutex
{
MT_PREVENT_COPY(mutex)
public:
typedef mutex this_type;
typedef void base_type;
mutex(void)
{
pthread_mutex_init(&(this->m), 0);
}
~mutex(void)
{
pthread_mutex_destroy(&(this->m));
}
void lock(void)
{
pthread_mutex_lock(&(this->m));
}
void unlock(void)
{
pthread_mutex_unlock(&(this->m));
}
private:
friend class condition;
pthread_mutex_t m;
};
}
#endif // MT_MUTEX_H

View File

@ -0,0 +1,57 @@
#ifndef MT_RW_LOCK_H
#define MT_RW_LOCK_H
#include "base.h"
#include <pthread.h>
namespace mt
{
class rw_lock
{
MT_PREVENT_COPY(rw_lock)
public:
typedef rw_lock this_type;
typedef void base_type;
rw_lock(void)
{
pthread_rwlock_init(&(this->rw), 0);
}
~rw_lock(void)
{
pthread_rwlock_destroy(&(this->rw));
}
void lock_read(void)
{
pthread_rwlock_rdlock(&(this->rw));
}
void unlock_read(void)
{
pthread_rwlock_unlock(&(this->rw));
}
void lock_write(void)
{
pthread_rwlock_wrlock(&(this->rw));
}
void unlock_write(void)
{
pthread_rwlock_unlock(&(this->rw));
}
private:
pthread_rwlock_t rw;
};
}
#endif // MT_RW_LOCK_H

View File

@ -0,0 +1,80 @@
#ifndef MT_SCOPED_MUTEX_LOCK_H
#define MT_SCOPED_MUTEX_LOCK_H
#include "mutex.h"
namespace mt
{
class scoped_mutex_lock
{
MT_PREVENT_COPY(scoped_mutex_lock)
public:
typedef scoped_mutex_lock this_type;
typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
protected:
mutex & mtx;
};
}
#endif // MT_SCOPED_MUTEX_LOCK_H
/*
#ifndef MT_SCOPED_MUTEX_LOCK_H
#define MT_SCOPED_MUTEX_LOCK_H
#include "mutex.h"
namespace mt
{
class scoped_mutex_lock
{
MT_PREVENT_COPY(scoped_mutex_lock)
public:
typedef scoped_mutex_lock this_type;
typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
protected:
mutex & mtx;
};
}
#endif // MT_SCOPED_MUTEX_LOCK_H
*/

View File

@ -0,0 +1,35 @@
#ifndef MT_SCOPED_READ_LOCK_H
#define MT_SCOPED_READ_LOCK_H
#include "rw_lock.h"
namespace mt
{
class scoped_read_lock
{
MT_PREVENT_COPY(scoped_read_lock)
public:
typedef scoped_read_lock this_type;
typedef void base_type;
scoped_read_lock(rw_lock & rwl) : rw(rwl)
{
this->rw.lock_read();
}
~scoped_read_lock(void)
{
this->rw.unlock_read();
}
protected:
rw_lock & rw;
};
}
#endif // MT_SCOPED_READ_LOCK_H

View File

@ -0,0 +1,35 @@
#ifndef MT_SCOPED_WRITE_LOCK_H
#define MT_SCOPED_WRITE_LOCK_H
#include "rw_lock.h"
namespace mt
{
class scoped_write_lock
{
MT_PREVENT_COPY(scoped_write_lock)
public:
typedef scoped_write_lock this_type;
typedef void base_type;
scoped_write_lock(rw_lock & rwl) : rw(rwl)
{
this->rw.lock_write();
}
~scoped_write_lock(void)
{
this->rw.unlock_write();
}
protected:
rw_lock & rw;
};
}
#endif // MT_SCOPED_WRITE_LOCK_H

View File

@ -0,0 +1,72 @@
#ifndef MT_SEMAPHORE_H
#define MT_SEMAPHORE_H
#include "base.h"
#include <semaphore.h>
namespace mt
{
class semaphore
{
MT_PREVENT_COPY(semaphore)
public:
typedef semaphore this_type;
typedef void base_type;
semaphore(void)
{
sem_init(&(this->s), 0, 0);
}
semaphore(int value)
{
sem_init(&(this->s), 0, value);
}
~semaphore(void)
{
sem_destroy(&(this->s));
}
void post(void)
{
sem_post(&(this->s));
}
/*
void post(int n)
{
sem_post_multiple(&(this->s), n);
}
*/
void wait(void)
{
sem_wait(&(this->s));
}
bool trywait(void)
{
return (sem_trywait(&(this->s)) == 0);
}
//jnoguera 11-Nov-2011
int available()
{
int value;
sem_getvalue( &(this->s), &value );
return value;
}
private:
sem_t s;
};
}
#endif // MT_SEMAPHORE_H

View File

@ -0,0 +1,111 @@
#ifndef MT_THREAD_H
#define MT_THREAD_H
#include <memory.h>
#include "base.h"
#include <pthread.h>
namespace mt
{
class thread
{
MT_PREVENT_COPY(thread)
public:
typedef thread this_type;
typedef void base_type;
thread(void) : flags(0)
{
;
}
virtual ~thread(void)
{
;
}
virtual bool start(void)
{
if ((this->flags & thread_started) != 0) return false;
pthread_create(&(this->tid), 0, this_type::thread_func, reinterpret_cast<void *>(this));
/*
sched_param sp;
memset(&sp, 0, sizeof(sched_param));
sp.sched_priority = sched_get_priority_min(SCHED_OTHER);
sp.sched_priority = 0; // normal
pthread_setschedparam(this->tid, SCHED_OTHER, &sp);
*/
this->flags |= thread_started;
return true;
}
virtual bool wait(void)
{
if ((this->flags & thread_started) == 0) return false;
pthread_join(this->tid, 0);
this->flags &= ~thread_started;
return true;
}
virtual bool kill(void)
{
if ((this->flags & thread_started) == 0) return false;
pthread_kill(this->tid, 0);
this->flags &= ~(thread_started | thread_running);
return true;
}
bool is_started(void) const
{
return ((this->flags & thread_started) != 0);
}
bool is_running(void) const
{
return ((this->flags & thread_running) != 0);
}
protected:
virtual void run(void)
{
;
}
private:
enum thread_flags
{
thread_none = ( 0),
thread_started = (1 << 0),
thread_running = (1 << 1)
};
volatile unsigned int flags;
pthread_t tid;
static void * thread_func(void * param)
{
this_type * p_this = reinterpret_cast<this_type *>(param);
MT_ASSERT(p_this != 0);
p_this->flags |= thread_running;
p_this->run();
p_this->flags &= ~thread_running;
return 0;
}
};
}
#endif // MT_THREAD_H

View File

@ -0,0 +1,41 @@
#ifndef MT_UTIL_H
#define MT_UTIL_H
#if (defined(_MSC_VER))
#include <windows.h>
#elif (defined(__MINGW32__))
#include <windows.h>
#elif (defined(__linux__))
#include <unistd.h>
#else
#include <unistd.h>
//#error "mt utils.h : unrecognized environment."
#endif
namespace mt
{
inline void sleep_ms(unsigned int msecs)
{
#if (defined(_MSC_VER))
Sleep(DWORD(msecs));
#elif (defined(__MINGW32__))
Sleep(DWORD(msecs));
#elif (defined(__linux__))
const unsigned int secs = ((unsigned int)(msecs / 1000));
const unsigned long usecs = ((unsigned long)((msecs % 1000) * 1000));
sleep(secs);
usleep(usecs);
#else
const unsigned int secs = ((unsigned int)(msecs / 1000));
const unsigned long usecs = ((unsigned long)((msecs % 1000) * 1000));
sleep(secs);
usleep(usecs);
#endif
}
}
#endif // MT_UTIL_H

View File

@ -224,7 +224,7 @@ bool GetOpt::parse(QString &error) {
return false;
}
arg = args[i];
if(i == args.size() || arg[0] == '-') {
if(i == args.size()) {
error = "Missing argument after option '" + arg + "'";
return false;
}

View File

@ -0,0 +1,175 @@
#include "L3DChronometer.h"
/******************************************************************************
Implementacion de metodos multiplataforma
*******************************************************************************/
// -----------------------------------------------------------------------------
void L3DChronometer::stop ()
{
if (state == TIMER_RUNNING || state == TIMER_PAUSED) {
if (state == TIMER_RUNNING)
updateTime ();
state = TIMER_STOPPED;
}
}
// -----------------------------------------------------------------------------
void L3DChronometer::pause ()
{
if (state == TIMER_RUNNING) {
updateTime ();
state = TIMER_PAUSED;
}
}
/******************************************************************************
Implementacion de metodos para Windows
*******************************************************************************/
#ifdef DEVICE_X86_WIN
// -----------------------------------------------------------------------------
L3DChronometer::L3DChronometer() {
tacum.QuadPart = 0;
QueryPerformanceFrequency(&freq);
state = TIMER_STOPPED;
}
// -----------------------------------------------------------------------------
float L3DChronometer::timeSecs () {
return (float) tacum.QuadPart / freq.QuadPart;
}
// -----------------------------------------------------------------------------
float L3DChronometer::timeMSecs () {
return (float) tacum.QuadPart / ((float)freq.QuadPart / 1000);
}
// -----------------------------------------------------------------------------
float L3DChronometer::timeUSecs () {
return (float) tacum.QuadPart / ((float)freq.QuadPart / 1000000u);
}
// -----------------------------------------------------------------------------
void L3DChronometer::updateTime ()
{
LARGE_INTEGER tnow;
QueryPerformanceCounter(&tnow);
tacum.QuadPart += (tnow.QuadPart - tini.QuadPart);
tini.QuadPart = tnow.QuadPart;
}
// -----------------------------------------------------------------------------
void L3DChronometer::start ()
{
if (state == TIMER_STOPPED) {
tacum.QuadPart = 0;
QueryPerformanceCounter(&tini);
state = TIMER_RUNNING;
}
}
// -----------------------------------------------------------------------------
void L3DChronometer::resume ()
{
if (state == TIMER_PAUSED) {
QueryPerformanceCounter(&tini);
state = TIMER_RUNNING;
}
}
// -----------------------------------------------------------------------------
/******************************************************************************
Implementacion de metodos para sistemas operativos posix
*******************************************************************************/
#else
// -----------------------------------------------------------------------------
L3DChronometer::L3DChronometer() {
sec = 0;
usec = 0;
state = TIMER_STOPPED;
}
// -----------------------------------------------------------------------------
float L3DChronometer::timeSecs () {
return (float) sec + (float) usec / 1000000.0f;
}
// -----------------------------------------------------------------------------
float L3DChronometer::timeMSecs () {
return (float) sec * 1000.0f + (float) usec / 1000.0f;
}
// -----------------------------------------------------------------------------
float L3DChronometer::timeUSecs () {
return (float) sec * 1000000.0f + (float) usec;
}
// -----------------------------------------------------------------------------
void L3DChronometer::updateTime ()
{
timeval tnow;
gettimeofday (&tnow, 0);
sec += tnow.tv_sec - tini.tv_sec;
usec += tnow.tv_usec - tini.tv_usec;
if (usec < 0) {
sec --;
usec += 1000000u;
}
}
// -----------------------------------------------------------------------------
void L3DChronometer::start ()
{
if (state == TIMER_STOPPED) {
sec = 0; usec = 0;
gettimeofday (&tini, 0);
state = TIMER_RUNNING;
}
}
// -----------------------------------------------------------------------------
void L3DChronometer::resume ()
{
if (state == TIMER_PAUSED) {
gettimeofday (&tini, 0);
state = TIMER_RUNNING;
}
}
// -----------------------------------------------------------------------------
#endif

View File

@ -0,0 +1,76 @@
//*******************************************************************
// L3DChronometer.h -- Header for L3DChronometer.cpp
// Copyright (c) 2010 Jose Maria Noguera
// Aug 10, 2010
//
// Jose Maria Noguera Rozua http://wwwdi.ujaen.es/~jnoguera/
//
//*******************************************************************
#ifndef _L3D_CHRONOMETER_H_
#define _L3D_CHRONOMETER_H_
#include "../../L3DPlatform.h"
#ifdef DEVICE_X86_WIN
#include <windows.h>
#else
#include <sys/time.h>
#endif
#define TIMER_STOPPED 0
#define TIMER_RUNNING 1
#define TIMER_PAUSED 2
/** @brief Chronometer used to measure performance of algoritms */
class L3DChronometer{
public:
/** Initializes the Chronometer */
L3DChronometer();
/** Starts to measure time */
void start ();
/** Pause without losing the measured time */
void pause ();
/** Resume a previously paused Chronometer */
void resume ();
/** Stops the Chronometer */
void stop ();
float elapsed() { stop(); float r = timeMSecs(); resume(); return r }
/** Gets time in seconds. The Chronometer must be stop */
float timeSecs ();
/** Gets time in seconds. The Chronometer must be stop */
float timeMSecs ();
/** Gets time in seconds. The Chronometer must be stop*/
float timeUSecs ();
private:
#ifdef DEVICE_X86_WIN
/* Initial time */
LARGE_INTEGER tini;
/* Seconds accumulator */
LARGE_INTEGER tacum;
/* Frequency */
LARGE_INTEGER freq;
#else
/** Initial time */
timeval tini;
/* Seconds accumulator */
unsigned sec;
/* MicroSeconds accumulator */
long usec;
#endif
/* Chronometer state: stopped, running, paused */
int state;
/* Updates time accumulators */
void updateTime ();
};
#endif

View File

@ -0,0 +1,77 @@
#include "L3DTimer.h"
#ifdef DEVICE_X86_WIN
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#include <time.h>
L3DTimer::L3DTimer()
{/*
base = 0;
initialized = false;
basetime = 0;*/
}
//---------------------------------------------------------------------------
int L3DTimer::GetCurrentSystemTime(void)
{
int iReturnValue = -1;
#ifdef DEVICE_X86_WIN
iReturnValue = Win_GetCurrentSystemTime();
#else
iReturnValue = Linux_GetCurrentSystemTime();
#endif
return iReturnValue;
}
//---------------------------------------------------------------------------
#ifdef DEVICE_X86_WIN
int L3DTimer::Win_GetCurrentSystemTime(void)
{
int curtime;
static int base;
static bool initialized = false;
if(!initialized)
{
base = timeGetTime() & 0xffff0000;
initialized = true;
}
curtime = timeGetTime() - base;
return curtime;
}
//---------------------------------------------------------------------------
#else
//---------------------------------------------------------------------------
int L3DTimer::Linux_GetCurrentSystemTime(void)
{
struct timeval tp;
struct timezone tzp;
static int basetime;
gettimeofday(&tp, &tzp);
if(!basetime)
{
basetime = tp.tv_sec;
return tp.tv_usec / 1000;
}
return (tp.tv_sec - basetime) * 1000 + tp.tv_usec / 1000;
}
#endif
//---------------------------------------------------------------------------

43
wrap/system/time/clock.h Normal file
View File

@ -0,0 +1,43 @@
//*******************************************************************
// L3DTimer.h -- Header for L3DTimer.cpp
// Copyright (c) 2010 Jose Maria Noguera
// Aug 10, 2010
//
// Jose Maria Noguera Rozua http://wwwdi.ujaen.es/~jnoguera/
//
//*******************************************************************
#ifndef _L3D_TIMER_
#define _L3D_TIMER_
#include "../../L3DPlatform.h"
class L3DTimer
{
public:
L3DTimer();
~L3DTimer(){;}
/**
This function retrieves the system time, in milliseconds. The system time is the time elapsed since the first call
to this function.
*/
static int GetCurrentSystemTime(void);
private:
/*
static int base;
static bool initialized;
static int basetime;
*/
#ifdef DEVICE_X86_WIN
static int Win_GetCurrentSystemTime(void);
#else
static int Linux_GetCurrentSystemTime(void);
#endif
};
#endif