Files
xahaud/extras/beast/unit_test/thread.hpp
Vinnie Falco d8dea963fa Squashed 'src/beast/' changes from 1b9a714..6d5547a
6d5547a Set version to 1.0.0-b34
6fab138 Fix and tidy up CMake build scripts:
ccefa54 Set version to 1.0.0-b33
32afe41 Set internal state correctly when writing frames:
fe3e20b Add write_frames unit test
578dcd0 Add decorator unit test
aaa3733 Use fwrite return value in file_body
df66165 Require Visual Studio 2015 Update 3 or later
b8e5a21 Set version to 1.0.0-b32
ffb1758 Update CMake scripts for finding packages:
b893749 Remove http Writer suspend and resume feature (API Change):
27864fb Add io_service completion invariants tests
eba05a7 Set version to 1.0.0-b31
484bcef Fix badge markdown in README.md
5663bea Add missing dynabuf_readstream member
0d7a551 Tidy up build settings
0fd4030 Move the handler, don't copy it

git-subtree-dir: src/beast
git-subtree-split: 6d5547a32c50ec95832c4779311502555ab0ee1f
2017-04-20 13:40:52 -07:00

125 lines
2.3 KiB
C++

//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_UNIT_TEST_THREAD_HPP
#define BEAST_UNIT_TEST_THREAD_HPP
#include <beast/unit_test/suite.hpp>
#include <functional>
#include <thread>
#include <utility>
namespace beast {
namespace unit_test {
/** Replacement for std::thread that handles exceptions in unit tests. */
class thread
{
private:
suite* s_ = nullptr;
std::thread t_;
public:
using id = std::thread::id;
using native_handle_type = std::thread::native_handle_type;
thread() = default;
thread(thread const&) = delete;
thread& operator=(thread const&) = delete;
thread(thread&& other)
: s_(other.s_)
, t_(std::move(other.t_))
{
}
thread& operator=(thread&& other)
{
s_ = other.s_;
t_ = std::move(other.t_);
return *this;
}
template<class F, class... Args>
explicit
thread(suite& s, F&& f, Args&&... args)
: s_(&s)
{
std::function<void(void)> b =
std::bind(std::forward<F>(f),
std::forward<Args>(args)...);
t_ = std::thread(&thread::run, this,
std::move(b));
}
bool
joinable() const
{
return t_.joinable();
}
std::thread::id
get_id() const
{
return t_.get_id();
}
static
unsigned
hardware_concurrency() noexcept
{
return std::thread::hardware_concurrency();
}
void
join()
{
t_.join();
s_->propagate_abort();
}
void
detach()
{
t_.detach();
}
void
swap(thread& other)
{
std::swap(s_, other.s_);
std::swap(t_, other.t_);
}
private:
void
run(std::function <void(void)> f)
{
try
{
f();
}
catch(suite::abort_exception const&)
{
}
catch(std::exception const& e)
{
s_->fail("unhandled exception: " +
std::string(e.what()));
}
catch(...)
{
s_->fail("unhandled exception");
}
}
};
} // unit_test
} // beast
#endif