MOTION  0.01
Framework for mixed-protocol multi-party computation
fiber_condition.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Oleksandr Tkachenko, Lennart Braun
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 <boost/fiber/condition_variable.hpp>
28 #include <boost/fiber/mutex.hpp>
29 #include <functional>
30 
31 namespace encrypto::motion {
32 
36  public:
37  ~FiberCondition() = default;
38  FiberCondition() = delete;
39  FiberCondition(FiberCondition&) = delete;
40 
43  FiberCondition(const std::function<bool()> condition_function)
44  : condition_function_(condition_function) {}
45 
46  // checks if the condition was satisfied
47  // bool operator()() {
48  // std::scoped_lock lock(mutex_);
49  // return condition_function_();
50  // }
51 
53  void Wait() const {
54  std::unique_lock<decltype(mutex_)> lock(mutex_);
55  condition_variable_.wait(lock, condition_function_);
56  }
57 
60  template <typename Tick, typename Period>
61  bool WaitFor(std::chrono::duration<Tick, Period> duration) const {
62  std::unique_lock<decltype(mutex_)> lock(mutex_);
63  condition_variable_.wait_for(lock, duration, condition_function_);
64  return condition_function_();
65  }
66 
68  void NotifyOne() const noexcept { condition_variable_.notify_one(); }
69 
71  void NotifyAll() const noexcept { condition_variable_.notify_all(); }
72 
76  boost::fibers::mutex& GetMutex() noexcept { return mutex_; }
77 
78  private:
79  mutable boost::fibers::condition_variable condition_variable_;
80  mutable boost::fibers::mutex mutex_;
81  const std::function<bool()> condition_function_;
82 };
83 
84 } // namespace encrypto::motion
main
int main(int ac, char *av[])
Definition: example_template_main.cpp:48
party.h
encrypto::motion::FiberCondition::GetMutex
boost::fibers::mutex & GetMutex() noexcept
Get the mutex.
Definition: fiber_condition.h:76
encrypto::motion::PartyPointer
std::unique_ptr< Party > PartyPointer
Definition: party.h:387
encrypto::motion::FiberCondition
Wraps a boost::fibers::condition_variable with a boost::fibers::mutex and a condition checking functi...
Definition: fiber_condition.h:35
encrypto::motion::FiberCondition::FiberCondition
FiberCondition()=delete
ParseProgramOptions
std::pair< program_options::variables_map, bool > ParseProgramOptions(int ac, char *av[])
Definition: example_template_main.cpp:78
kPartyArgumentRegex
const std::regex kPartyArgumentRegex("(\\d+),(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}),(\\d{1,5})")
encrypto::motion::FiberCondition::NotifyAll
void NotifyAll() const noexcept
Unblocks all threads waiting for condition_variable_.
Definition: fiber_condition.h:71
encrypto::motion::communication::TcpSetupHelper
Definition: tcp_transport.h:69
communication_layer.h
encrypto::motion::FiberCondition::WaitFor
bool WaitFor(std::chrono::duration< Tick, Period > duration) const
Blocks until fiber is notified and condition_function_ returns true or duration time has passed.
Definition: fiber_condition.h:61
encrypto::motion::FiberCondition::NotifyOne
void NotifyOne() const noexcept
Unblocks one thread waiting for condition_variable_.
Definition: fiber_condition.h:68
ParsePartyArgument
std::tuple< std::size_t, std::string, std::uint16_t > ParsePartyArgument(const std::string &party_argument)
Definition: example_template_main.cpp:67
encrypto::motion::communication::TcpPartiesConfiguration
std::vector< TcpConnectionConfiguration > TcpPartiesConfiguration
Definition: tcp_transport.h:62
encrypto::motion::FiberCondition::FiberCondition
FiberCondition(const std::function< bool()> condition_function)
Registers the condition function that encapsulates the condition checking.
Definition: fiber_condition.h:43
CheckPartyArgumentSyntax
bool CheckPartyArgumentSyntax(const std::string &party_argument)
Definition: example_template_main.cpp:62
encrypto::motion
Definition: algorithm_description.cpp:35
geninput.help
help
Definition: geninput.py:150
encrypto::motion::FiberCondition::~FiberCondition
~FiberCondition()=default
example_template.h
EvaluateProtocol
encrypto::motion::RunTimeStatistics EvaluateProtocol(encrypto::motion::PartyPointer &party, std::size_t number_of_simd, encrypto::motion::MpcProtocol protocol, bool check)
Definition: aes128.cpp:56
encrypto::motion::FiberCondition::Wait
void Wait() const
Blocks until fiber is notified and condition_function_ returns true.
Definition: fiber_condition.h:53
tcp_transport.h
CreateParty
encrypto::motion::PartyPointer CreateParty(const program_options::variables_map &user_options)
Definition: example_template_main.cpp:137
encrypto::motion::communication::TcpSetupHelper::SetupConnections
std::vector< std::unique_ptr< Transport > > SetupConnections()
Definition: tcp_transport.cpp:203