26#include <seastar/core/thread_impl.hh>
27#include <seastar/core/future.hh>
28#include <seastar/core/do_with.hh>
33#include <seastar/util/std-compat.hh>
34#include <seastar/util/modules.hh>
36#include <boost/intrusive/list.hpp>
74SEASTAR_MODULE_EXPORT_BEGIN
76class thread_attributes;
81 std::optional<seastar::scheduling_group> sched_group;
83 size_t stack_size = 0;
85SEASTAR_MODULE_EXPORT_END
88extern thread_local jmp_buf_link g_unthreaded_context;
93class thread_context final :
private task {
94 struct stack_deleter {
95 void operator()(
char *ptr)
const noexcept;
97 stack_deleter(
int valgrind_id);
99 using stack_holder = std::unique_ptr<char[], stack_deleter>;
102 noncopyable_function<void ()> _func;
103 jmp_buf_link _context;
105 bool _joined =
false;
107 boost::intrusive::list_member_hook<> _all_link;
108 using all_thread_list = boost::intrusive::list<thread_context,
109 boost::intrusive::member_hook<thread_context, boost::intrusive::list_member_hook<>,
110 &thread_context::_all_link>,
111 boost::intrusive::constant_time_size<false>>;
113 static thread_local all_thread_list _all_threads;
115 static void s_main(
int lo,
int hi);
116 void setup(
size_t stack_size);
118 stack_holder make_stack(
size_t stack_size);
119 virtual void run_and_dispose() noexcept override;
121 thread_context(thread_attributes attr, noncopyable_function<
void ()> func);
125 bool should_yield() const;
128 task* waiting_task() noexcept
override {
return _done.waiting_task(); }
130 friend void thread_impl::switch_in(thread_context*);
131 friend void thread_impl::switch_out(thread_context*);
132 friend scheduling_group thread_impl::sched_group(
const thread_context*);
145 std::unique_ptr<thread_context> _context;
146 static thread_local thread* _current;
155 template <
typename Func>
162 template <
typename Func>
171 ~thread() { assert(!_context || _context->_joined); }
194 static bool running_in_thread() {
195 return thread_impl::get() !=
nullptr;
199template <
typename Func>
202 : _context(
std::make_unique<thread_context>(
std::move(attr),
std::move(func))) {
205template <
typename Func>
214 _context->_joined =
true;
215 return _context->_done.get_future();
218SEASTAR_MODULE_EXPORT_BEGIN
242template <
typename Func,
typename... Args>
244futurize_t<std::invoke_result_t<Func, Args...>>
246 using return_type = std::invoke_result_t<Func, Args...>;
250 std::tuple<Args...> args;
256 auto wp = std::make_unique<work>(work{std::move(attr), std::forward<Func>(func), std::forward_as_tuple(std::forward<Args>(args)...)});
258 auto ret = w.pr.get_future();
259 w.th =
thread(std::move(w.attr), [&w] {
260 futurize<return_type>::apply(std::move(w.func), std::move(w.args)).forward_to(std::move(w.pr));
262 return w.th.join().then([ret = std::move(ret)] ()
mutable {
263 return std::move(ret);
264 }).
finally([wp = std::move(wp)] {});
279template <
typename Func,
typename... Args>
281futurize_t<std::invoke_result_t<Func, Args...>>
282async(Func&& func, Args&&... args)
noexcept {
287SEASTAR_MODULE_EXPORT_END
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
thread - stateful thread of execution
Definition: thread.hh:144
~thread()
Destroys a thread object.
Definition: thread.hh:171
thread(thread &&x) noexcept=default
Moves a thread object.
static void maybe_yield()
Yield if this thread ought to call yield() now.
static void yield()
Voluntarily defer execution of current thread.
thread()=default
Constructs a thread object that does not represent a thread of execution.
static bool should_yield()
Checks whether this thread ought to call yield() now.
thread & operator=(thread &&x) noexcept=default
Move-assigns a thread object.
future yield() noexcept
Returns a future which is not ready but is scheduled to resolve soon.
Class that holds attributes controling the behavior of a thread.
Definition: thread.hh:79
futurize_t< std::invoke_result_t< Func, Args... > > async(thread_attributes attr, Func &&func, Args &&... args) noexcept
Definition: thread.hh:245
future join()
Waits for thread execution to terminate.
Definition: thread.hh:213
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Converts a type to a future type, if it isn't already.
Definition: future.hh:1853