Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXReadWriteLock.h
1 /********************************************************************************
2 * *
3 * R e a d - W r i t e L o c k C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2004,2024 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXREADWRITELOCK_H
22 #define FXREADWRITELOCK_H
23 
24 namespace FX {
25 
26 
37 class FXAPI FXReadWriteLock {
38 private:
39  FXuval data[32];
40 private:
42  FXReadWriteLock &operator=(const FXReadWriteLock&);
43 public:
44 
47 
49  void readLock();
50 
52  FXbool tryReadLock();
53 
55  void readUnlock();
56 
58  FXbool readLocked();
59 
61  void writeLock();
62 
64  FXbool tryWriteLock();
65 
67  void writeUnlock();
68 
70  FXbool writeLocked();
71 
73  ~FXReadWriteLock();
74  };
75 
76 
78 class FXAPI FXScopedReadLock {
79 private:
80  FXReadWriteLock& rwlock;
81 private:
84  FXScopedReadLock& operator=(const FXScopedReadLock&);
85 public:
86 
88  FXScopedReadLock(FXReadWriteLock& rwl):rwlock(rwl){ lock(); }
89 
91  FXReadWriteLock& readwritelock(){ return rwlock; }
92 
94  void lock(){ rwlock.readLock(); }
95 
97  FXbool trylock(){ return rwlock.tryReadLock(); }
98 
100  FXbool locked(){ return rwlock.readLocked(); }
101 
103  void unlock(){ rwlock.readUnlock(); }
104 
106  ~FXScopedReadLock(){ unlock(); }
107  };
108 
109 
110 
112 class FXAPI FXScopedWriteLock {
113 private:
114  FXReadWriteLock& rwlock;
115 private:
118  FXScopedWriteLock& operator=(const FXScopedWriteLock&);
119 public:
120 
122  FXScopedWriteLock(FXReadWriteLock& rwl):rwlock(rwl){ lock(); }
123 
125  FXReadWriteLock& readwritelock(){ return rwlock; }
126 
128  void lock(){ rwlock.writeLock(); }
129 
131  FXbool trylock(){ return rwlock.tryWriteLock(); }
132 
134  FXbool locked(){ return rwlock.writeLocked(); }
135 
137  void unlock(){ rwlock.writeUnlock(); }
138 
140  ~FXScopedWriteLock(){ unlock(); }
141  };
142 
143 }
144 
145 #endif
146 
FXbool tryWriteLock()
Try to acquire write lock for read/write lock.
void lock()
Lock read-write lock.
Definition: FXReadWriteLock.h:128
FXbool trylock()
Return true if succeeded locking the read-write lock.
Definition: FXReadWriteLock.h:131
FXbool locked()
Return true if read-write lock is already locked.
Definition: FXReadWriteLock.h:134
void unlock()
Unlock mutex.
Definition: FXReadWriteLock.h:103
FXbool readLocked()
Test if read locked.
FXScopedWriteLock(FXReadWriteLock &rwl)
Construct & lock associated read-write lock.
Definition: FXReadWriteLock.h:122
FXbool locked()
Return true if read-write lock is already locked.
Definition: FXReadWriteLock.h:100
void lock()
Lock read-write lock.
Definition: FXReadWriteLock.h:94
FXbool tryReadLock()
Try to acquire read lock for read/write lock.
FXScopedReadLock(FXReadWriteLock &rwl)
Construct & lock associated read-write lock.
Definition: FXReadWriteLock.h:88
Scoped read lock.
Definition: FXReadWriteLock.h:78
Definition: FX4Splitter.h:28
Scoped write lock.
Definition: FXReadWriteLock.h:112
FXbool trylock()
Return true if succeeded locking the read-write lock.
Definition: FXReadWriteLock.h:97
~FXScopedReadLock()
Destroy and unlock associated read-write lock.
Definition: FXReadWriteLock.h:106
void readLock()
Acquire read lock for read/write lock.
void unlock()
Unlock read-write lock.
Definition: FXReadWriteLock.h:137
FXbool writeLocked()
Test if write locked.
void readUnlock()
Unlock read lock.
FXReadWriteLock & readwritelock()
Return reference to associated read-write lock.
Definition: FXReadWriteLock.h:91
FXReadWriteLock allows multiple readers but only a single writer.
Definition: FXReadWriteLock.h:37
~FXScopedWriteLock()
Destroy and unlock associated read-write lock.
Definition: FXReadWriteLock.h:140
void writeUnlock()
Unlock write mutex.
FXReadWriteLock & readwritelock()
Return reference to associated read-write lock.
Definition: FXReadWriteLock.h:125
void writeLock()
Acquire write lock for read/write mutex.

Copyright © 1997-2022 Jeroen van der Zijp