Iros
 
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/assert/prelude.h"
6#include "di/meta/util.h"
7#include "di/util/prelude.h"
8#include "dius/config.h"
9#include "dius/error.h"
10#include "steady_clock.h"
11
12#include DIUS_PLATFORM_PATH(thread.h)
13
14namespace dius {
15namespace this_thread {
16 void sleep_for(di::Nanoseconds duration);
17 void sleep_until(SteadyClock::TimePoint time_point);
18}
19
25class Thread {
26private:
27 static auto do_start(di::Function<void()>) -> di::Result<Thread>;
28
30 : m_platform_thread(di::move(platform_thread)) {}
31
32public:
34
36 requires(di::concepts::InvocableTo<F, void, Args...>)
37 static auto create(F&& function, Args&&... args) -> di::Result<Thread> {
38 auto copied_function = di::bind_front(di::forward<F>(function), di::forward<Args>(args)...);
39 auto erased_function = di::make_function<void()>(di::move(copied_function));
40 return do_start(di::move(erased_function));
41 }
42
43 Thread() = default;
44
45 Thread(Thread const&) = delete;
46 Thread(Thread&& other) : m_platform_thread(di::move(other.m_platform_thread)) {}
47
49
50 auto operator=(Thread const&) -> Thread& = delete;
51 auto operator=(Thread&& other) -> Thread& {
52 ASSERT(!joinable());
53 m_platform_thread = di::move(other.m_platform_thread);
54 return *this;
55 }
56
57 [[nodiscard]] auto joinable() const -> bool { return Id() != get_id(); }
58 [[nodiscard]] auto get_id() const -> Id { return m_platform_thread ? m_platform_thread->id() : Id(); }
59
66 if (!m_platform_thread) {
67 return di::Unexpected(PosixError::InvalidArgument);
68 }
69 auto guard = di::ScopeExit([&] {
70 m_platform_thread.reset();
71 });
72 return m_platform_thread->join();
73 }
74
75private:
76 friend void tag_invoke(di::Tag<di::swap>, Thread& a, Thread& b) {
77 di::swap(a.m_platform_thread, b.m_platform_thread);
78 }
79
81};
82}
#define ASSERT
Definition assert_bool.h:16
Definition function.h:365
Definition scope_exit.h:12
Definition box.h:28
Definition unexpected.h:14
di::TimePoint< SteadyClock > TimePoint
Definition steady_clock.h:13
Thread(Thread const &)=delete
auto joinable() const -> bool
Definition thread.h:57
di::ThreadId Id
Definition thread.h:33
~Thread()
Definition thread.h:48
auto operator=(Thread const &) -> Thread &=delete
friend void tag_invoke(di::Tag< di::swap >, Thread &a, Thread &b)
Definition thread.h:76
Thread(Thread &&other)
Definition thread.h:46
Thread()=default
auto operator=(Thread &&other) -> Thread &
Definition thread.h:51
auto get_id() const -> Id
Definition thread.h:58
static auto create(F &&function, Args &&... args) -> di::Result< Thread >
Definition thread.h:37
auto join() -> di::Result< void >
Wait for the spawned thread's execution to terminate.
Definition thread.h:65
Definition invoke.h:69
Definition util.h:71
Duration< i64, math::Nano > Nanoseconds
Definition duration_literals.h:7
Definition as_bool.h:8
std::thread::id ThreadId
Definition custom.h:31
di::meta::Decay< decltype(T)> Tag
Definition tag_invoke.h:28
Expected< T, Error > Result
Definition result.h:8
Definition zstring_parser.h:9
constexpr auto bind_front(F &&f, Args &&... args)
Definition bind_front.h:68
constexpr auto make_function
Definition function.h:681
constexpr struct di::util::SwapFunction swap
Definition thread.h:15
void sleep_for(di::Nanoseconds duration)
Definition thread.cpp:10
void sleep_until(SteadyClock::TimePoint time_point)
Definition thread.cpp:17
Definition directory_entry.h:11