rippled
Loading...
Searching...
No Matches
thread.h
1// Distributed under the Boost Software License, Version 1.0. (See accompanying
2// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
3//
4
5#ifndef BEAST_UNIT_TEST_THREAD_HPP
6#define BEAST_UNIT_TEST_THREAD_HPP
7
8#include <xrpl/beast/unit_test/suite.h>
9
10#include <functional>
11#include <thread>
12#include <utility>
13
14namespace beast {
15namespace unit_test {
16
18class thread
19{
20private:
21 suite* s_ = nullptr;
23
24public:
26 using native_handle_type = std::thread::native_handle_type;
27
28 thread() = default;
29 thread(thread const&) = delete;
30 thread&
31 operator=(thread const&) = delete;
32
33 thread(thread&& other) : s_(other.s_), t_(std::move(other.t_))
34 {
35 }
36
37 thread&
39 {
40 s_ = other.s_;
41 t_ = std::move(other.t_);
42 return *this;
43 }
44
45 template <class F, class... Args>
46 explicit thread(suite& s, F&& f, Args&&... args) : s_(&s)
47 {
48 std::function<void(void)> b =
50 t_ = std::thread(&thread::run, this, std::move(b));
51 }
52
53 bool
54 joinable() const
55 {
56 return t_.joinable();
57 }
58
60 get_id() const
61 {
62 return t_.get_id();
63 }
64
65 static unsigned
70
71 void
73 {
74 t_.join();
76 }
77
78 void
80 {
81 t_.detach();
82 }
83
84 void
85 swap(thread& other)
86 {
87 std::swap(s_, other.s_);
88 std::swap(t_, other.t_);
89 }
90
91private:
92 void
93 run(std::function<void(void)> f)
94 {
95 try
96 {
97 f();
98 }
99 catch (suite::abort_exception const&)
100 {
101 }
102 catch (std::exception const& e)
103 {
104 s_->fail("unhandled exception: " + std::string(e.what()));
105 }
106 catch (...)
107 {
108 s_->fail("unhandled exception");
109 }
110 }
111};
112
113} // namespace unit_test
114} // namespace beast
115
116#endif
T bind(T... args)
A testsuite class.
Definition suite.h:52
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:530
Replacement for std::thread that handles exceptions in unit tests.
Definition thread.h:19
bool joinable() const
Definition thread.h:54
std::thread::id get_id() const
Definition thread.h:60
thread(thread &&other)
Definition thread.h:33
void swap(thread &other)
Definition thread.h:85
thread & operator=(thread &&other)
Definition thread.h:38
thread(suite &s, F &&f, Args &&... args)
Definition thread.h:46
std::thread::native_handle_type native_handle_type
Definition thread.h:26
static unsigned hardware_concurrency() noexcept
Definition thread.h:66
void run(std::function< void(void)> f)
Definition thread.h:93
thread(thread const &)=delete
thread & operator=(thread const &)=delete
T detach(T... args)
T get_id(T... args)
T hardware_concurrency(T... args)
T is_same_v
T join(T... args)
T joinable(T... args)
STL namespace.
T swap(T... args)
T what(T... args)