IBR-DTNSuite
0.12
Main Page
Namespaces
Classes
Files
File List
File Members
AtomicCounter.cpp
Go to the documentation of this file.
1
/*
2
* AtomicCounter.cpp
3
*
4
* Copyright (C) 2011 IBR, TU Braunschweig
5
*
6
* Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7
*
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
11
*
12
* http://www.apache.org/licenses/LICENSE-2.0
13
*
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
19
*
20
*/
21
22
#include "
ibrcommon/thread/AtomicCounter.h
"
23
#include "
ibrcommon/thread/MutexLock.h
"
24
25
namespace
ibrcommon
26
{
27
AtomicCounter::AtomicCounter
(
int
init
)
28
: _value(init), _unblock(false)
29
{
30
31
}
32
33
AtomicCounter::~AtomicCounter
()
34
{
35
unblockAll
();
36
}
37
38
int
AtomicCounter::value
()
39
{
40
ibrcommon::MutexLock
l(_lock);
41
return
_value;
42
}
43
44
void
AtomicCounter::unblockAll
()
45
{
46
ibrcommon::MutexLock
l(_lock);
47
_unblock =
true
;
48
_lock.
signal
(
true
);
49
}
50
51
void
AtomicCounter::wait
(
int
until)
52
{
53
ibrcommon::MutexLock
l(_lock);
54
while
((_value != until) && !_unblock)
55
{
56
_lock.
wait
();
57
}
58
}
59
60
AtomicCounter
&
AtomicCounter::operator++
()
61
{
62
ibrcommon::MutexLock
l(_lock);
63
_value++;
64
_lock.
signal
(
true
);
65
return
*
this
;
66
}
67
68
AtomicCounter
AtomicCounter::operator++
(
int
)
69
{
70
ibrcommon::MutexLock
l(_lock);
71
_value++;
72
_lock.
signal
(
true
);
73
return
AtomicCounter
(_value - 1);
74
}
75
76
AtomicCounter
&
AtomicCounter::operator--
()
77
{
78
ibrcommon::MutexLock
l(_lock);
79
_value--;
80
_lock.
signal
(
true
);
81
return
*
this
;
82
}
83
84
AtomicCounter
AtomicCounter::operator--
(
int
)
85
{
86
ibrcommon::MutexLock
l(_lock);
87
_value--;
88
_lock.
signal
(
true
);
89
return
AtomicCounter
(_value + 1);
90
}
91
92
AtomicCounter::Lock::Lock
(
AtomicCounter
&counter)
93
: _counter(counter)
94
{
95
_counter++;
96
}
97
98
AtomicCounter::Lock::~Lock
()
99
{
100
_counter--;
101
}
102
}
ibrcommon
ibrcommon
thread
AtomicCounter.cpp
Generated on Thu Mar 27 2014 09:26:21 for IBR-DTNSuite by
1.8.4