1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/********************************************************************************
* *
* R e a d - W r i t e L o c k C l a s s *
* *
*********************************************************************************
* Copyright (C) 2004,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library 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 library 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 "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "FXReadWriteLock.h"
/*
Notes:
- Read/write lock variable.
*/
using namespace FX;
/*******************************************************************************/
namespace FX {
#if defined(WIN32)
struct RWLOCK {
CRITICAL_SECTION mutex[1];
CRITICAL_SECTION access[1];
DWORD readers;
};
#endif
// Initialize read/write lock
FXReadWriteLock::FXReadWriteLock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
// If this fails on your machine, determine what value
// of sizeof(RWLOCK) is supposed to be on your
// machine and mail it to: jeroen@fox-toolkit.net!!
//FXTRACE((150,"sizeof(SRWLOCK)=%d\n",sizeof(SRWLOCK)));
FXASSERT_STATIC(sizeof(data)>=sizeof(SRWLOCK));
InitializeSRWLock((SRWLOCK*)data);
#elif defined(WIN32)
// If this fails on your machine, determine what value
// of sizeof(RWLOCK) is supposed to be on your
// machine and mail it to: jeroen@fox-toolkit.net!!
//FXTRACE((150,"sizeof(RWLOCK)=%d\n",sizeof(RWLOCK)));
FXASSERT_STATIC(sizeof(data)>=sizeof(RWLOCK));
InitializeCriticalSection(((RWLOCK*)data)->mutex);
InitializeCriticalSection(((RWLOCK*)data)->access);
((RWLOCK*)data)->readers=0;
#elif (_XOPEN_SOURCE >= 500) || (_POSIX_C_SOURCE >= 200809L)
// If this fails on your machine, determine what value
// of sizeof(pthread_rwlock_t) is supposed to be on your
// machine and mail it to: jeroen@fox-toolkit.net!!
//FXTRACE((150,"sizeof(pthread_rwlock_t)=%d\n",sizeof(pthread_rwlock_t)));
FXASSERT_STATIC(sizeof(data)>=sizeof(pthread_rwlock_t));
pthread_rwlockattr_t rwlockatt;
pthread_rwlockattr_init(&rwlockatt);
pthread_rwlockattr_setkind_np(&rwlockatt,PTHREAD_RWLOCK_PREFER_WRITER_NP);
pthread_rwlock_init((pthread_rwlock_t*)data,&rwlockatt);
pthread_rwlockattr_destroy(&rwlockatt);
#else
// If this fails on your machine, determine what value
// of sizeof(pthread_rwlock_t) is supposed to be on your
// machine and mail it to: jeroen@fox-toolkit.net!!
//FXTRACE((150,"sizeof(pthread_rwlock_t)=%d\n",sizeof(pthread_rwlock_t)));
FXASSERT_STATIC(sizeof(data)>=sizeof(pthread_rwlock_t));
pthread_rwlock_init((pthread_rwlock_t*)data,nullptr);
#endif
}
// Acquire read lock for read/write lock
void FXReadWriteLock::readLock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
AcquireSRWLockShared((SRWLOCK*)data);
#elif defined(WIN32)
EnterCriticalSection(((RWLOCK*)data)->mutex);
if(++((RWLOCK*)data)->readers==1){
EnterCriticalSection(((RWLOCK*)data)->access);
}
LeaveCriticalSection(((RWLOCK*)data)->mutex);
#else
pthread_rwlock_rdlock((pthread_rwlock_t*)data);
#endif
}
// Try to acquire read lock for read/write lock
FXbool FXReadWriteLock::tryReadLock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
return TryAcquireSRWLockShared((SRWLOCK*)data)!=0;
#elif defined(WIN32)
if(TryEnterCriticalSection(((RWLOCK*)data)->mutex)){
if(++((RWLOCK*)data)->readers==1 && !TryEnterCriticalSection(((RWLOCK*)data)->access)){
--((RWLOCK*)data)->readers;
LeaveCriticalSection(((RWLOCK*)data)->mutex);
return false;
}
LeaveCriticalSection(((RWLOCK*)data)->mutex);
return true;
}
return false;
#else
return pthread_rwlock_tryrdlock((pthread_rwlock_t*)data)==0;
#endif
}
// Unlock read lock
void FXReadWriteLock::readUnlock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
ReleaseSRWLockShared((SRWLOCK*)data);
#elif defined(WIN32)
EnterCriticalSection(((RWLOCK*)data)->mutex);
if(--((RWLOCK*)data)->readers==0){
LeaveCriticalSection(((RWLOCK*)data)->access);
}
LeaveCriticalSection(((RWLOCK*)data)->mutex);
#else
pthread_rwlock_unlock((pthread_rwlock_t*)data);
#endif
}
// Test if read locked
FXbool FXReadWriteLock::readLocked(){
if(tryReadLock()){
readUnlock();
return false;
}
return true;
}
// Acquire write lock for read/write lock
void FXReadWriteLock::writeLock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
AcquireSRWLockExclusive((SRWLOCK*)data);
#elif defined(WIN32)
EnterCriticalSection(((RWLOCK*)data)->access);
#else
pthread_rwlock_wrlock((pthread_rwlock_t*)data);
#endif
}
// Try to acquire write lock for read/write lock
FXbool FXReadWriteLock::tryWriteLock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
return TryAcquireSRWLockExclusive((SRWLOCK*)data)!=0;
#elif defined(WIN32) && (_WIN32_WINNT >= 0x0400)
return TryEnterCriticalSection(((RWLOCK*)data)->access)!=0;
#elif defined(WIN32)
return false;
#else
return pthread_rwlock_trywrlock((pthread_rwlock_t*)data)==0;
#endif
}
// Unlock write lock
void FXReadWriteLock::writeUnlock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
ReleaseSRWLockExclusive((SRWLOCK*)data);
#elif defined(WIN32)
LeaveCriticalSection(((RWLOCK*)data)->access);
#else
pthread_rwlock_unlock((pthread_rwlock_t*)data);
#endif
}
// Test if write locked
FXbool FXReadWriteLock::writeLocked(){
if(tryWriteLock()){
writeUnlock();
return false;
}
return true;
}
// Delete read/write lock
FXReadWriteLock::~FXReadWriteLock(){
#if defined(WIN32) && (_WIN32_WINNT >= 0x0600) // Vista or newer
// NOP //
#elif defined(WIN32)
DeleteCriticalSection(((RWLOCK*)data)->mutex);
DeleteCriticalSection(((RWLOCK*)data)->access);
#else
pthread_rwlock_destroy((pthread_rwlock_t*)data);
#endif
}
}
|