-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCondition.cpp
124 lines (105 loc) · 3.37 KB
/
Condition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Condition.cpp
*
* Created on: Apr 12, 2012
* Author: knuthelv
*/
#include "RxConcurrent/Condition.h"
#include "RxConcurrent/WaitSet.h"
namespace BaseLib { namespace Concurrent
{
// -------------------------------------------------------
// Base class for Condition implementations
// -------------------------------------------------------
/**
* @brief Condition::~Condition: Since WaitSet uses shared_pointers it is up to the
* user to detach from waitSet and then condition is automatically deleted.
*/
AbstractCondition::~AbstractCondition()
{ }
/**
* @brief Condition::GetPtr
* @return
*/
AbstractCondition::Ptr AbstractCondition::GetPtr()
{
return shared_from_this();
}
// -------------------------------------------------------
// Thread safe implementation
// -------------------------------------------------------
void AbstractCondition::SignalAll()
{
WaitSetSet copyOfSet;
{
MutexLocker lock(&mutex_);
copyOfSet = waitSet_;
}
// NB! WaitSet calls mutexed functions in Condition objects,
// therefore, this should be a non-mutexed function
AbstractCondition::signalAll(copyOfSet, this->GetPtr());
}
// -------------------------------------------------------
// Thread safe implementation
// -------------------------------------------------------
bool AbstractCondition::Attach(WaitSet *waitSet)
{
MutexLocker lock(&mutex_);
return attach(waitSet);
}
// -------------------------------------------------------
// Thread safe implementation
// -------------------------------------------------------
bool AbstractCondition::Detach(WaitSet *waitSet)
{
MutexLocker lock(&mutex_);
return detach(waitSet);
}
// -------------------------------------------------------
// Usage: When a Condition's trigger value is altered then
// call SignalAll to wakeup WaitSet
// -------------------------------------------------------
void AbstractCondition::signalAll(const WaitSetSet &waitSet, std::shared_ptr<AbstractCondition> self)
{
// NB! WaitSet calls mutexed functions in Condition objects,
// therefore, this should be a non-mutexed function
for(WaitSetSet::const_iterator it = waitSet.begin(), it_end = waitSet.end(); it != it_end; ++it)
{
WaitSet *waitSet = *it;
ASSERT(waitSet != NULL);
waitSet->Signal(self);
}
}
// -------------------------------------------------------
// Usage: When a Condition is attached to a WaitSet
// the WaitSet calls attachWaitSet(this)
// -------------------------------------------------------
bool AbstractCondition::attach(WaitSet *waitSet)
{
std::pair<WaitSetSet::iterator, bool> pairIterator = waitSet_.insert(waitSet);
if(pairIterator.second == false)
{
IWARNING() << "WaitSet already present!";
}
// -- debug element is inserted --
ASSERT(pairIterator.second == true);
// -- debug --
return pairIterator.second;
}
// -------------------------------------------------------
// Usage: When a Condition is detached from a WaitSet
// the WaitSet calls detachWaitSet(this)
// -------------------------------------------------------
bool AbstractCondition::detach(WaitSet *waitSet)
{
size_t erased = waitSet_.erase(waitSet);
if(erased == 0)
{
IWARNING() << "WaitSet already deleted!";
}
// -- debug element is erased --
ASSERT(erased == 1);
// -- debug --
return (erased == 1);
}
}}