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,2017 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 

Copyright © 1997-2016 Jeroen van der Zijp