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

FXMutex.h
1 /********************************************************************************
2 * *
3 * M u t e x 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 FXMUTEX_H
22 #define FXMUTEX_H
23 
24 namespace FX {
25 
26 
31 class FXAPI FXMutex {
32  friend class FXCondition;
33 private:
34  FXuval data[24];
35 private:
36  FXMutex(const FXMutex&);
37  FXMutex &operator=(const FXMutex&);
38 public:
39 
44  FXMutex(FXbool recursive=false);
45 
47  void lock();
48 
50  FXbool trylock();
51 
53  FXbool locked();
54 
56  void unlock();
57 
59  ~FXMutex();
60  };
61 
62 
71 class FXAPI FXScopedMutex {
72 private:
73  FXMutex& mtx;
74 private:
75  FXScopedMutex();
77  FXScopedMutex& operator=(const FXScopedMutex&);
78 public:
79 
81  FXScopedMutex(FXMutex& m):mtx(m){ lock(); }
82 
84  FXMutex& mutex(){ return mtx; }
85 
87  void lock(){ mtx.lock(); }
88 
90  FXbool trylock(){ return mtx.trylock(); }
91 
93  FXbool locked(){ return mtx.locked(); }
94 
96  void unlock(){ mtx.unlock(); }
97 
99  ~FXScopedMutex(){ unlock(); }
100  };
101 
102 
109 class FXAPI FXReverseMutex {
110 private:
111  FXMutex& mtx;
112 private:
113  FXReverseMutex();
115  FXReverseMutex& operator=(const FXReverseMutex&);
116 public:
117 
119  FXReverseMutex(FXMutex& m):mtx(m){ unlock(); }
120 
122  FXMutex& mutex(){ return mtx; }
123 
125  void lock(){ mtx.lock(); }
126 
128  FXbool trylock(){ return mtx.trylock(); }
129 
131  FXbool locked(){ return mtx.locked(); }
132 
134  void unlock(){ mtx.unlock(); }
135 
137  ~FXReverseMutex(){ lock(); }
138  };
139 
140 
141 }
142 
143 #endif
144 
void lock()
Lock mutex.
Definition: FXMutex.h:125
Establish a correspondence between a C++ scope and a FXMutex, so that entering and leaving the scope ...
Definition: FXMutex.h:71
FXbool trylock()
Return true if succeeded locking the mutex.
FXScopedMutex(FXMutex &m)
Construct and lock associated mutex.
Definition: FXMutex.h:81
void unlock()
Unlock mutex.
Definition: FXMutex.h:96
FXbool locked()
Return true if mutex is already locked.
Definition: FXMutex.h:93
FXMutex & mutex()
Return reference to associated mutex.
Definition: FXMutex.h:122
FXbool trylock()
Return true if succeeded locking the mutex.
Definition: FXMutex.h:128
A condition allows one or more threads to synchronize to an event.
Definition: FXCondition.h:35
~FXScopedMutex()
Destroy and unlock associated mutex.
Definition: FXMutex.h:99
The Reverse Mutex unlocks its associated FXMutex when entering the scope, and automatically relocks i...
Definition: FXMutex.h:109
void lock()
Lock the mutex.
void unlock()
Unlock mutex.
FXbool locked()
Return true if mutex is already locked.
FXReverseMutex(FXMutex &m)
Construct and unlock associated mutex.
Definition: FXMutex.h:119
FXbool trylock()
Return true if succeeded locking the mutex.
Definition: FXMutex.h:90
Definition: FX4Splitter.h:28
void lock()
Lock mutex.
Definition: FXMutex.h:87
void unlock()
Unlock mutex.
Definition: FXMutex.h:134
FXbool locked()
Return true if mutex is already locked.
Definition: FXMutex.h:131
FXMutex provides a mutex which can be used to enforce critical sections around updates of data shared...
Definition: FXMutex.h:31
FXMutex & mutex()
Return reference to associated mutex.
Definition: FXMutex.h:84
~FXReverseMutex()
Destroy and relock associated mutex.
Definition: FXMutex.h:137

Copyright © 1997-2022 Jeroen van der Zijp