Updated to mime the QThread & related classes interface

This commit is contained in:
Paolo Cignoni 2011-12-14 17:39:38 +00:00
parent ebc522f182
commit 2aa0988989
4 changed files with 192 additions and 144 deletions

View File

@ -3,9 +3,22 @@
#ifdef QT_CORE_LIB
#include <QSemaphore>
tyepdef QSemaphore mt::Semaphore;
#include <QThread.h>
#include <QMutex.h>
#include <QSemaphore.h>
namespace mt{
typedef QThread thread;
typedef QMutex mutex;
typedef QMutexLocker mutexlocker;
typedef QSemaphore semaphore;
//cache.h, token.h
//QAtomicInt
}//namespace
#else
@ -18,6 +31,9 @@ tyepdef QSemaphore mt::Semaphore;
#include "scoped_read_lock.h"
#include "scoped_write_lock.h"
namespace mt{
typedef scoped_mutex_lock mutexlocker;
}
#endif
#endif // MT_MT_H

View File

@ -8,26 +8,34 @@ namespace mt
class scoped_mutex_lock
{
MT_PREVENT_COPY(scoped_mutex_lock)
MT_PREVENT_COPY(scoped_mutex_lock)
public:
public:
typedef scoped_mutex_lock this_type;
typedef void base_type;
typedef scoped_mutex_lock this_type;
typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
protected:
//jnoguera 14-12-2011
//method added to mime QMutexLocker
scoped_mutex_lock(mutex * m) : mtx( *m )
{
this->mtx.lock();
}
mutex & mtx;
protected:
mutex & mtx;
};
}
@ -49,26 +57,26 @@ namespace mt
class scoped_mutex_lock
{
MT_PREVENT_COPY(scoped_mutex_lock)
MT_PREVENT_COPY(scoped_mutex_lock)
public:
public:
typedef scoped_mutex_lock this_type;
typedef void base_type;
typedef scoped_mutex_lock this_type;
typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
scoped_mutex_lock(mutex & m) : mtx(m)
{
this->mtx.lock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
~scoped_mutex_lock(void)
{
this->mtx.unlock();
}
protected:
protected:
mutex & mtx;
mutex & mtx;
};
}

View File

@ -3,6 +3,7 @@
#include "base.h"
#include <iostream>
#include <semaphore.h>
namespace mt
@ -10,61 +11,84 @@ namespace mt
class semaphore
{
MT_PREVENT_COPY(semaphore)
MT_PREVENT_COPY(semaphore)
public:
public:
typedef semaphore this_type;
typedef void base_type;
typedef semaphore this_type;
typedef void base_type;
semaphore(void)
{
sem_init(&(this->s), 0, 0);
}
semaphore(void)
{
sem_init(&(this->s), 0, 0);
}
semaphore(int value)
{
sem_init(&(this->s), 0, value);
}
semaphore(int value)
{
sem_init(&(this->s), 0, value);
}
~semaphore(void)
{
sem_destroy(&(this->s));
}
~semaphore(void)
{
sem_destroy(&(this->s));
}
void post(void)
{
sem_post(&(this->s));
}
void post(void)
{
sem_post(&(this->s));
}
/*
void post(int n)
{
sem_post_multiple(&(this->s), n);
}
*/
/*
void post(int n)
{
sem_post_multiple(&(this->s), n);
}
*/
void wait(void)
{
sem_wait(&(this->s));
}
void wait(void)
{
sem_wait(&(this->s));
}
bool trywait(void)
{
return (sem_trywait(&(this->s)) == 0);
}
bool trywait(void)
{
return (sem_trywait(&(this->s)) == 0);
}
//jnoguera 11-Nov-2011
int available()
{
int value;
sem_getvalue( &(this->s), &value );
return value;
}
//methods added for conforming to the QT implementation
//jnoguera 14-12-2011
private:
void release(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.release() not supported\n";
sem_post(&(this->s));
}
sem_t s;
void acquire(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
sem_wait(&(this->s));
}
bool tryAcquire(int n=1)
{
if(n != 1)
std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
return (sem_trywait(&(this->s)) == 0);
}
int available()
{
int value;
sem_getvalue( &(this->s), &value );
return value;
}
private:
sem_t s;
};
}

View File

@ -12,98 +12,98 @@ namespace mt
class thread
{
MT_PREVENT_COPY(thread)
MT_PREVENT_COPY(thread)
public:
public:
typedef thread this_type;
typedef void base_type;
typedef thread this_type;
typedef void base_type;
thread(void) : flags(0)
{
;
}
thread(void) : flags(0)
{
;
}
virtual ~thread(void)
{
;
}
virtual ~thread(void)
{
;
}
virtual bool start(void)
{
if ((this->flags & thread_started) != 0) return false;
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));
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);
*/
/*
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;
this->flags |= thread_started;
return true;
}
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 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;
}
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_started(void) const
{
return ((this->flags & thread_started) != 0);
}
bool is_running(void) const
{
return ((this->flags & thread_running) != 0);
}
bool is_running(void) const
{
return ((this->flags & thread_running) != 0);
}
protected:
protected:
virtual void run(void)
{
;
}
virtual void run(void)
{
;
}
private:
private:
enum thread_flags
{
thread_none = ( 0),
thread_started = (1 << 0),
thread_running = (1 << 1)
};
enum thread_flags
{
thread_none = ( 0),
thread_started = (1 << 0),
thread_running = (1 << 1)
};
volatile unsigned int flags;
pthread_t tid;
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);
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;
p_this->flags |= thread_running;
p_this->run();
p_this->flags &= ~thread_running;
return 0;
}
return 0;
}
};
}