Jpp  18.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMutex.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JMUTEX__
2 #define __JLANG__JMUTEX__
3 
4 #include <pthread.h>
5 
6 /**
7  * \file
8  * Very basic lock facilities to be used in multithreaded programs.
9  * \author cpellegrino
10  */
11 
12 namespace JSYNCHRONIZATION {}
13 namespace JPP { using namespace JSYNCHRONIZATION; }
14 
15 namespace JSYNCHRONIZATION {
16 
17 /**
18  * Scoped lock.
19  *
20  * The main purpose of this class is to provide an exception-safe locking
21  * facility.
22  * The lock on the mutex is acquired during construction, while the
23  * unlock is performed in the destructor.
24  *
25  * Es:
26  * Case 1, without scoped lock:
27  * using namespace JSYNCHRONIZATION;
28  * JMutex mutex;
29  *
30  * {
31  * mutex.lock();
32  * throwing_function(variable_to_protect); // <- this shall throw
33  * mutex.lock(); // <- this call is never
34  * // executed -> deadlock
35  * }
36  *
37  * Case 2, with scoped lock:
38  * using namespace JSYNCHRONIZATION;
39  * JMutex mutex;
40  *
41  * {
42  * JScopedLock(mutex);
43  * throwing_function(variable_to_protect); // <- this shall throw
44  * } // the lock is released here automatically
45  */
46 template<typename Lockable>
48 {
49 private:
50  Lockable* m_lock;
51 
52  /**
53  * Neither copy-constructible nor copy-assignable
54  */
59 
60  public:
61 
62  /**
63  * Constructor
64  * \param lock a lockable object (e.g.\ JScopedLock)
65  */
66  explicit JBasicScopedLock(Lockable& lock)
67  :
68  m_lock(&lock)
69  {
70  m_lock->lock();
71  }
72 
73  /**
74  * Destructor
75  */
77  {
78  unlock();
79  }
80 
81  /**
82  * Unlock the mutex.
83  * \return 0 on success, an error code otherwise.
84  * See man 3p pthread_mutex_lock for further info.
85  */
86  int unlock()
87  {
88  return m_lock->unlock();
89  }
90 };
91 
92 /**
93  * Mutex.
94  */
95 class JMutex
96 {
97  pthread_mutex_t m_mutex;
98 
99  public:
100 
101  /**
102  * Constructor.
103  */
105  {
106  pthread_mutex_init(&m_mutex, 0);
107  }
108 
109  /**
110  * Destructor.
111  */
113  {
114  pthread_mutex_destroy(&m_mutex);
115  }
116 
117  /**
118  * Lock the mutex.
119  * \return 0 on success, an error code otherwise.
120  * See man 3p pthread_mutex_lock for further info.
121  */
122  int lock()
123  {
124  return pthread_mutex_lock(&m_mutex);
125  }
126 
127  /**
128  * Try lock the mutex.
129  * \return 0 on success, an error code otherwise.
130  * See man 3p pthread_mutex_lock for further info.
131  */
132  int try_lock()
133  {
134  return pthread_mutex_trylock(&m_mutex);
135  }
136 
137  /**
138  * Unlock the mutex.
139  * \return 0 on success, an error code otherwise.
140  * See man 3p pthread_mutex_lock for further info.
141  */
142  int unlock()
143  {
144  return pthread_mutex_unlock(&m_mutex);
145  }
146 
148 };
149 
150 } // ns JSYNCHRONIZATION
151 
152 #endif // __JLANG__JMUTEX__
~JMutex()
Destructor.
Definition: JMutex.hh:112
JBasicScopedLock< JMutex > JScopedLock
Definition: JMutex.hh:147
JBasicScopedLock operator=(const JBasicScopedLock &)
int lock()
Lock the mutex.
Definition: JMutex.hh:122
int unlock()
Unlock the mutex.
Definition: JMutex.hh:86
JBasicScopedLock(const JBasicScopedLock &)
Neither copy-constructible nor copy-assignable.
pthread_mutex_t m_mutex
Definition: JMutex.hh:97
int unlock()
Unlock the mutex.
Definition: JMutex.hh:142
JBasicScopedLock(Lockable &lock)
Constructor.
Definition: JMutex.hh:66
JMutex()
Constructor.
Definition: JMutex.hh:104
int try_lock()
Try lock the mutex.
Definition: JMutex.hh:132