/* 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 . */ #include "piconditionlock.h" #ifdef WINDOWS # include #else # include #endif PRIVATE_DEFINITION_START(PIConditionLock) #ifdef WINDOWS CRITICAL_SECTION #else pthread_mutex_t #endif nativeHandle; PRIVATE_DEFINITION_END(PIConditionLock) PIConditionLock::PIConditionLock() { #ifdef WINDOWS InitializeCriticalSection(&PRIVATE->nativeHandle); #else 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); #endif } PIConditionLock::~PIConditionLock() { #ifdef WINDOWS DeleteCriticalSection(&PRIVATE->nativeHandle); #else pthread_mutex_destroy(&(PRIVATE->nativeHandle)); #endif } void PIConditionLock::lock() { #ifdef WINDOWS EnterCriticalSection(&PRIVATE->nativeHandle); #else pthread_mutex_lock(&(PRIVATE->nativeHandle)); #endif } void PIConditionLock::unlock() { #ifdef WINDOWS LeaveCriticalSection(&PRIVATE->nativeHandle); #else pthread_mutex_unlock(&(PRIVATE->nativeHandle)); #endif } void *PIConditionLock::handle() { #ifdef WINDOWS return &PRIVATE->nativeHandle; #else return &PRIVATE->nativeHandle; #endif } bool PIConditionLock::tryLock() { #ifdef WINDOWS return TryEnterCriticalSection(&PRIVATE->nativeHandle) != 0; #else return (pthread_mutex_trylock(&(PRIVATE->nativeHandle)) == 0); #endif }