00001 00002 #ifndef _JOT_STD_THREAD_MUTEX_H 00003 #define _JOT_STD_THREAD_MUTEX_H 00004 // This file should contain generalized thread support, but for now 00005 // it's just a pthread implmentation of a thread syncronization primitive. 00006 00007 #ifdef USE_PTHREAD 00008 #include <pthread.h> 00009 class ThreadMutex { 00010 public: 00011 ThreadMutex() { pthread_mutex_init(&_mtx, 0); } 00012 ~ThreadMutex() { pthread_mutex_destroy(&_mtx); } 00013 void lock() { pthread_mutex_lock(&_mtx); } 00014 void unlock() { pthread_mutex_unlock(&_mtx); } 00015 protected: 00016 pthread_mutex_t _mtx; 00017 }; 00018 #else 00019 class ThreadMutex { 00020 public: 00021 ThreadMutex() { } 00022 ~ThreadMutex() { } 00023 void lock() { } 00024 void unlock() { } 00025 }; 00026 #endif 00027 00028 class CriticalSection { 00029 public: 00030 CriticalSection(ThreadMutex *mutex) : _mutex(mutex) { _mutex->lock(); } 00031 ~CriticalSection() { _mutex->unlock(); } 00032 protected: 00033 ThreadMutex *_mutex; 00034 }; 00035 #endif