Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TRWSpinLock.hxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Authors: Enric Tejedor CERN 12/09/2016
3 // Philippe Canal FNAL 12/09/2016
4 
5 /*************************************************************************
6  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 
13 #ifndef ROOT_TRWSpinLock
14 #define ROOT_TRWSpinLock
15 
16 #include "TSpinMutex.hxx"
17 
18 #include <atomic>
19 #include <condition_variable>
20 
21 namespace ROOT {
22 class TRWSpinLock {
23 private:
24  std::atomic<int> fReaders; ///<! Number of readers
25  std::atomic<int> fReaderReservation; ///<! A reader wants access
26  std::atomic<int> fWriterReservation; ///<! A writer wants access
27  std::atomic<bool> fWriter; ///<! Is there a writer?
28  ROOT::TSpinMutex fMutex; ///<! RWlock internal mutex
29  std::condition_variable_any fCond; ///<! RWlock internal condition variable
30 
31 public:
32  ////////////////////////////////////////////////////////////////////////
33  /// Regular constructor.
34  TRWSpinLock() : fReaders(0), fReaderReservation(0), fWriterReservation(0), fWriter(false) {}
35 
36  void ReadLock();
37  void ReadUnLock();
38  void WriteLock();
39  void WriteUnLock();
40 };
41 
42 class TRWSpinLockReadGuard {
43 private:
44  TRWSpinLock &fLock;
45 
46 public:
47  TRWSpinLockReadGuard(TRWSpinLock &lock);
48  ~TRWSpinLockReadGuard();
49 };
50 
51 class TRWSpinLockWriteGuard {
52 private:
53  TRWSpinLock &fLock;
54 
55 public:
56  TRWSpinLockWriteGuard(TRWSpinLock &lock);
57  ~TRWSpinLockWriteGuard();
58 };
59 
60 } // end of namespace ROOT
61 
62 #endif