MOTION  0.01
Framework for mixed-protocol multi-party computation
fiber_thread_pool.hpp
Go to the documentation of this file.
1 // Copyright 2019 Lennart Braun
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20 
21 #ifndef FIBER_THREAD_POOL_HPP
22 #define FIBER_THREAD_POOL_HPP
23 
24 #include <functional>
25 #include <memory>
26 #include <thread>
27 #include <vector>
28 
29 namespace boost::fibers {
30 
31 class barrier;
32 template <typename T>
34 
35 } // namespace boost::fibers
36 
37 struct pool_ctx;
38 
39 namespace encrypto::motion {
40 
42 public:
43  using task_t = std::function<void()>;
44 
45  // Create a thread pool with given number of workers
46  // - number_of_workers
47  // if 0 then the value of std::thread::hardware_concurrency() is used
48  // else it must be at least 2
49  // - number_of_tasks
50  // number of tasks that are to be expected
51  // - suspend_scheduler
52  // suspend if there is no work to be done
53  FiberThreadPool(std::size_t number_of_workers, std::size_t number_of_tasks = 0,
54  bool suspend_scheduler = true);
55 
56  // Destructor, calls join() if necessary
58 
59  // Post a new task to the pool's queue.
60  // This may block if the task queue is currently full
61  void post(task_t task);
62 
63  // Close the pool. No new tasks can be posted to the pool.
64  // Note: Be sure that all previously posted tasks has been completed before
65  // you call this method.
66  void join();
67 
68  // Join all the fibers that were created.
69  // No new fibers must be created during this call.
70  void join_fibers();
71 
72 private:
73  void create_threads();
74 
75  std::size_t number_of_workers_;
76  bool running_;
77  bool suspend_scheduler_;
78  std::unique_ptr<boost::fibers::buffered_channel<task_t>> task_queue_;
79  std::unique_ptr<boost::fibers::barrier> worker_barrier_;
80  std::vector<std::thread> worker_threads_;
81  std::shared_ptr<pool_ctx> pool_ctx_;
82 };
83 
84 } // namespace encrypto::motion
85 
86 #endif // FIBER_THREAD_POOL_HPP
fiber_thread_pool.hpp
pooled_work_stealing::create_pool_ctx
static std::shared_ptr< pool_ctx > create_pool_ctx(std::uint32_t, bool=false)
Definition: pooled_work_stealing.cpp:37
encrypto::motion::FiberThreadPool::join_fibers
void join_fibers()
encrypto::motion::FiberThreadPool::task_t
std::function< void()> task_t
Definition: fiber_thread_pool.hpp:43
encrypto::motion::FiberThreadPool::post
void post(task_t task)
Definition: fiber_thread_pool.cpp:148
boost
Definition: fiber_thread_pool.hpp:29
encrypto::motion::kFiberStackAllocator
constexpr FiberStackAllocator kFiberStackAllocator
Definition: constants.h:84
encrypto::motion::FiberStackAllocator::kFixedSize
@ kFixedSize
pool_ctx
Definition: pooled_work_stealing.cpp:21
locked_fiber_queue.h
encrypto::motion::ThreadSetName
void ThreadSetName(std::thread &thread, const std::string &name)
Definition: thread.cpp:29
encrypto::motion::worker_fctn
static void worker_fctn(std::shared_ptr< pool_ctx > pool_ctx, boost::fibers::buffered_channel< FiberThreadPool::task_t > &task_queue, boost::fibers::barrier &barrier)
Definition: fiber_thread_pool.cpp:70
singleton_pooled_fixedsize_stack.hpp
boost::fibers
Definition: fiber_thread_pool.hpp:29
encrypto::motion::LockedFiberQueue::enqueue
void enqueue(const T &item)
Definition: locked_fiber_queue.h:72
thread.h
encrypto::motion::FiberThreadPool
Definition: fiber_thread_pool.hpp:41
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::FiberThreadPool::FiberThreadPool
FiberThreadPool(std::size_t number_of_workers, std::size_t number_of_tasks=0, bool suspend_scheduler=true)
Definition: fiber_thread_pool.cpp:44
encrypto::motion::FiberThreadPool::~FiberThreadPool
~FiberThreadPool()
Definition: fiber_thread_pool.cpp:62
encrypto::motion::FiberStackAllocator::kPooledFixedSize
@ kPooledFixedSize
boost::fibers::buffered_channel
Definition: fiber_thread_pool.hpp:33
pooled_work_stealing.hpp
encrypto::motion::LockedFiberQueue::dequeue
std::optional< T > dequeue() noexcept
Definition: locked_fiber_queue.h:97
encrypto::motion::FiberStackAllocator::kProtectedFixedSize
@ kProtectedFixedSize
encrypto::motion::kFiberStackSize
constexpr std::size_t kFiberStackSize
Definition: constants.h:71
encrypto::motion::LockedFiberQueue::close
void close() noexcept
Definition: locked_fiber_queue.h:61
encrypto::motion::FiberThreadPool::join
void join()
Definition: fiber_thread_pool.cpp:140
encrypto::motion::LockedFiberQueue
Definition: locked_fiber_queue.h:40
constants.h
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36