Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReadWriteLock.h
Go to the documentation of this file.
1 /*
2  * ReadWriteLock.h
3  *
4  * Define classes to guard the old C-style mutexes that are used in Hall D
5  *
6  * Created on: Apr 25, 2017
7  * Author: Hovanes Egiyan
8  */
9 
10 #ifndef READWRITELOCK_H_
11 #define READWRITELOCK_H_
12 
13 #include <pthread.h>
14 
15 #include <functional>
16 #include <iostream>
17 
19 public:
21 
23  if( ! functionsAreInitialized ) {
24  initFunctions();
25  }
26  }
27  virtual ~MutexLockBase() {}
28 
29  static int initFunctions() {
31  [=](pthread_mutex_t& mutex) -> int {return pthread_mutex_lock(&mutex);};
33  [=](pthread_mutex_t& mutex) -> int {return pthread_mutex_unlock(&mutex);};
34 
36  [=](pthread_rwlock_t& mutex) -> int {return pthread_rwlock_rdlock(&mutex);};
38  [=](pthread_rwlock_t& mutex) -> int {return pthread_rwlock_unlock(&mutex);};
39 
41  [=](pthread_rwlock_t& mutex) -> int {return pthread_rwlock_rdlock(&mutex);};
43  [=](pthread_rwlock_t& mutex) -> int {return pthread_rwlock_unlock(&mutex);};
45  return 0;
46  }
47 
48  static std::function<int(pthread_mutex_t&)> plainLockLock ;
49  static std::function<int(pthread_mutex_t&)> plainLockUnlock ;
50 
51  static std::function<int(pthread_rwlock_t&)> readLockLock ;
52  static std::function<int(pthread_rwlock_t&)> readLockUnock ;
53 
54  static std::function<int(pthread_rwlock_t&)> writeLockLock ;
55  static std::function<int(pthread_rwlock_t&)> writeLockUnlock ;
56 };
57 
58 // Define the template class
59 // This class will be the main class used for locking/unlocking
60 template<typename T>
61 class MutexLock : public MutexLockBase {
62 protected:
63  T& m_;
64  std::function<int(T&)> lockFunc;
65  std::function<int(T&)> unlockFunc;
66 public:
67 
68  //! Construct and lock mutex
69  MutexLock(T& m, std::function<int(T&)>& lock,
70  std::function<int(T&)>& unlock) : MutexLockBase(),
72  lockFunc(m_);
73  }
74  //! Destruct and unlock mutex
75  virtual ~MutexLock() {
76  unlockFunc(m_);
77  }
78  //! Explicitly lock mutex, need to make sure you do not get into racing condition
79  virtual int lock() {
80  return lockFunc(m_);
81  }
82  //! Explicitly unlock existing mutex
83  virtual int unlock() {
84  return unlockFunc(m_);
85  }
86  virtual T& getLock() {
87  return m_;
88  }
89 };
90 
91 
92 // class for simple mutex
93 class PlainLock: public MutexLock<pthread_mutex_t> {
94 public:
95  PlainLock(pthread_mutex_t& m) :
96  MutexLock<pthread_mutex_t>(m, plainLockLock,
98  }
99 };
100 
101 // class for read-write mutex for read locking
102 class ReadLock: public MutexLock<pthread_rwlock_t> {
103 public:
104  ReadLock(pthread_rwlock_t& m) :
105  MutexLock<pthread_rwlock_t>(m, readLockLock,
106  readLockUnock) {
107  }
108 };
109 
110 // class for read-write mutex for write locking
111 class WriteLock: public MutexLock<pthread_rwlock_t> {
112 public:
113  WriteLock(pthread_rwlock_t& m) :
114  MutexLock<pthread_rwlock_t>(m, writeLockLock,
115  writeLockUnlock) {
116  }
117 };
118 
119 #endif /* READWRITELOCK_H_ */
static int initFunctions()
Definition: ReadWriteLock.h:29
virtual ~MutexLockBase()
Definition: ReadWriteLock.h:27
MutexLock(T &m, std::function< int(T &)> &lock, std::function< int(T &)> &unlock)
Construct and lock mutex.
Definition: ReadWriteLock.h:69
virtual T & getLock()
Definition: ReadWriteLock.h:86
static std::function< int(pthread_rwlock_t &)> readLockLock
Definition: ReadWriteLock.h:51
PlainLock(pthread_mutex_t &m)
Definition: ReadWriteLock.h:95
virtual ~MutexLock()
Destruct and unlock mutex.
Definition: ReadWriteLock.h:75
static bool functionsAreInitialized
Definition: ReadWriteLock.h:20
static std::function< int(pthread_rwlock_t &)> writeLockLock
Definition: ReadWriteLock.h:54
virtual int unlock()
Explicitly unlock existing mutex.
Definition: ReadWriteLock.h:83
ReadLock(pthread_rwlock_t &m)
static std::function< int(pthread_rwlock_t &)> readLockUnock
Definition: ReadWriteLock.h:52
static std::function< int(pthread_mutex_t &)> plainLockLock
Definition: ReadWriteLock.h:48
static std::function< int(pthread_mutex_t &)> plainLockUnlock
Definition: ReadWriteLock.h:49
WriteLock(pthread_rwlock_t &m)
virtual int lock()
Explicitly lock mutex, need to make sure you do not get into racing condition.
Definition: ReadWriteLock.h:79
std::function< int(T &)> lockFunc
Definition: ReadWriteLock.h:64
std::function< int(T &)> unlockFunc
Definition: ReadWriteLock.h:65
static std::function< int(pthread_rwlock_t &)> writeLockUnlock
Definition: ReadWriteLock.h:55