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

FXSpinLock.h
1 /********************************************************************************
2 * *
3 * S p i n l o c k C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2004,2022 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 FXSPINLOCK_H
22 #define FXSPINLOCK_H
23 
24 namespace FX {
25 
26 
36 class FXAPI FXSpinLock {
37 private:
38  volatile FXuval data[4];
39 private:
40  FXSpinLock(const FXSpinLock&);
41  FXSpinLock &operator=(const FXSpinLock&);
42 public:
43 
45  FXSpinLock();
46 
48  void lock();
49 
51  FXbool trylock();
52 
54  FXbool locked();
55 
57  void unlock();
58 
60  ~FXSpinLock();
61  };
62 
63 
72 class FXAPI FXScopedSpinLock {
73 private:
74  FXSpinLock& spn;
75 private:
78  FXScopedSpinLock& operator=(const FXScopedSpinLock&);
79 public:
80 
82  FXScopedSpinLock(FXSpinLock& s):spn(s){ lock(); }
83 
85  FXSpinLock& spinlock(){ return spn; }
86 
88  void lock(){ spn.lock(); }
89 
91  FXbool trylock(){ return spn.trylock(); }
92 
94  FXbool locked(){ return spn.locked(); }
95 
97  void unlock(){ spn.unlock(); }
98 
100  ~FXScopedSpinLock(){ unlock(); }
101  };
102 
103 
110 class FXAPI FXReverseSpinLock {
111 private:
112  FXSpinLock& spn;
113 private:
116  FXReverseSpinLock& operator=(const FXReverseSpinLock&);
117 public:
118 
120  FXReverseSpinLock(FXSpinLock& s):spn(s){ unlock(); }
121 
123  FXSpinLock& spinlock(){ return spn; }
124 
126  void lock(){ spn.lock(); }
127 
129  FXbool trylock(){ return spn.trylock(); }
130 
132  FXbool locked(){ return spn.locked(); }
133 
135  void unlock(){ spn.unlock(); }
136 
138  ~FXReverseSpinLock(){ lock(); }
139  };
140 
141 }
142 
143 #endif
144 
FXReverseSpinLock(FXSpinLock &s)
Construct and unlock associated spin lock.
Definition: FXSpinLock.h:120
void lock()
Lock spinlock.
Definition: FXSpinLock.h:88
FXbool locked()
Return true if spinlock is already locked.
FXbool trylock()
Return true if succeeded locking the spinlock.
Definition: FXSpinLock.h:91
The reverse spin lock unlocks its associated FXSpinLock when entering the scope, and automatically re...
Definition: FXSpinLock.h:110
FXSpinLock can be used to provide safe access to very small critical sections.
Definition: FXSpinLock.h:36
FXSpinLock & spinlock()
Return reference to associated spinlock.
Definition: FXSpinLock.h:85
void unlock()
Unlock spin lock.
Definition: FXSpinLock.h:135
~FXScopedSpinLock()
Destroy and unlock associated spinlock.
Definition: FXSpinLock.h:100
FXSpinLock & spinlock()
Return reference to associated spin lock.
Definition: FXSpinLock.h:123
void lock()
Lock the spinlock.
Definition: FX4Splitter.h:28
Establish a correspondence between a C++ scope and an FXSpinLock, so that entering and leaving the sc...
Definition: FXSpinLock.h:72
FXScopedSpinLock(FXSpinLock &s)
Construct & lock associated spinlock.
Definition: FXSpinLock.h:82
void unlock()
Unlock spinlock.
FXbool trylock()
Return true if succeeded locking the spinlock.
FXbool locked()
Return true if spinlock is already locked.
Definition: FXSpinLock.h:94
void unlock()
Unlock spin lock.
Definition: FXSpinLock.h:97
FXbool locked()
Return true if spin lock is already locked.
Definition: FXSpinLock.h:132
FXbool trylock()
Return true if succeeded locking the spin lock.
Definition: FXSpinLock.h:129
void lock()
Lock spin lock.
Definition: FXSpinLock.h:126
~FXReverseSpinLock()
Destroy and relock associated spin lock.
Definition: FXSpinLock.h:138

Copyright © 1997-2022 Jeroen van der Zijp