05.11.2011 - stable version, 0.1.0, self-test program, work at GCC 2.95 - 4.5, VC 2010, MinGW, Linux, Windows, QNX
This commit is contained in:
100
pimutex.h
100
pimutex.h
@@ -1,53 +1,53 @@
|
||||
#ifndef PIMUTEX_H
|
||||
#define PIMUTEX_H
|
||||
|
||||
#ifndef PIMUTEX_H
|
||||
#define PIMUTEX_H
|
||||
|
||||
#include "piincludes.h"
|
||||
#ifdef CC_GCC
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include "piincludes.h"
|
||||
|
||||
class 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);}
|
||||
#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);}
|
||||
#endif
|
||||
|
||||
private:
|
||||
#ifndef WINDOWS
|
||||
pthread_mutex_t mutex;
|
||||
#else
|
||||
void * mutex;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
class PIMutexLocker
|
||||
{
|
||||
public:
|
||||
PIMutexLocker(PIMutex * m): mutex(m) {mutex->lock();}
|
||||
PIMutexLocker(PIMutex & m): mutex(&m) {mutex->lock();}
|
||||
~PIMutexLocker() {mutex->unlock();}
|
||||
private:
|
||||
PIMutex * mutex;
|
||||
};
|
||||
|
||||
#endif // PIMUTEX_H
|
||||
|
||||
class 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);}
|
||||
#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);}
|
||||
#endif
|
||||
|
||||
private:
|
||||
#ifndef WINDOWS
|
||||
pthread_mutex_t mutex;
|
||||
#else
|
||||
void * mutex;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
class PIMutexLocker
|
||||
{
|
||||
public:
|
||||
PIMutexLocker(PIMutex * m): mutex(m) {mutex->lock();}
|
||||
PIMutexLocker(PIMutex & m): mutex(&m) {mutex->lock();}
|
||||
~PIMutexLocker() {mutex->unlock();}
|
||||
private:
|
||||
PIMutex * mutex;
|
||||
};
|
||||
|
||||
#endif // PIMUTEX_H
|
||||
|
||||
Reference in New Issue
Block a user