MOTION  0.01
Framework for mixed-protocol multi-party computation
condition.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Oleksandr Tkachenko
4 // Cryptography and Privacy Engineering Group (ENCRYPTO)
5 // TU Darmstadt, Germany
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 
25 #pragma once
26 
27 #include <condition_variable>
28 #include <functional>
29 #include <mutex>
30 
31 namespace encrypto::motion {
32 
35 class Condition {
36  public:
37  ~Condition() = default;
38  Condition() = delete;
39  Condition(Condition&) = delete;
40 
43  Condition(const std::function<bool()> condition_function)
44  : condition_function_(condition_function) {}
45 
47  bool operator()() {
48  std::scoped_lock lock(mutex_);
49  return condition_function_();
50  }
51 
53  bool Wait();
54 
58  template <typename Tick, typename Period>
59  bool WaitFor(std::chrono::duration<Tick, Period> duration) {
60  std::unique_lock<std::mutex> lock(mutex_);
61  condition_variable_.wait_for(lock, duration, condition_function_);
62  return condition_function_();
63  }
64 
66  void NotifyOne() noexcept { condition_variable_.notify_one(); }
67 
69  void NotifyAll() noexcept { condition_variable_.notify_all(); }
70 
74  std::mutex& GetMutex() noexcept { return mutex_; }
75 
76  private:
77  std::condition_variable condition_variable_;
78  std::mutex mutex_;
79  const std::function<bool()> condition_function_;
80 };
81 
82 } // namespace encrypto::motion
condition.h
encrypto::motion::Condition::Wait
bool Wait()
Blocks until thread is notified and condition_function_ returns true.
Definition: condition.cpp:29
encrypto::motion::Condition::NotifyOne
void NotifyOne() noexcept
Unblocks one thread waiting for condition_variable_.
Definition: condition.h:66
encrypto::motion::Condition::GetMutex
std::mutex & GetMutex() noexcept
Get the mutex.
Definition: condition.h:74
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::Condition::WaitFor
bool WaitFor(std::chrono::duration< Tick, Period > duration)
Blocks until thread is notified and condition_function_ returns true or duration time has passed.
Definition: condition.h:59
encrypto::motion::Condition::Condition
Condition()=delete
encrypto::motion::Condition::operator()
bool operator()()
checks if the condition was satisfied
Definition: condition.h:47
encrypto::motion::Condition
Wraps a std::condition_variable with a std::mutex and a condition checking function.
Definition: condition.h:35
encrypto::motion::Condition::NotifyAll
void NotifyAll() noexcept
Unblocks all threads waiting for condition_variable_.
Definition: condition.h:69
encrypto::motion::Condition::~Condition
~Condition()=default
encrypto::motion::Condition::Condition
Condition(const std::function< bool()> condition_function)
Registers the condition function that encapsulates the condition checking.
Definition: condition.h:43