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 #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 #else
@ -18,6 +31,9 @@ tyepdef QSemaphore mt::Semaphore;
#include "scoped_read_lock.h" #include "scoped_read_lock.h"
#include "scoped_write_lock.h" #include "scoped_write_lock.h"
namespace mt{
typedef scoped_mutex_lock mutexlocker;
}
#endif #endif
#endif // MT_MT_H #endif // MT_MT_H

View File

@ -8,26 +8,34 @@ namespace mt
class scoped_mutex_lock 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 scoped_mutex_lock this_type;
typedef void base_type; typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m) scoped_mutex_lock(mutex & m) : mtx(m)
{ {
this->mtx.lock(); this->mtx.lock();
} }
~scoped_mutex_lock(void) ~scoped_mutex_lock(void)
{ {
this->mtx.unlock(); 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 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 scoped_mutex_lock this_type;
typedef void base_type; typedef void base_type;
scoped_mutex_lock(mutex & m) : mtx(m) scoped_mutex_lock(mutex & m) : mtx(m)
{ {
this->mtx.lock(); this->mtx.lock();
} }
~scoped_mutex_lock(void) ~scoped_mutex_lock(void)
{ {
this->mtx.unlock(); this->mtx.unlock();
} }
protected: protected:
mutex & mtx; mutex & mtx;
}; };
} }

View File

@ -3,6 +3,7 @@
#include "base.h" #include "base.h"
#include <iostream>
#include <semaphore.h> #include <semaphore.h>
namespace mt namespace mt
@ -10,61 +11,84 @@ namespace mt
class semaphore class semaphore
{ {
MT_PREVENT_COPY(semaphore) MT_PREVENT_COPY(semaphore)
public: public:
typedef semaphore this_type; typedef semaphore this_type;
typedef void base_type; typedef void base_type;
semaphore(void) semaphore(void)
{ {
sem_init(&(this->s), 0, 0); sem_init(&(this->s), 0, 0);
} }
semaphore(int value) semaphore(int value)
{ {
sem_init(&(this->s), 0, value); sem_init(&(this->s), 0, value);
} }
~semaphore(void) ~semaphore(void)
{ {
sem_destroy(&(this->s)); sem_destroy(&(this->s));
} }
void post(void) void post(void)
{ {
sem_post(&(this->s)); sem_post(&(this->s));
} }
/* /*
void post(int n) void post(int n)
{ {
sem_post_multiple(&(this->s), n); sem_post_multiple(&(this->s), n);
} }
*/ */
void wait(void) void wait(void)
{ {
sem_wait(&(this->s)); sem_wait(&(this->s));
} }
bool trywait(void) bool trywait(void)
{ {
return (sem_trywait(&(this->s)) == 0); return (sem_trywait(&(this->s)) == 0);
} }
//jnoguera 11-Nov-2011 //methods added for conforming to the QT implementation
int available() //jnoguera 14-12-2011
{
int value;
sem_getvalue( &(this->s), &value );
return value;
}
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 class thread
{ {
MT_PREVENT_COPY(thread) MT_PREVENT_COPY(thread)
public: public:
typedef thread this_type; typedef thread this_type;
typedef void base_type; typedef void base_type;
thread(void) : flags(0) thread(void) : flags(0)
{ {
; ;
} }
virtual ~thread(void) virtual ~thread(void)
{ {
; ;
} }
virtual bool start(void) virtual bool start(void)
{ {
if ((this->flags & thread_started) != 0) return false; 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; sched_param sp;
memset(&sp, 0, sizeof(sched_param)); memset(&sp, 0, sizeof(sched_param));
sp.sched_priority = sched_get_priority_min(SCHED_OTHER); sp.sched_priority = sched_get_priority_min(SCHED_OTHER);
sp.sched_priority = 0; // normal sp.sched_priority = 0; // normal
pthread_setschedparam(this->tid, SCHED_OTHER, &sp); pthread_setschedparam(this->tid, SCHED_OTHER, &sp);
*/ */
this->flags |= thread_started; this->flags |= thread_started;
return true; return true;
} }
virtual bool wait(void) virtual bool wait(void)
{ {
if ((this->flags & thread_started) == 0) return false; if ((this->flags & thread_started) == 0) return false;
pthread_join(this->tid, 0); pthread_join(this->tid, 0);
this->flags &= ~thread_started; this->flags &= ~thread_started;
return true; return true;
} }
virtual bool kill(void) virtual bool kill(void)
{ {
if ((this->flags & thread_started) == 0) return false; if ((this->flags & thread_started) == 0) return false;
pthread_kill(this->tid, 0); pthread_kill(this->tid, 0);
this->flags &= ~(thread_started | thread_running); this->flags &= ~(thread_started | thread_running);
return true; return true;
} }
bool is_started(void) const bool is_started(void) const
{ {
return ((this->flags & thread_started) != 0); return ((this->flags & thread_started) != 0);
} }
bool is_running(void) const bool is_running(void) const
{ {
return ((this->flags & thread_running) != 0); return ((this->flags & thread_running) != 0);
} }
protected: protected:
virtual void run(void) virtual void run(void)
{ {
; ;
} }
private: private:
enum thread_flags enum thread_flags
{ {
thread_none = ( 0), thread_none = ( 0),
thread_started = (1 << 0), thread_started = (1 << 0),
thread_running = (1 << 1) thread_running = (1 << 1)
}; };
volatile unsigned int flags; volatile unsigned int flags;
pthread_t tid; pthread_t tid;
static void * thread_func(void * param) static void * thread_func(void * param)
{ {
this_type * p_this = reinterpret_cast<this_type *>(param); this_type * p_this = reinterpret_cast<this_type *>(param);
MT_ASSERT(p_this != 0); MT_ASSERT(p_this != 0);
p_this->flags |= thread_running; p_this->flags |= thread_running;
p_this->run(); p_this->run();
p_this->flags &= ~thread_running; p_this->flags &= ~thread_running;
return 0; return 0;
} }
}; };
} }