30.11.2013 - New PICollection namespace, Android support, my own PIVector implementation
This commit is contained in:
65
pimutex.h
65
pimutex.h
@@ -24,41 +24,58 @@
|
||||
#define PIMUTEX_H
|
||||
|
||||
#include "piincludes.h"
|
||||
#ifndef WINDOWS
|
||||
# include <pthread.h>
|
||||
#endif
|
||||
|
||||
class PIP_EXPORT PIMutex
|
||||
{
|
||||
public:
|
||||
#ifndef WINDOWS
|
||||
PIMutex() {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
//pthread_mutexattr_settype(&attr, PTHREAD_PROCESS_SHARED);
|
||||
pthread_mutex_init(&mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
}
|
||||
~PIMutex() {pthread_mutex_destroy(&mutex);}
|
||||
|
||||
void lock() {pthread_mutex_lock(&mutex);}
|
||||
void unlock() {pthread_mutex_unlock(&mutex);}
|
||||
bool tryLock() {return (pthread_mutex_trylock(&mutex) == 0);}
|
||||
//! Constructs unlocked mutex
|
||||
PIMutex();
|
||||
|
||||
~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() {
|
||||
#ifdef WINDOWS
|
||||
WaitForSingleObject(mutex, INFINITE);
|
||||
#else
|
||||
PIMutex() {mutex = CreateMutex(0, false, 0);}
|
||||
~PIMutex() {CloseHandle(mutex);}
|
||||
|
||||
void lock() {WaitForSingleObject(mutex, INFINITE);}
|
||||
void unlock() {ReleaseMutex(mutex);}
|
||||
bool tryLock() {return (WaitForSingleObject(mutex, 0) == WAIT_OBJECT_0);}
|
||||
pthread_mutex_lock(&mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
//! \brief Unlock mutex
|
||||
//! \details In any case this function returns immediate
|
||||
void unlock() {
|
||||
#ifdef WINDOWS
|
||||
ReleaseMutex(mutex);
|
||||
#else
|
||||
pthread_mutex_unlock(&mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
//! \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() {
|
||||
#ifdef WINDOWS
|
||||
return (WaitForSingleObject(mutex, 0) == WAIT_OBJECT_0);
|
||||
#else
|
||||
return (pthread_mutex_trylock(&mutex) == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
#ifndef WINDOWS
|
||||
pthread_mutex_t mutex;
|
||||
#ifdef WINDOWS
|
||||
void *
|
||||
#else
|
||||
void * mutex;
|
||||
pthread_mutex_t
|
||||
#endif
|
||||
mutex;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user