Apache Mesos
gate.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 __GATE_HPP__
14 #define __GATE_HPP__
15 
16 #include <condition_variable>
17 #include <mutex>
18 
19 #include <stout/synchronized.hpp>
20 
21 namespace process {
22 
23 // A Gate abstracts the concept of something that can "only happen
24 // once and every one else needs to queue up and wait until that thing
25 // happens" ... kind of like a gate that can only ever open.
26 //
27 // NOTE: historically a gate could be opened and _closed_ allowing but
28 // those semantics are no longer needed so they have been removed in
29 // order to simplify the implementation.
30 //
31 // TODO(benh): Consider removing this entirely and using `Once` for
32 // cleanup of a `ProcessBase` instead of a `Gate`.
33 class Gate
34 {
35 public:
36  // Opens the gate and notifies all the waiters.
37  void open()
38  {
39  synchronized (mutex) {
40  opened = true;
41  cond.notify_all();
42  }
43  }
44 
45  // Blocks the current thread until the gate has been opened.
46  void wait()
47  {
48  synchronized (mutex) {
49  while (!opened) {
50  synchronized_wait(&cond, &mutex);
51  }
52  }
53  }
54 
55 private:
56  bool opened = false;
57  std::mutex mutex;
58  std::condition_variable cond;
59 };
60 
61 } // namespace process {
62 
63 #endif // __GATE_HPP__
void synchronized_wait(CV *cv, Lock *lock)
Waits on the condition variable associated with &#39;lock&#39; which has already been synchronized.
void wait()
Definition: gate.hpp:46
Definition: gate.hpp:33
void open()
Definition: gate.hpp:37
Definition: executor.hpp:48