Botan 3.14.1
Crypto and TLS for C&
thread_pool.h
Go to the documentation of this file.
1/*
2* (C) 2019 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_THREAD_POOL_H_
8#define BOTAN_THREAD_POOL_H_
9
10#include <botan/types.h>
11#include <botan/work_executor.h>
12#include <functional>
13#include <future>
14#include <memory>
15#include <mutex>
16#include <optional>
17#include <type_traits>
18#include <utility>
19
20namespace Botan {
21
22/**
23* A future which waits for its work to finish when destroyed, so that work
24* referencing the enclosing frame cannot outlive it
25*/
26template <typename T>
27class Joining_Future final {
28 public:
29 explicit Joining_Future(std::future<T> future) : m_future(std::move(future)) {}
30
32 if(m_future.valid()) {
33 m_future.wait();
34 }
35 }
36
39
40 Joining_Future(Joining_Future&&) noexcept = default;
41
42 Joining_Future& operator=(Joining_Future&& other) noexcept {
43 if(this != &other) {
44 if(m_future.valid()) {
45 m_future.wait();
46 }
47 m_future = std::move(other.m_future);
48 }
49 return *this;
50 }
51
52 decltype(auto) get() { return m_future.get(); }
53
54 void wait() const { m_future.wait(); }
55
56 bool valid() const noexcept { return m_future.valid(); }
57
58 private:
59 std::future<T> m_future;
60};
61
62/**
63* Thread pool for computationally bound tasks
64*
65* Callers queue fork-join work which is executed in arbitrary order.
66* Do not use this interface for any kind of IO scheduling.
67*/
69 public:
70 /**
71 * Return an instance to a shared thread pool
72 */
74
75 /**
76 * Initialize a thread pool with some number of threads
77 * @param pool_size number of threads in the pool, if 0
78 * then some default value is chosen. If the optional
79 * is nullopt then the thread pool is disabled; all
80 * work is executed immediately when queued.
81 *
82 * The threads are not started until work is first queued.
83 */
84 explicit Thread_Pool(std::optional<size_t> pool_size);
85
86 /**
87 * Initialize a thread pool with some number of threads
88 * @param pool_size number of threads in the pool, if 0
89 * then some default value is chosen.
90 */
91 explicit Thread_Pool(size_t pool_size = 0) : Thread_Pool(std::optional<size_t>(pool_size)) {}
92
94
95 void shutdown();
96
97 /**
98 * Return the number of threads available to run queued work
99 *
100 * With an executor installed this is the max_concurrency it reported
101 * when it was installed. Otherwise it is the number of threads in the
102 * pool, where zero means the pool is disabled and all work runs in the
103 * calling thread. Returns zero once the pool has shut down.
104 */
105 size_t worker_count() const;
106
107 /**
108 * Install an executor which runs queued work in place of the pool's threads
109 *
110 * May be called at most once; the executor then remains in use for the
111 * lifetime of the pool. Any threads already started finish their queued
112 * work and exit.
113 *
114 * Throws Invalid_State if an executor is already set, or if called from
115 * within work run by this pool.
116 */
117 void set_executor(std::shared_ptr<Work_Executor> executor);
118
119 Thread_Pool(const Thread_Pool&) = delete;
121
124
125 /*
126 * Enqueue some work; the thunk must not throw
127 *
128 * @warning depending on pool state, the work may be executed directly
129 * in the calling thread.
130 */
131 void queue_thunk(const std::function<void()>& work);
132
133 /**
134 * Queue fork-join work which will be executed in arbitrary order
135 */
136 template <class F, class... Args>
137 auto run(F&& f, Args&&... args) -> Joining_Future<std::invoke_result_t<F, Args...>> {
138 using return_type = std::invoke_result_t<F, Args...>;
139
140 const auto future_work = std::bind(std::forward<F>(f), std::forward<Args>(args)...); // NOLINT(*-avoid-bind)
141 const auto task = std::make_shared<std::packaged_task<return_type()>>(future_work);
142 auto future_result = task->get_future();
143 queue_thunk([task]() { (*task)(); });
144 // Wrapped only once queued; otherwise unwinding would wait on a task
145 // still owned by this frame
146 return Joining_Future<return_type>(std::move(future_result));
147 }
148
149 private:
150 mutable std::mutex m_mutex;
151 // The library's own pool until set_executor replaces it; null once shut down
152 std::shared_ptr<Work_Executor> m_executor;
153 size_t m_worker_count;
154 bool m_executor_was_set;
155};
156
157} // namespace Botan
158
159#endif
#define BOTAN_TEST_API
Definition api.h:41
bool valid() const noexcept
Definition thread_pool.h:56
Joining_Future(Joining_Future &&) noexcept=default
Joining_Future & operator=(const Joining_Future &)=delete
decltype(auto) get()
Definition thread_pool.h:52
Joining_Future(std::future< T > future)
Definition thread_pool.h:29
Joining_Future(const Joining_Future &)=delete
Thread_Pool(Thread_Pool &&)=delete
Thread_Pool & operator=(Thread_Pool &&)=delete
Thread_Pool(std::optional< size_t > pool_size)
auto run(F &&f, Args &&... args) -> Joining_Future< std::invoke_result_t< F, Args... > >
Thread_Pool(size_t pool_size=0)
Definition thread_pool.h:91
void queue_thunk(const std::function< void()> &work)
static Thread_Pool & global_instance()
Thread_Pool & operator=(const Thread_Pool &)=delete
Thread_Pool(const Thread_Pool &)=delete