MOTION  0.01
Framework for mixed-protocol multi-party computation
locked_fiber_queue.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2020 Lennart Braun
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef LOCKED_FIBER_QUEUE_HPP
24 #define LOCKED_FIBER_QUEUE_HPP
25 
26 #include <boost/fiber/condition_variable.hpp>
27 #include <boost/fiber/mutex.hpp>
28 #include <future>
29 #include <iostream>
30 #include <mutex>
31 #include <optional>
32 #include <queue>
33 
34 namespace encrypto::motion {
35 
39 template <typename T>
41  public:
45  bool empty() const noexcept {
46  std::lock_guard lock(mutex_);
47  return queue_.empty();
48  }
49 
53  bool IsClosed() const noexcept {
54  std::lock_guard lock(mutex_);
55  return closed_;
56  }
57 
61  void close() noexcept {
62  {
63  std::scoped_lock lock(mutex_);
64  closed_ = true;
65  }
66  condition_variable_.notify_all();
67  }
68 
72  void enqueue(const T& item) {
73  if (closed_) {
74  throw std::logic_error("Tried to enqueue in closed LockedFiberQueue");
75  }
76  {
77  std::scoped_lock lock(mutex_);
78  queue_.push(item);
79  }
80  condition_variable_.notify_one();
81  }
82 
83  void enqueue(T&& item) {
84  if (closed_) {
85  throw std::logic_error("Tried to enqueue in closed LockedFiberQueue");
86  }
87  {
88  std::scoped_lock lock(mutex_);
89  queue_.push(std::move(item));
90  }
91  condition_variable_.notify_one();
92  }
93 
97  std::optional<T> dequeue() noexcept {
98  std::unique_lock lock(mutex_);
99  if (queue_.empty() && closed_) {
100  return std::nullopt;
101  }
102  if (queue_.empty() && !closed_) {
103  condition_variable_.wait(lock, [this] { return !this->queue_.empty() || this->closed_; });
104  }
105  if (queue_.empty()) {
106  return std::nullopt;
107  }
108  auto item = std::move(queue_.front());
109  queue_.pop();
110  lock.unlock();
111  return std::optional<T>(std::move(item));
112  }
113 
114  private:
115  bool closed_ = false;
116  std::queue<T> queue_;
117  mutable boost::fibers::mutex mutex_;
118  boost::fibers::condition_variable_any condition_variable_;
119 };
120 
121 } // namespace encrypto::motion
122 
123 #endif // LOCKED_QUEUE_HPP
encrypto::motion::AccumulatedCommunicationStatistics::Add
void Add(const communication::TransportStatistics &statistics)
Definition: analysis.cpp:125
analysis.h
party.h
encrypto::motion::AccumulatedRunTimeStatistics
Definition: analysis.h:43
encrypto::motion::MpcProtocol::kBmr
@ kBmr
encrypto::motion::PartyPointer
std::unique_ptr< Party > PartyPointer
Definition: party.h:387
encrypto::motion::LockedFiberQueue::enqueue
void enqueue(T &&item)
Definition: locked_fiber_queue.h:83
encrypto::motion::LockedFiberQueue::empty
bool empty() const noexcept
Definition: locked_fiber_queue.h:45
innerproduct.h
ParseProgramOptions
std::pair< program_options::variables_map, std::vector< bool > > ParseProgramOptions(int ac, char *av[])
Definition: innerproduct_main.cpp:117
encrypto::motion::MpcProtocol::kBooleanGmw
@ kBooleanGmw
encrypto::motion::LockedFiberQueue::enqueue
void enqueue(const T &item)
Definition: locked_fiber_queue.h:72
encrypto::motion::communication::TcpSetupHelper
Definition: tcp_transport.h:69
communication_layer.h
kPartyArgumentRegex
const std::regex kPartyArgumentRegex("([01]),([^,]+),(\\d{1,5})")
encrypto::motion::communication::TcpPartiesConfiguration
std::vector< TcpConnectionConfiguration > TcpPartiesConfiguration
Definition: tcp_transport.h:62
encrypto::motion
Definition: algorithm_description.cpp:35
geninput.help
help
Definition: geninput.py:150
encrypto::motion::PrintStatistics
std::string PrintStatistics(const std::string &experiment_name, const AccumulatedRunTimeStatistics &execution_statistics, const AccumulatedCommunicationStatistics &communication_statistics)
Definition: analysis.cpp:176
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
typedefs.h
encrypto::motion::MpcProtocol
MpcProtocol
Definition: typedefs.h:140
encrypto::motion::LockedFiberQueue::dequeue
std::optional< T > dequeue() noexcept
Definition: locked_fiber_queue.h:97
encrypto::motion::LockedFiberQueue::IsClosed
bool IsClosed() const noexcept
Definition: locked_fiber_queue.h:53
encrypto::motion::LockedFiberQueue::close
void close() noexcept
Definition: locked_fiber_queue.h:61
encrypto::motion::MpcProtocol::kArithmeticGmw
@ kArithmeticGmw
tcp_transport.h
encrypto::motion::LockedFiberQueue
Definition: locked_fiber_queue.h:40
ParsePartyArgument
std::tuple< std::size_t, std::string, std::uint16_t > ParsePartyArgument(const std::string &party_argument)
Definition: innerproduct_main.cpp:106
CheckPartyArgumentSyntax
bool CheckPartyArgumentSyntax(const std::string &party_argument)
Definition: innerproduct_main.cpp:101
main
int main(int ac, char *av[])
Definition: innerproduct_main.cpp:49
encrypto::motion::AccumulatedCommunicationStatistics
Definition: analysis.h:65
CreateParty
encrypto::motion::PartyPointer CreateParty(const program_options::variables_map &user_options)
Definition: innerproduct_main.cpp:199
encrypto::motion::communication::TcpSetupHelper::SetupConnections
std::vector< std::unique_ptr< Transport > > SetupConnections()
Definition: tcp_transport.cpp:203