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#pragma once
6
7#include <xrpl/beast/unit_test/suite.h>
8
9#include <functional>
10#include <thread>
11#include <utility>
12
13namespace beast {
14namespace unit_test {
15
17class thread
18{
19private:
20 suite* s_ = nullptr;
22
23public:
25 using native_handle_type = std::thread::native_handle_type;
26
27 thread() = default;
28 thread(thread const&) = delete;
29 thread&
30 operator=(thread const&) = delete;
31
32 thread(thread&& other) : s_(other.s_), t_(std::move(other.t_))
33 {
34 }
35
36 thread&
38 {
39 s_ = other.s_;
40 t_ = std::move(other.t_);
41 return *this;
42 }
43
44 template <class F, class... Args>
45 explicit thread(suite& s, F&& f, Args&&... args) : s_(&s)
46 {
47 std::function<void(void)> b = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
48 t_ = std::thread(&thread::run, this, std::move(b));
49 }
50
51 bool
52 joinable() const
53 {
54 return t_.joinable();
55 }
56
58 get_id() const
59 {
60 return t_.get_id();
61 }
62
63 static unsigned
68
69 void
71 {
72 t_.join();
74 }
75
76 void
78 {
79 t_.detach();
80 }
81
82 void
83 swap(thread& other)
84 {
85 std::swap(s_, other.s_);
86 std::swap(t_, other.t_);
87 }
88
89private:
90 void
91 run(std::function<void(void)> f)
92 {
93 try
94 {
95 f();
96 }
97 catch (suite::abort_exception const&)
98 {
99 }
100 catch (std::exception const& e)
101 {
102 s_->fail("unhandled exception: " + std::string(e.what()));
103 }
104 catch (...)
105 {
106 s_->fail("unhandled exception");
107 }
108 }
109};
110
111} // namespace unit_test
112} // namespace beast
T bind(T... args)
A testsuite class.
Definition suite.h:51
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:516
Replacement for std::thread that handles exceptions in unit tests.
Definition thread.h:18
bool joinable() const
Definition thread.h:52
std::thread::id get_id() const
Definition thread.h:58
thread(thread &&other)
Definition thread.h:32
void swap(thread &other)
Definition thread.h:83
thread & operator=(thread &&other)
Definition thread.h:37
thread(suite &s, F &&f, Args &&... args)
Definition thread.h:45
std::thread::native_handle_type native_handle_type
Definition thread.h:25
static unsigned hardware_concurrency() noexcept
Definition thread.h:64
void run(std::function< void(void)> f)
Definition thread.h:91
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)