IBR-DTNSuite  0.10
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 
24 
25 namespace ibrcommon
26 {
28  : _value(init), _unblock(false)
29  {
30 
31  }
32 
34  {
35  unblockAll();
36  }
37 
39  {
40  ibrcommon::MutexLock l(_lock);
41  return _value;
42  }
43 
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 
61  {
62  ibrcommon::MutexLock l(_lock);
63  _value++;
64  _lock.signal(true);
65  return *this;
66  }
67 
69  {
70  ibrcommon::MutexLock l(_lock);
71  _value++;
72  _lock.signal(true);
73  return AtomicCounter(_value - 1);
74  }
75 
77  {
78  ibrcommon::MutexLock l(_lock);
79  _value--;
80  _lock.signal(true);
81  return *this;
82  }
83 
85  {
86  ibrcommon::MutexLock l(_lock);
87  _value--;
88  _lock.signal(true);
89  return AtomicCounter(_value + 1);
90  }
91 
93  : _counter(counter)
94  {
95  _counter++;
96  }
97 
99  {
100  _counter--;
101  }
102 }