From 2aa09889899999e34f40cdd4dccab9a5afaf53ed Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Wed, 14 Dec 2011 17:39:38 +0000 Subject: [PATCH] Updated to mime the QThread & related classes interface --- wrap/system/multithreading/mt.h | 20 ++- .../system/multithreading/scoped_mutex_lock.h | 64 ++++---- wrap/system/multithreading/semaphore.h | 110 ++++++++------ wrap/system/multithreading/thread.h | 142 +++++++++--------- 4 files changed, 192 insertions(+), 144 deletions(-) diff --git a/wrap/system/multithreading/mt.h b/wrap/system/multithreading/mt.h index 5682c885..cb759aa9 100755 --- a/wrap/system/multithreading/mt.h +++ b/wrap/system/multithreading/mt.h @@ -3,9 +3,22 @@ #ifdef QT_CORE_LIB -#include -tyepdef QSemaphore mt::Semaphore; +#include +#include +#include + +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 diff --git a/wrap/system/multithreading/scoped_mutex_lock.h b/wrap/system/multithreading/scoped_mutex_lock.h index c6f4d311..174dab35 100755 --- a/wrap/system/multithreading/scoped_mutex_lock.h +++ b/wrap/system/multithreading/scoped_mutex_lock.h @@ -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; }; } diff --git a/wrap/system/multithreading/semaphore.h b/wrap/system/multithreading/semaphore.h index 5ef96fb7..bb8bb68f 100755 --- a/wrap/system/multithreading/semaphore.h +++ b/wrap/system/multithreading/semaphore.h @@ -3,6 +3,7 @@ #include "base.h" +#include #include 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; }; } diff --git a/wrap/system/multithreading/thread.h b/wrap/system/multithreading/thread.h index fdb610d4..11edd54b 100755 --- a/wrap/system/multithreading/thread.h +++ b/wrap/system/multithreading/thread.h @@ -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(this)); + pthread_create(&(this->tid), 0, this_type::thread_func, reinterpret_cast(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(param); - MT_ASSERT(p_this != 0); + static void * thread_func(void * param) + { + this_type * p_this = reinterpret_cast(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; + } }; }