Jpp  pmt_effective_area_update
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  Lockable* m_lock;
50 
51  /**
52  * Neither copy-constructible nor copy-assignable
53  */
56 
57  public:
58 
59  /**
60  * Constructor
61  * \param lock a lockable object (e.g. JScopedLock)
62  */
63  explicit JBasicScopedLock(Lockable& lock)
64  :
65  m_lock(&lock)
66  {
67  m_lock->lock();
68  }
69 
70  /**
71  * Destructor
72  */
74  {
75  unlock();
76  }
77 
78  /**
79  * Unlock the mutex.
80  * \return 0 on success, an error code otherwise.
81  * See man 3p pthread_mutex_lock for further info.
82  */
83  int unlock()
84  {
85  return m_lock->unlock();
86  }
87 };
88 
89 /**
90  * Mutex.
91  */
92 class JMutex
93 {
94  pthread_mutex_t m_mutex;
95 
96  public:
97 
98  /**
99  * Constructor.
100  */
102  {
103  pthread_mutex_init(&m_mutex, 0);
104  }
105 
106  /**
107  * Destructor.
108  */
110  {
111  pthread_mutex_destroy(&m_mutex);
112  }
113 
114  /**
115  * Lock the mutex.
116  * \return 0 on success, an error code otherwise.
117  * See man 3p pthread_mutex_lock for further info.
118  */
119  int lock()
120  {
121  return pthread_mutex_lock(&m_mutex);
122  }
123 
124  /**
125  * Try lock the mutex.
126  * \return 0 on success, an error code otherwise.
127  * See man 3p pthread_mutex_lock for further info.
128  */
129  int try_lock()
130  {
131  return pthread_mutex_trylock(&m_mutex);
132  }
133 
134  /**
135  * Unlock the mutex.
136  * \return 0 on success, an error code otherwise.
137  * See man 3p pthread_mutex_lock for further info.
138  */
139  int unlock()
140  {
141  return pthread_mutex_unlock(&m_mutex);
142  }
143 
145 };
146 
147 } // ns JSYNCHRONIZATION
148 
149 #endif // __JLANG__JMUTEX__
~JMutex()
Destructor.
Definition: JMutex.hh:109
JBasicScopedLock< JMutex > JScopedLock
Definition: JMutex.hh:144
JBasicScopedLock operator=(const JBasicScopedLock &)
int lock()
Lock the mutex.
Definition: JMutex.hh:119
int unlock()
Unlock the mutex.
Definition: JMutex.hh:83
JBasicScopedLock(const JBasicScopedLock &)
Neither copy-constructible nor copy-assignable.
pthread_mutex_t m_mutex
Definition: JMutex.hh:94
int unlock()
Unlock the mutex.
Definition: JMutex.hh:139
JBasicScopedLock(Lockable &lock)
Constructor.
Definition: JMutex.hh:63
JMutex()
Constructor.
Definition: JMutex.hh:101
int try_lock()
Try lock the mutex.
Definition: JMutex.hh:129