PIMutex as std::mutex
This commit is contained in:
@@ -34,110 +34,100 @@
|
||||
*
|
||||
* */
|
||||
|
||||
#include "pimutex.h"
|
||||
#include "piincludes_p.h"
|
||||
#ifdef BLACKBERRY
|
||||
# include <pthread.h>
|
||||
#endif
|
||||
//#include "pimutex.h"
|
||||
//#include "piincludes_p.h"
|
||||
//#ifdef BLACKBERRY
|
||||
//# include <pthread.h>
|
||||
//#endif
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PIMutex)
|
||||
#ifdef WINDOWS
|
||||
HANDLE
|
||||
#else
|
||||
pthread_mutex_t
|
||||
#endif
|
||||
mutex;
|
||||
PRIVATE_DEFINITION_END(PIMutex)
|
||||
//PRIVATE_DEFINITION_START(PIMutex)
|
||||
//#ifdef WINDOWS
|
||||
// HANDLE
|
||||
//#else
|
||||
// pthread_mutex_t
|
||||
//#endif
|
||||
// mutex;
|
||||
//PRIVATE_DEFINITION_END(PIMutex)
|
||||
|
||||
|
||||
PIMutex::PIMutex(): inited_(false) {
|
||||
//printf("new Mutex %p\n", this);
|
||||
#ifdef WINDOWS
|
||||
PRIVATE->mutex = 0;
|
||||
#endif
|
||||
init();
|
||||
}
|
||||
//PIMutex::PIMutex(): inited_(false) {
|
||||
// //printf("new Mutex %p\n", this);
|
||||
//#ifdef WINDOWS
|
||||
// PRIVATE->mutex = 0;
|
||||
//#endif
|
||||
// init();
|
||||
//}
|
||||
|
||||
|
||||
PIMutex::~PIMutex() {
|
||||
//printf("del Mutex %p\n", this);
|
||||
destroy();
|
||||
}
|
||||
//PIMutex::~PIMutex() {
|
||||
// //printf("del Mutex %p\n", this);
|
||||
// destroy();
|
||||
//}
|
||||
|
||||
|
||||
void PIMutex::lock() {
|
||||
#ifdef WINDOWS
|
||||
// std::cout << (ullong)PRIVATE->mutex << "locking..." << std::endl;
|
||||
// DWORD wr =
|
||||
WaitForSingleObject(PRIVATE->mutex, INFINITE);
|
||||
// std::cout << (ullong)PRIVATE->mutex << " lock wr=" << wr << std::endl;
|
||||
#else
|
||||
pthread_mutex_lock(&(PRIVATE->mutex));
|
||||
#endif
|
||||
locked = true;
|
||||
}
|
||||
//void PIMutex::lock() {
|
||||
//#ifdef WINDOWS
|
||||
//// std::cout << (ullong)PRIVATE->mutex << "locking..." << std::endl;
|
||||
//// DWORD wr =
|
||||
// WaitForSingleObject(PRIVATE->mutex, INFINITE);
|
||||
//// std::cout << (ullong)PRIVATE->mutex << " lock wr=" << wr << std::endl;
|
||||
//#else
|
||||
// pthread_mutex_lock(&(PRIVATE->mutex));
|
||||
//#endif
|
||||
//}
|
||||
|
||||
|
||||
void PIMutex::unlock() {
|
||||
#ifdef WINDOWS
|
||||
// BOOL wr =
|
||||
// ReleaseMutex(PRIVATE->mutex);
|
||||
SetEvent(PRIVATE->mutex);
|
||||
// std::cout << (ullong)PRIVATE->mutex << " unlock wr=" << wr << std::endl;
|
||||
#else
|
||||
pthread_mutex_unlock(&(PRIVATE->mutex));
|
||||
#endif
|
||||
locked = false;
|
||||
}
|
||||
//void PIMutex::unlock() {
|
||||
//#ifdef WINDOWS
|
||||
//// BOOL wr =
|
||||
//// ReleaseMutex(PRIVATE->mutex);
|
||||
// SetEvent(PRIVATE->mutex);
|
||||
//// std::cout << (ullong)PRIVATE->mutex << " unlock wr=" << wr << std::endl;
|
||||
//#else
|
||||
// pthread_mutex_unlock(&(PRIVATE->mutex));
|
||||
//#endif
|
||||
//}
|
||||
|
||||
|
||||
bool PIMutex::tryLock() {
|
||||
bool ret =
|
||||
#ifdef WINDOWS
|
||||
(WaitForSingleObject(PRIVATE->mutex, 0) == WAIT_OBJECT_0);
|
||||
#else
|
||||
(pthread_mutex_trylock(&(PRIVATE->mutex)) == 0);
|
||||
#endif
|
||||
locked = true;
|
||||
return ret;
|
||||
}
|
||||
//bool PIMutex::tryLock() {
|
||||
// bool ret =
|
||||
//#ifdef WINDOWS
|
||||
// (WaitForSingleObject(PRIVATE->mutex, 0) == WAIT_OBJECT_0);
|
||||
//#else
|
||||
// (pthread_mutex_trylock(&(PRIVATE->mutex)) == 0);
|
||||
//#endif
|
||||
// return ret;
|
||||
//}
|
||||
|
||||
|
||||
bool PIMutex::isLocked() const {
|
||||
// std::cout << "test " << (ullong)PRIVATE->mutex << std::endl;
|
||||
return locked;
|
||||
}
|
||||
//void PIMutex::init() {
|
||||
// if (inited_) destroy();
|
||||
//#ifdef WINDOWS
|
||||
// PRIVATE->mutex = CreateEvent(NULL, FALSE, TRUE, NULL);
|
||||
//// std::cout << "create " << (ullong)PRIVATE->mutex << std::endl;
|
||||
//#else
|
||||
// pthread_mutexattr_t attr;
|
||||
// memset(&attr, 0, sizeof(attr));
|
||||
// pthread_mutexattr_init(&attr);
|
||||
// pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
// memset(&(PRIVATE->mutex), 0, sizeof(PRIVATE->mutex));
|
||||
// pthread_mutex_init(&(PRIVATE->mutex), &attr);
|
||||
// pthread_mutexattr_destroy(&attr);
|
||||
//#endif
|
||||
// inited_ = true;
|
||||
//}
|
||||
|
||||
|
||||
void PIMutex::init() {
|
||||
if (inited_) destroy();
|
||||
#ifdef WINDOWS
|
||||
PRIVATE->mutex = CreateEvent(NULL, FALSE, TRUE, NULL);
|
||||
// std::cout << "create " << (ullong)PRIVATE->mutex << std::endl;
|
||||
#else
|
||||
pthread_mutexattr_t attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
memset(&(PRIVATE->mutex), 0, sizeof(PRIVATE->mutex));
|
||||
pthread_mutex_init(&(PRIVATE->mutex), &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#endif
|
||||
locked = false;
|
||||
inited_ = true;
|
||||
}
|
||||
|
||||
|
||||
void PIMutex::destroy() {
|
||||
if (inited_) {
|
||||
#ifdef WINDOWS
|
||||
// std::cout << "destroy " << (ullong)PRIVATE->mutex << std::endl;
|
||||
if (PRIVATE->mutex) CloseHandle(PRIVATE->mutex);
|
||||
PRIVATE->mutex = 0;
|
||||
#else
|
||||
pthread_mutex_destroy(&(PRIVATE->mutex));
|
||||
#endif
|
||||
}
|
||||
locked = inited_ = false;
|
||||
}
|
||||
//void PIMutex::destroy() {
|
||||
// if (inited_) {
|
||||
//#ifdef WINDOWS
|
||||
//// std::cout << "destroy " << (ullong)PRIVATE->mutex << std::endl;
|
||||
// if (PRIVATE->mutex) CloseHandle(PRIVATE->mutex);
|
||||
// PRIVATE->mutex = 0;
|
||||
//#else
|
||||
// pthread_mutex_destroy(&(PRIVATE->mutex));
|
||||
//#endif
|
||||
// }
|
||||
// inited_ = false;
|
||||
//}
|
||||
|
||||
@@ -25,42 +25,42 @@
|
||||
|
||||
#include "piinit.h"
|
||||
|
||||
class PIP_EXPORT PIMutex
|
||||
{
|
||||
public:
|
||||
//! Constructs unlocked mutex
|
||||
explicit PIMutex();
|
||||
|
||||
//class PIP_EXPORT PIMutex
|
||||
//{
|
||||
//public:
|
||||
// //! Constructs unlocked mutex
|
||||
// explicit PIMutex();
|
||||
|
||||
//! Destroy mutex
|
||||
~PIMutex();
|
||||
// //! Destroy mutex
|
||||
// ~PIMutex();
|
||||
|
||||
|
||||
//! \brief Lock mutex
|
||||
//! \details If mutex is unlocked it set to locked state and returns immediate.
|
||||
//! If mutex is already locked function blocks until mutex will be unlocked
|
||||
void lock();
|
||||
// //! \brief Lock mutex
|
||||
// //! \details If mutex is unlocked it set to locked state and returns immediate.
|
||||
// //! If mutex is already locked function blocks until mutex will be unlocked
|
||||
// void lock();
|
||||
|
||||
//! \brief Unlock mutex
|
||||
//! \details In any case this function returns immediate
|
||||
void unlock();
|
||||
// //! \brief Unlock mutex
|
||||
// //! \details In any case this function returns immediate
|
||||
// void unlock();
|
||||
|
||||
//! \brief Try to lock mutex
|
||||
//! \details If mutex is unlocked it set to locked state and returns "true" immediate.
|
||||
//! If mutex is already locked function returns immediate an returns "false"
|
||||
bool tryLock();
|
||||
// //! \brief Try to lock mutex
|
||||
// //! \details If mutex is unlocked it set to locked state and returns "true" immediate.
|
||||
// //! If mutex is already locked function returns immediate an returns "false"
|
||||
// bool tryLock();
|
||||
|
||||
//! Returns if mutex is locked
|
||||
bool isLocked() const;
|
||||
// //! Returns if mutex is locked
|
||||
// bool isLocked() const;
|
||||
|
||||
private:
|
||||
NO_COPY_CLASS(PIMutex)
|
||||
//private:
|
||||
// NO_COPY_CLASS(PIMutex)
|
||||
|
||||
void init();
|
||||
void destroy();
|
||||
bool inited_;
|
||||
bool locked;
|
||||
PRIVATE_DECLARATION
|
||||
};
|
||||
// void init();
|
||||
// void destroy();
|
||||
// bool inited_;
|
||||
// PRIVATE_DECLARATION
|
||||
//};
|
||||
|
||||
|
||||
class PIP_EXPORT PIMutexLocker
|
||||
@@ -71,7 +71,7 @@ public:
|
||||
~PIMutexLocker() {if (cond) mutex->unlock();}
|
||||
private:
|
||||
PIMutex * mutex;
|
||||
volatile bool cond;
|
||||
std::atomic_bool cond;
|
||||
};
|
||||
|
||||
#endif // PIMUTEX_H
|
||||
|
||||
Reference in New Issue
Block a user