GCC Code Coverage Report


Directory: codebase/
File: codebase/system/include/private/sys.Mutex.hpp
Date: 2023-03-16 04:37:09
Exec Total Coverage
Lines: 38 41 92.7%
Functions: 8 8 100.0%
Branches: 9 17 52.9%

Line Branch Exec Source
1 /**
2 * @file sys.Mutex.hpp
3 * @author Sergey Baigudin, sergey@baigudin.software
4 * @copyright 2021-2022, Sergey Baigudin, Baigudin Software
5 */
6 #ifndef SYS_MUTEX_HPP_
7 #define SYS_MUTEX_HPP_
8
9 #include "sys.NonCopyable.hpp"
10 #include "api.Mutex.hpp"
11
12 namespace eoos
13 {
14 namespace sys
15 {
16
17 /**
18 * @class Mutex.
19 * @brief Mutex class.
20 */
21 class Mutex : public NonCopyable, public api::Mutex
22 {
23 typedef NonCopyable Parent;
24
25 public:
26
27 /**
28 * @brief Constructor.
29 */
30 63 Mutex()
31 63 : NonCopyable()
32 , api::Mutex()
33 63 , mutex_() {
34
1/2
✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
63 bool_t const isConstructed( construct() );
35 63 setConstructed( isConstructed );
36 63 }
37
38 /**
39 * @brief Destructor.
40 */
41 248 virtual ~Mutex()
42 {
43 124 destruct();
44
2/3
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
248 }
45
46 /**
47 * @copydoc eoos::api::Object::isConstructed()
48 */
49 511 virtual bool_t isConstructed() const ///< SCA MISRA-C++:2008 Justified Rule 10-3-1
50 {
51 511 return Parent::isConstructed();
52 }
53
54 /**
55 * @copydoc eoos::api::Mutex::tryLock()
56 */
57 4 virtual bool_t tryLock()
58 {
59 4 bool_t res( false );
60
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if( isConstructed() )
61 {
62 4 int_t const error( ::pthread_mutex_trylock(&mutex_) );
63 4 res = (error == 0) ? true : false;
64 }
65 4 return res;
66 }
67
68 /**
69 * @copydoc eoos::api::Mutex::lock()
70 */
71 158 virtual bool_t lock()
72 {
73 158 bool_t res( false );
74
1/2
✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
158 if( isConstructed() )
75 {
76 158 int_t const error( ::pthread_mutex_lock(&mutex_) );
77 158 res = (error == 0) ? true : false;
78 }
79 158 return res;
80 }
81
82 /**
83 * @copydoc eoos::api::Mutex::unlock()
84 */
85 160 virtual void unlock()
86 {
87
1/2
✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
160 if( isConstructed() )
88 {
89 160 int_t const error( ::pthread_mutex_unlock(&mutex_) );
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
160 if (error != 0)
91 { ///< UT Justified Branch: OS dependency
92 setConstructed(false);
93 }
94 }
95 160 }
96
97 private:
98
99 /**
100 * @brief Constructs this object.
101 *
102 * @return True if object has been constructed successfully.
103 */
104 63 bool_t construct()
105 {
106 63 bool_t res( false );
107 do
108 {
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
63 if( !isConstructed() )
110 { ///< UT Justified Branch: HW dependency
111 break;
112 }
113 63 int_t const error( ::pthread_mutex_init(&mutex_, NULL) );
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if(error != 0)
115 { ///< UT Justified Branch: OS dependency
116 break;
117 }
118 63 res = true;
119 } while(false);
120 63 return res;
121 }
122
123 /**
124 * @brief Destructs this object.
125 */
126 62 void destruct()
127 {
128 62 static_cast<void>( ::pthread_mutex_destroy(&mutex_) );
129 62 }
130
131 /**
132 * @brief Mutex resource identifier.
133 */
134 ::pthread_mutex_t mutex_;
135
136 };
137
138 } // namespace sys
139 } // namespace eoos
140 #endif // SYS_MUTEX_HPP_
141