|
15 | 15 |
|
16 | 16 | #include "pthread/mutex.hpp" |
17 | 17 |
|
18 | | - |
19 | 18 | namespace pthread { |
20 | 19 |
|
21 | | - /** \addtogroup concurrency |
22 | | - * |
23 | | - * @{ |
24 | | - */ |
25 | | - |
26 | | - //class condition_variable ; |
27 | | - |
28 | | - /** |
29 | | - This class was designed to encapsulate a mutex and automatically control the lock attribute. |
30 | | -
|
31 | | - The lock_guard lock the associated mutex once we instanciate the class and the lock is automatically unlocked |
32 | | - once the object is destroyed. This allow us to correlate the lock with the scope of the object. |
33 | | -
|
34 | | - @author herbert koelman |
35 | | - */ |
36 | | - template<class MutexType> |
37 | | - class lock_guard { |
38 | | - |
39 | | - friend class condition_variable; |
40 | | - |
41 | | - public: |
42 | | - /** |
43 | | - * The constructor is forced to only accept a mutex object or any object of a subclass. |
| 20 | + /** \addtogroup concurrency |
44 | 21 | * |
45 | | - * The mutex is locked up upon completion. |
46 | | - * |
47 | | - * @param m reference to a valid pthread::mutex |
48 | | - */ |
49 | | - explicit lock_guard(MutexType &m){ //: _mutex(&m) { |
50 | | - _mutex = &m ; |
51 | | - _mutex->lock(); |
52 | | - } |
53 | | - |
54 | | - /** The destructor release the guarded mutex. |
| 22 | + * @{ |
55 | 23 | */ |
56 | | - ~lock_guard() { |
57 | | - _mutex->unlock(); |
58 | | - } |
59 | 24 |
|
60 | 25 | /** |
61 | | - * Cannot be copied. |
62 | | - */ |
63 | | - lock_guard( const lock_guard &) = delete; |
64 | | - |
65 | | - /** @return a const reference to the guarded mutex |
| 26 | + * This class was designed to encapsulate a mutex and automatically control the lock attribute. |
66 | 27 | * |
67 | | - * @deprecated condition_variable is now a friend class |
68 | | - */ |
69 | | - MutexType *internal_mutex() { |
70 | | - return _mutex ; |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - Desabling the = operator. |
| 28 | + * The lock_guard lock the associated mutex once we instanciate the class and the lock is automatically unlocked |
| 29 | + * once the object is destroyed. This allow us to correlate the lock with the scope of the object. |
| 30 | + * |
| 31 | + * > *WARN* this is a wrapper and therefore doesn't deal with allocating/deallocating the mutex it is guarding. |
| 32 | + * |
| 33 | + * @author herbert koelman |
| 34 | + * @tparam MutexType a kind of mutex |
75 | 35 | */ |
76 | | - void operator=(lock_guard &) = delete; |
77 | | - |
78 | | - private: |
79 | | - MutexType *_mutex; |
80 | | - }; |
81 | | - |
82 | | - /** @} */ |
| 36 | + template<class MutexType> |
| 37 | + class lock_guard { |
| 38 | + |
| 39 | + friend class condition_variable; |
| 40 | + |
| 41 | + public: |
| 42 | + /** |
| 43 | + * The constructor is forced to only accept a mutex object or any object of a subclass. |
| 44 | + * |
| 45 | + * > *WARN* The mutex is locked up upon completion. |
| 46 | + * |
| 47 | + * @param mutex reference to a valid mutex type. |
| 48 | + * @see mutex#lock |
| 49 | + */ |
| 50 | + explicit lock_guard(MutexType &mutex) { //: _mutex(&m) { |
| 51 | + _mutex = &mutex; |
| 52 | + _mutex->lock(); |
| 53 | + } |
| 54 | + |
| 55 | + /** The destructor release the guarded mutex. |
| 56 | + * |
| 57 | + * @see mutex#unlock |
| 58 | + */ |
| 59 | + ~lock_guard() { |
| 60 | + _mutex->unlock(); |
| 61 | + } |
| 62 | + |
| 63 | + /** @return a const reference to the guarded mutex |
| 64 | + * |
| 65 | + * @deprecated condition_variable is now a friend class |
| 66 | + * @see condition_variable |
| 67 | + */ |
| 68 | + MutexType *internal_mutex() { |
| 69 | + return _mutex; |
| 70 | + } |
| 71 | + |
| 72 | + /**Cannot be copied. |
| 73 | + */ |
| 74 | + lock_guard(const lock_guard &) = delete; |
| 75 | + |
| 76 | + /** |
| 77 | + * Desabling the = operator. |
| 78 | + */ |
| 79 | + void operator=(lock_guard &) = delete; |
| 80 | + |
| 81 | + private: |
| 82 | + MutexType *_mutex; |
| 83 | + }; |
| 84 | + |
| 85 | + /** @} */ |
83 | 86 | } // namespace pthread |
84 | 87 |
|
85 | 88 | #endif /* pthread_lock_guard_H */ |
0 commit comments