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