Directory: | ./ |
---|---|
File: | src/condition_variable.cpp |
Date: | 2022-06-30 06:29:57 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 18 | 28 | 64.3% |
Branches: | 8 | 24 | 33.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "shared_memory/condition_variable.hpp" | ||
2 | |||
3 | namespace shared_memory | ||
4 | { | ||
5 | 59 | ConditionVariable::ConditionVariable(const std::string object_id, | |
6 | 59 | bool clean_memory_on_destruction) | |
7 | : | ||
8 | |||
9 | condition_id_(object_id), | ||
10 | 59 | clean_memory_on_destruction_(clean_memory_on_destruction) | |
11 | { | ||
12 | // note: using an instance that is defined in initializer list | ||
13 | // sometimes results in segfault. Thus switching to pointer. | ||
14 |
1/2✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
|
118 | condition_variable_ = new SHMCondition(boost::interprocess::open_or_create, |
15 |
2/4✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 59 times.
✗ Branch 6 not taken.
|
118 | condition_id_.c_str()); |
16 | 59 | } | |
17 | |||
18 | 118 | ConditionVariable::~ConditionVariable() | |
19 | { | ||
20 |
1/2✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
|
59 | delete (condition_variable_); |
21 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 1 times.
|
59 | if (clean_memory_on_destruction_) |
22 | { | ||
23 | 58 | boost::interprocess::named_condition::remove(condition_id_.c_str()); | |
24 | } | ||
25 | 59 | } | |
26 | |||
27 | ✗ | void ConditionVariable::clean(const std::string object_id) | |
28 | { | ||
29 | ✗ | boost::interprocess::named_condition::remove(object_id.c_str()); | |
30 | } | ||
31 | |||
32 | ✗ | void ConditionVariable::notify_all() | |
33 | { | ||
34 | ✗ | condition_variable_->notify_all(); | |
35 | } | ||
36 | |||
37 | 22 | void ConditionVariable::notify_one() | |
38 | { | ||
39 | 22 | condition_variable_->notify_one(); | |
40 | 22 | } | |
41 | |||
42 | 20 | void ConditionVariable::wait(Lock &lock) | |
43 | { | ||
44 | // ConditionVariable is friend of Lock | ||
45 | 20 | condition_variable_->wait(lock.lock_); | |
46 | 20 | } | |
47 | |||
48 | ✗ | bool ConditionVariable::timed_wait(Lock &lock, long wait_nano_seconds) | |
49 | { | ||
50 | boost::posix_time::ptime current_time = | ||
51 | ✗ | boost::interprocess::microsec_clock::universal_time(); | |
52 | boost::posix_time::time_duration waiting_time = | ||
53 | ✗ | boost::posix_time::microseconds( | |
54 | ✗ | static_cast<long>(static_cast<double>(wait_nano_seconds) * 0.001)); | |
55 | ✗ | return condition_variable_->timed_wait(lock.lock_, | |
56 | ✗ | current_time + waiting_time); | |
57 | } | ||
58 | |||
59 |
2/4✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
✗ Branch 4 not taken.
|
162 | } // namespace shared_memory |
60 |