]> git.proxmox.com Git - ceph.git/blame - ceph/src/Beast/extras/beast/unit_test/thread.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / Beast / extras / beast / unit_test / thread.hpp
CommitLineData
7c673cae
FG
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 <beast/unit_test/suite.hpp>
12#include <functional>
13#include <thread>
14#include <utility>
15
16namespace beast {
17namespace unit_test {
18
19/** Replacement for std::thread that handles exceptions in unit tests. */
20class thread
21{
22private:
23 suite* s_ = nullptr;
24 std::thread t_;
25
26public:
27 using id = std::thread::id;
28 using native_handle_type = std::thread::native_handle_type;
29
30 thread() = default;
31 thread(thread const&) = delete;
32 thread& operator=(thread const&) = delete;
33
34 thread(thread&& other)
35 : s_(other.s_)
36 , t_(std::move(other.t_))
37 {
38 }
39
40 thread& operator=(thread&& other)
41 {
42 s_ = other.s_;
43 t_ = std::move(other.t_);
44 return *this;
45 }
46
47 template<class F, class... Args>
48 explicit
49 thread(suite& s, F&& f, Args&&... args)
50 : s_(&s)
51 {
52 std::function<void(void)> b =
53 std::bind(std::forward<F>(f),
54 std::forward<Args>(args)...);
55 t_ = std::thread(&thread::run, this,
56 std::move(b));
57 }
58
59 bool
60 joinable() const
61 {
62 return t_.joinable();
63 }
64
65 std::thread::id
66 get_id() const
67 {
68 return t_.get_id();
69 }
70
71 static
72 unsigned
73 hardware_concurrency() noexcept
74 {
75 return std::thread::hardware_concurrency();
76 }
77
78 void
79 join()
80 {
81 t_.join();
82 s_->propagate_abort();
83 }
84
85 void
86 detach()
87 {
88 t_.detach();
89 }
90
91 void
92 swap(thread& other)
93 {
94 std::swap(s_, other.s_);
95 std::swap(t_, other.t_);
96 }
97
98private:
99 void
100 run(std::function <void(void)> f)
101 {
102 try
103 {
104 f();
105 }
106 catch(suite::abort_exception const&)
107 {
108 }
109 catch(std::exception const& e)
110 {
111 s_->fail("unhandled exception: " +
112 std::string(e.what()));
113 }
114 catch(...)
115 {
116 s_->fail("unhandled exception");
117 }
118 }
119};
120
121} // unit_test
122} // beast
123
124#endif