GCC Code Coverage Report


Directory: ./
File: src/locked_condition_variable.cpp
Date: 2022-06-30 06:29:57
Exec Total Coverage
Lines: 35 61 57.4%
Branches: 20 50 40.0%

Line Branch Exec Source
1 #include "shared_memory/locked_condition_variable.hpp"
2
3 namespace shared_memory
4 {
5 178 LockedConditionVariable::LockedConditionVariable(
6 178 const std::string object_id, bool clean_memory_on_destruction)
7 : mutex_id_(object_id + "_mtx"),
8 condition_id_(object_id + "_cond"),
9 mutex_{boost::interprocess::open_or_create, mutex_id_.c_str()},
10
4/8
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 178 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 178 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 178 times.
✗ Branch 13 not taken.
178 clean_memory_on_destruction_(clean_memory_on_destruction)
11 {
12
1/2
✓ Branch 1 taken 178 times.
✗ Branch 2 not taken.
356 condition_variable_ = new SHMCondition(boost::interprocess::open_or_create,
13
2/4
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 178 times.
✗ Branch 6 not taken.
356 condition_id_.c_str());
14 178 }
15
16 356 LockedConditionVariable::~LockedConditionVariable()
17 {
18 178 unlock_scope();
19
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 if (condition_variable_ != nullptr)
20 {
21
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 delete (condition_variable_);
22 }
23
1/2
✓ Branch 0 taken 178 times.
✗ Branch 1 not taken.
178 if (clean_memory_on_destruction_)
24 {
25 178 boost::interprocess::named_mutex::remove(mutex_id_.c_str());
26 178 boost::interprocess::named_condition::remove(condition_id_.c_str());
27 }
28 178 }
29
30 void LockedConditionVariable::clean(const std::string segment_id)
31 {
32 LockedConditionVariable cv(segment_id, true);
33 }
34
35 8 void LockedConditionVariable::notify_all()
36 {
37 8 condition_variable_->notify_all();
38 8 }
39
40 void LockedConditionVariable::notify_one()
41 {
42 condition_variable_->notify_one();
43 }
44
45 8 void LockedConditionVariable::wait()
46 {
47
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 if (lock_)
48 {
49 8 condition_variable_->wait(*lock_);
50 }
51 else
52 {
53 std::cout
54 << "LockedConditionVariable::wait(): "
55 << "WARNING, undefined behavior, the scope has not been locked"
56 << std::endl;
57 }
58 8 }
59
60 1 bool LockedConditionVariable::timed_wait(long wait_nano_seconds)
61 {
62
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (lock_)
63 {
64 boost::posix_time::ptime current_time =
65
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 boost::interprocess::microsec_clock::universal_time();
66 boost::posix_time::time_duration waiting_time =
67
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 boost::posix_time::microseconds(static_cast<long>(
68
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 static_cast<double>(wait_nano_seconds) * 0.001));
69 2 return condition_variable_->timed_wait(*lock_,
70
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 current_time + waiting_time);
71 }
72 std::cout << "LockedConditionVariable::timed_wait(): "
73 << "WARNING, undefined behavior, the scope has not been locked"
74 << std::endl;
75 return false;
76 }
77
78 bool LockedConditionVariable::try_lock()
79 {
80 if (lock_)
81 {
82 return lock_->try_lock();
83 }
84 else
85 {
86 std::cout
87 << "LockedConditionVariable::try_lock(): "
88 << "WARNING, undefined behavior, the scope has not been locked"
89 << std::endl;
90 return false;
91 }
92 }
93
94 void LockedConditionVariable::unlock()
95 {
96 if (lock_)
97 {
98 lock_->unlock();
99 }
100 else
101 {
102 std::cout
103 << "LockedConditionVariable::try_lock(): "
104 << "WARNING, undefined behavior, the scope has not been locked"
105 << std::endl;
106 }
107 }
108
109 bool LockedConditionVariable::owns()
110 {
111 if (lock_)
112 {
113 return lock_->owns();
114 }
115 else
116 {
117 std::cout
118 << "LockedConditionVariable::owns(): "
119 << "WARNING, undefined behavior, the scope has not been locked"
120 << std::endl;
121 return false;
122 }
123 }
124
125 7 void LockedConditionVariable::lock_scope()
126 {
127
1/2
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 lock_.reset(new SHMScopeLock(mutex_));
128 7 }
129
130 185 void LockedConditionVariable::unlock_scope()
131 {
132 185 lock_.reset(nullptr);
133 185 }
134
135
2/4
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
✗ Branch 4 not taken.
162 } // namespace shared_memory
136