git-svn-id: svn://db.shs.com.ru/pip@370 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-04-17 08:52:24 +00:00
parent 9c59a27999
commit f76510e74a
21 changed files with 101 additions and 122 deletions

View File

@@ -17,12 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pimutex.h"
#ifdef WINDOWS
# include <windef.h>
# include <winbase.h>
#endif
/** \class PIMutex
* \brief Mutex
* \details
@@ -40,6 +34,25 @@
*
* */
#include "pimutex.h"
#ifdef WINDOWS
# include <windef.h>
# include <winbase.h>
#endif
#ifdef BLACKBERRY
# include <pthread.h>
#endif
PRIVATE_DEFINITION_START(PIMutex)
#ifdef WINDOWS
void *
#else
pthread_mutex_t
#endif
mutex;
PRIVATE_DEFINITION_END(PIMutex)
PIMutex::PIMutex(): inited_(false) {
init();
@@ -53,9 +66,9 @@ PIMutex::~PIMutex() {
void PIMutex::lock() {
#ifdef WINDOWS
WaitForSingleObject(mutex, INFINITE);
WaitForSingleObject(PRIVATE->mutex, INFINITE);
#else
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&(PRIVATE->mutex));
#endif
locked = true;
}
@@ -63,9 +76,9 @@ void PIMutex::lock() {
void PIMutex::unlock() {
#ifdef WINDOWS
ReleaseMutex(mutex);
ReleaseMutex(PRIVATE->mutex);
#else
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&(PRIVATE->mutex));
#endif
locked = false;
}
@@ -74,9 +87,9 @@ void PIMutex::unlock() {
bool PIMutex::tryLock() {
bool ret =
#ifdef WINDOWS
(WaitForSingleObject(mutex, 0) == WAIT_OBJECT_0);
(WaitForSingleObject(PRIVATE->mutex, 0) == WAIT_OBJECT_0);
#else
(pthread_mutex_trylock(&mutex) == 0);
(pthread_mutex_trylock(&(PRIVATE->mutex)) == 0);
#endif
locked = true;
return ret;
@@ -91,14 +104,14 @@ bool PIMutex::isLocked() const {
void PIMutex::init() {
if (inited_) destroy();
#ifdef WINDOWS
mutex = CreateMutex(0, false, 0);
PRIVATE->mutex = CreateMutex(0, false, 0);
#else
pthread_mutexattr_t attr;
memset(&attr, 0, sizeof(attr));
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
memset(&mutex, 0, sizeof(mutex));
pthread_mutex_init(&mutex, &attr);
memset(&(PRIVATE->mutex), 0, sizeof(PRIVATE->mutex));
pthread_mutex_init(&(PRIVATE->mutex), &attr);
pthread_mutexattr_destroy(&attr);
#endif
locked = false;
@@ -109,10 +122,10 @@ void PIMutex::init() {
void PIMutex::destroy() {
if (inited_) {
#ifdef WINDOWS
if (mutex) CloseHandle(mutex);
mutex = 0;
if (PRIVATE->mutex) CloseHandle(PRIVATE->mutex);
PRIVATE->mutex = 0;
#else
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&(PRIVATE->mutex));
#endif
}
locked = inited_ = false;

View File

@@ -24,9 +24,6 @@
#define PIMUTEX_H
#include "piinit.h"
#ifdef BLACKBERRY
# include <pthread.h>
#endif
class PIP_EXPORT PIMutex
{
@@ -62,13 +59,7 @@ private:
void init();
void destroy();
#ifdef WINDOWS
void *
#else
pthread_mutex_t
#endif
mutex;
PRIVATE_DECLARATION
bool inited_;
volatile bool locked;