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,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 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 }
104 
105 #endif
106 

Copyright © 1997-2016 Jeroen van der Zijp