109 lines
2.4 KiB
C++
109 lines
2.4 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
|
|
Stephan Fomenko
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "piconditionlock.h"
|
|
#ifdef WINDOWS
|
|
#include "synchapi.h"
|
|
#else
|
|
#include "pthread.h"
|
|
#endif
|
|
|
|
|
|
PRIVATE_DEFINITION_START(PIConditionLock)
|
|
#ifdef WINDOWS
|
|
CRITICAL_SECTION
|
|
#else
|
|
pthread_mutex_t
|
|
#endif
|
|
nativeHandle;
|
|
PRIVATE_DEFINITION_END(PIConditionLock)
|
|
|
|
|
|
#ifdef WINDOWS
|
|
PIConditionLock::PIConditionLock() {
|
|
InitializeCriticalSection(&PRIVATE->nativeHandle);
|
|
}
|
|
|
|
|
|
PIConditionLock::~PIConditionLock() {
|
|
DeleteCriticalSection(&PRIVATE->nativeHandle);
|
|
}
|
|
|
|
|
|
void PIConditionLock::lock() {
|
|
EnterCriticalSection(&PRIVATE->nativeHandle);
|
|
}
|
|
|
|
|
|
void PIConditionLock::unlock() {
|
|
LeaveCriticalSection(&PRIVATE->nativeHandle);
|
|
}
|
|
|
|
|
|
void *PIConditionLock::handle() {
|
|
return &PRIVATE->nativeHandle;
|
|
}
|
|
|
|
|
|
bool PIConditionLock::tryLock() {
|
|
return TryEnterCriticalSection(&PRIVATE->nativeHandle) != 0;
|
|
}
|
|
#else
|
|
|
|
|
|
PIConditionLock::PIConditionLock() {
|
|
pthread_mutexattr_t attr;
|
|
memset(&attr, 0, sizeof(attr));
|
|
pthread_mutexattr_init(&attr);
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
memset(&(PRIVATE->nativeHandle), 0, sizeof(PRIVATE->nativeHandle));
|
|
pthread_mutex_init(&(PRIVATE->nativeHandle), &attr);
|
|
pthread_mutexattr_destroy(&attr);
|
|
}
|
|
|
|
|
|
PIConditionLock::~PIConditionLock() {
|
|
pthread_mutex_destroy(&(PRIVATE->nativeHandle));
|
|
}
|
|
|
|
|
|
void PIConditionLock::lock() {
|
|
pthread_mutex_lock(&(PRIVATE->nativeHandle));
|
|
}
|
|
|
|
|
|
void PIConditionLock::unlock() {
|
|
pthread_mutex_unlock(&(PRIVATE->nativeHandle));
|
|
}
|
|
|
|
|
|
void *PIConditionLock::handle() {
|
|
return &PRIVATE->nativeHandle;
|
|
}
|
|
|
|
|
|
bool PIConditionLock::tryLock() {
|
|
return (pthread_mutex_trylock(&(PRIVATE->nativeHandle)) == 0);
|
|
}
|
|
#endif
|
|
|
|
|
|
|
|
|