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