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
|
/********************************************************************************
* *
* 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/> *
********************************************************************************/
#ifndef FXREADWRITELOCK_H
#define FXREADWRITELOCK_H
namespace FX {
/**
* FXReadWriteLock allows multiple readers but only a single writer.
*
* FXReadWriteLock provides access to a shared region just like FXMutex,
* except that threads that just read from the shared variables are not
* excluding each other. When a thread tries to write, however, it can
* only proceed when no other writers or readers are present.
* Thus, data structures which are frequently inspected but rarely updated
* can be more effectively accessed with a read/write lock than with a mutex.
*/
class FXAPI FXReadWriteLock {
private:
FXuval data[32];
private:
FXReadWriteLock(const FXReadWriteLock&);
FXReadWriteLock &operator=(const FXReadWriteLock&);
public:
/// Initialize the read/write lock
FXReadWriteLock();
/// Acquire read lock for read/write lock
void readLock();
/// Try to acquire read lock for read/write lock
FXbool tryReadLock();
/// Unlock read lock
void readUnlock();
/// Test if read locked
FXbool readLocked();
/// Acquire write lock for read/write mutex
void writeLock();
/// Try to acquire write lock for read/write lock
FXbool tryWriteLock();
/// Unlock write mutex
void writeUnlock();
/// Test if write locked
FXbool writeLocked();
/// Delete the read/write lock
~FXReadWriteLock();
};
/// Scoped read lock
class FXAPI FXScopedReadLock {
private:
FXReadWriteLock& rwlock;
private:
FXScopedReadLock();
FXScopedReadLock(const FXScopedReadLock&);
FXScopedReadLock& operator=(const FXScopedReadLock&);
public:
/// Construct & lock associated read-write lock
FXScopedReadLock(FXReadWriteLock& rwl):rwlock(rwl){ lock(); }
/// Return reference to associated read-write lock
FXReadWriteLock& readwritelock(){ return rwlock; }
/// Lock read-write lock
void lock(){ rwlock.readLock(); }
/// Return true if succeeded locking the read-write lock
FXbool trylock(){ return rwlock.tryReadLock(); }
/// Return true if read-write lock is already locked
FXbool locked(){ return rwlock.readLocked(); }
/// Unlock mutex
void unlock(){ rwlock.readUnlock(); }
/// Destroy and unlock associated read-write lock
~FXScopedReadLock(){ unlock(); }
};
/// Scoped write lock
class FXAPI FXScopedWriteLock {
private:
FXReadWriteLock& rwlock;
private:
FXScopedWriteLock();
FXScopedWriteLock(const FXScopedWriteLock&);
FXScopedWriteLock& operator=(const FXScopedWriteLock&);
public:
/// Construct & lock associated read-write lock
FXScopedWriteLock(FXReadWriteLock& rwl):rwlock(rwl){ lock(); }
/// Return reference to associated read-write lock
FXReadWriteLock& readwritelock(){ return rwlock; }
/// Lock read-write lock
void lock(){ rwlock.writeLock(); }
/// Return true if succeeded locking the read-write lock
FXbool trylock(){ return rwlock.tryWriteLock(); }
/// Return true if read-write lock is already locked
FXbool locked(){ return rwlock.writeLocked(); }
/// Unlock read-write lock
void unlock(){ rwlock.writeUnlock(); }
/// Destroy and unlock associated read-write lock
~FXScopedWriteLock(){ unlock(); }
};
}
#endif
|