Apache Mesos
count_down_latch.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_COUNT_DOWN_LATCH_HPP__
14 #define __PROCESS_COUNT_DOWN_LATCH_HPP__
15 
16 #include <atomic>
17 
18 #include <process/future.hpp>
19 
20 namespace process {
21 
22 // An implementation of a count down latch that returns a Future to
23 // signify when it gets triggered.
25 {
26 public:
27  CountDownLatch(size_t count = 1) : count(count) {}
28 
29  void decrement()
30  {
31  size_t expected = count.load();
32  while (expected > 0) {
33  if (count.compare_exchange_strong(expected, expected - 1)) {
34  if (expected == 1) {
35  promise.set(Nothing());
36  }
37  break;
38  }
39  }
40  }
41 
43  {
44  return promise.future();
45  }
46 
47 private:
48  std::atomic<size_t> count;
49  Promise<Nothing> promise;
50 };
51 
52 } // namespace process {
53 
54 #endif // __PROCESS_COUNT_DOWN_LATCH_HPP__
Definition: nothing.hpp:16
bool set(const T &_t)
Definition: future.hpp:827
CountDownLatch(size_t count=1)
Definition: count_down_latch.hpp:27
Future< Nothing > triggered()
Definition: count_down_latch.hpp:42
Definition: executor.hpp:48
Future< T > future() const
Definition: future.hpp:913
Definition: count_down_latch.hpp:24
void decrement()
Definition: count_down_latch.hpp:29