Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * @file lib.Mutex.hpp | ||
3 | * @author Sergey Baigudin, sergey@baigudin.software | ||
4 | * @copyright 2015-2022, Sergey Baigudin, Baigudin Software | ||
5 | */ | ||
6 | #ifndef LIB_MUTEX_HPP_ | ||
7 | #define LIB_MUTEX_HPP_ | ||
8 | |||
9 | #include "lib.NonCopyable.hpp" | ||
10 | #include "api.Mutex.hpp" | ||
11 | #include "sys.Call.hpp" | ||
12 | |||
13 | namespace eoos | ||
14 | { | ||
15 | namespace lib | ||
16 | { | ||
17 | |||
18 | /** | ||
19 | * @class Mutex<A> | ||
20 | * @brief Mutex class. | ||
21 | * | ||
22 | * @tparam A Heap memory allocator class. | ||
23 | */ | ||
24 | template <class A = Allocator> | ||
25 | class Mutex : public NonCopyable<A>, public api::Mutex | ||
26 | { | ||
27 | typedef Mutex<A> Self; | ||
28 | typedef NonCopyable<A> Parent; | ||
29 | |||
30 | public: | ||
31 | |||
32 | /** | ||
33 | * @brief Constructor. | ||
34 | */ | ||
35 | 63 | Mutex() | |
36 | : NonCopyable<A>() | ||
37 | , api::Mutex() | ||
38 | 63 | , mutex_ (NULLPTR){ | |
39 |
1/2✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
|
63 | bool_t const isConstructed( construct() ); |
40 | 63 | setConstructed( isConstructed ); | |
41 | 63 | } | |
42 | |||
43 | /** | ||
44 | * @brief Destructor. | ||
45 | */ | ||
46 | 124 | virtual ~Mutex() | |
47 | { | ||
48 | 124 | delete mutex_; | |
49 | } | ||
50 | |||
51 | /** | ||
52 | * @copydoc eoos::api::Object::isConstructed() | ||
53 | */ | ||
54 | 593 | virtual bool_t isConstructed() const | |
55 | { | ||
56 | 593 | return Parent::isConstructed(); | |
57 | } | ||
58 | |||
59 | /** | ||
60 | * @copydoc eoos::api::Mutex::tryLock() | ||
61 | */ | ||
62 | 4 | virtual bool_t tryLock() | |
63 | { | ||
64 | 4 | bool_t res( false ); | |
65 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | if( Self::isConstructed() ) |
66 | { | ||
67 | 4 | res = mutex_->tryLock(); | |
68 | } | ||
69 | 4 | return res; | |
70 | } | ||
71 | |||
72 | /** | ||
73 | * @copydoc eoos::api::Mutex::lock() | ||
74 | */ | ||
75 | 158 | virtual bool_t lock() | |
76 | { | ||
77 | 158 | bool_t res( false ); | |
78 |
1/2✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
|
158 | if( Self::isConstructed() ) |
79 | { | ||
80 | 158 | res = mutex_->lock(); | |
81 | } | ||
82 | 158 | return res; | |
83 | } | ||
84 | |||
85 | /** | ||
86 | * @copydoc eoos::api::Mutex::unlock() | ||
87 | */ | ||
88 | 160 | virtual void unlock() | |
89 | { | ||
90 |
1/2✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
|
160 | if( Self::isConstructed() ) |
91 | { | ||
92 | 160 | mutex_->unlock(); | |
93 | } | ||
94 | 160 | } | |
95 | |||
96 | protected: | ||
97 | |||
98 | using Parent::setConstructed; | ||
99 | |||
100 | private: | ||
101 | |||
102 | /** | ||
103 | * @brief Constructor. | ||
104 | * | ||
105 | * @return True if object has been constructed successfully. | ||
106 | */ | ||
107 | 63 | bool_t construct() | |
108 | { | ||
109 | 63 | bool_t res( false ); | |
110 | do | ||
111 | { | ||
112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
|
63 | if( !Self::isConstructed() ) |
113 | { ///< UT Justified Branch: HW dependency | ||
114 | ✗ | break; | |
115 | } | ||
116 | 63 | mutex_ = sys::Call::get().createMutex(); | |
117 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
|
63 | if( !Parent::isConstructed(mutex_) ) |
118 | { ///< UT Justified Branch: HW dependency | ||
119 | ✗ | break; | |
120 | } | ||
121 | 63 | res = true; | |
122 | } while(false); | ||
123 | 63 | return res; | |
124 | } | ||
125 | |||
126 | /** | ||
127 | * @brief System mutex interface. | ||
128 | */ | ||
129 | api::Mutex* mutex_; | ||
130 | |||
131 | }; | ||
132 | |||
133 | } // namespace lib | ||
134 | } // namespace eoos | ||
135 | #endif // LIB_MUTEX_HPP_ | ||
136 |