GCC Code Coverage Report


Directory: codebase/
File: codebase/library/include/public/lib.MutexGuard.hpp
Date: 2023-03-16 04:37:09
Exec Total Coverage
Lines: 15 16 93.8%
Functions: 6 8 75.0%
Branches: 6 8 75.0%

Line Branch Exec Source
1 /**
2 * @file lib.MutexGuard.hpp
3 * @author Sergey Baigudin, sergey@baigudin.software
4 * @copyright 2020-2022, Sergey Baigudin, Baigudin Software
5 */
6 #ifndef LIB_MUTEXGUARD_HPP_
7 #define LIB_MUTEXGUARD_HPP_
8
9 #include "lib.NonCopyable.hpp"
10 #include "lib.Mutex.hpp"
11
12 namespace eoos
13 {
14 namespace lib
15 {
16
17 /**
18 * @class MutexGuard<A>
19 * @brief Guard of mutex.
20 *
21 * @tparam A heap memory allocator class.
22 */
23 template <class A = Allocator>
24 class MutexGuard : public NonCopyable<A>
25 {
26 typedef NonCopyable<A> Parent;
27
28 public:
29
30 using Parent::isConstructed;
31
32 /**
33 * @brief Constructor.
34 *
35 * @param mutex A mutex for guarding.
36 */
37 156 MutexGuard(api::Mutex& mutex)
38 : NonCopyable<A>()
39 156 , mutex_ (mutex){
40
1/2
✓ Branch 1 taken 156 times.
✗ Branch 2 not taken.
156 bool_t const isConstructed( construct() );
41 156 setConstructed( isConstructed );
42 156 }
43
44 /**
45 * @brief Destructor.
46 */
47 312 virtual ~MutexGuard()
48 {
49
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 1 times.
312 if( isConstructed() )
50 {
51 310 mutex_.unlock();
52 }
53 }
54
55 protected:
56
57 using Parent::setConstructed;
58
59 private:
60
61 /**
62 * @brief Constructs this object.
63 *
64 * @return True if this object has been constructed successfully.
65 */
66 156 bool_t construct()
67 {
68 156 bool_t res( false );
69 do
70 {
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 156 times.
156 if( !isConstructed() )
72 { ///< UT Justified Branch: HW dependency
73 break;
74 }
75
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 155 times.
156 if( !mutex_.isConstructed() )
76 {
77 1 break;
78 }
79 155 res = mutex_.lock();
80 } while(false);
81 156 return res;
82 }
83
84 /**
85 * @brief Mutex resource identifier.
86 */
87 api::Mutex& mutex_;
88 };
89
90 } // namespace lib
91 } // namespace eoos
92 #endif // LIB_MUTEXGUARD_HPP_
93