added <gsl_thread> and thread_tests.cpp

This commit is contained in:
Galik 2017-03-28 09:13:21 +01:00
parent 80bcf224a6
commit a423e75f49
2 changed files with 215 additions and 0 deletions

103
include/gsl/gsl_thread Normal file
View File

@ -0,0 +1,103 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2017 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
#ifndef GSL_THREAD_H
#define GSL_THREAD_H
#include <thread>
#ifdef _MSC_VER
#pragma warning(push)
// turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant
// blanket turn off warnings from CppCoreCheck for now
// so people aren't annoyed by them when running the tool.
// more targeted suppressions will be added in a future update to the GSL
#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
#endif // _MSC_VER
namespace gsl
{
class detached_thread
{
public:
detached_thread(detached_thread const&) = delete;
template<typename Callable, typename... Args>
explicit detached_thread(Callable&& f, Args&&... args)
: t(std::forward<Callable>(f), std::forward<Args>(args)...) { t.detach(); }
private:
std::thread t;
};
class raii_thread
{
friend void swap(raii_thread& t1, raii_thread& t2) noexcept;
public:
raii_thread() noexcept = default;
raii_thread(raii_thread const&) = delete;
raii_thread(raii_thread&& other): t(std::move(other.t)) {}
raii_thread(std::thread const&) = delete;
raii_thread(std::thread&& other) noexcept: t(std::move(other)) {}
raii_thread& operator=(raii_thread const&) = delete;
raii_thread& operator=(raii_thread&& other) noexcept { t = std::move(other.t); return *this; }
raii_thread& operator=(std::thread const&) = delete;
raii_thread& operator=(std::thread&& other) noexcept { t = std::move(other); return *this; }
template<typename Callable, typename... Args>
explicit raii_thread(Callable&& f, Args&&... args)
: t(std::forward<Callable>(f), std::forward<Args>(args)...) {}
~raii_thread() { if(t.joinable()) t.join(); }
bool joinable() const { return t.joinable(); }
std::thread::id get_id() const noexcept { return t.get_id(); }
std::thread::native_handle_type native_handle() { return t.native_handle(); }
void join() { t.join(); }
void swap(raii_thread& other) noexcept { using std::swap; swap(t, other.t); }
private:
std::thread t;
};
void swap(raii_thread& t1, raii_thread& t2) noexcept
{
using std::swap;
swap(t1.t, t2.t);
}
} // namespace gsl
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
#endif // GSL_THREAD_H

112
tests/thread_tests.cpp Normal file
View File

@ -0,0 +1,112 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <mutex>
#include <random>
#include <string>
#include <gsl/gsl_assert>
#include <gsl/gsl_thread>
namespace
{
inline
int random_number(int from, int to)
{
static std::mutex mtx;
static std::mt19937 mt{std::random_device{}()};
std::lock_guard<std::mutex> lock(mtx);
return std::uniform_int_distribution<int>{from, to}(mt);
}
SUITE(thread_tests)
{
class scope_test_fixture
{
public:
scope_test_fixture(): v1(fill( 0, 49)), v2(fill(50, 99)), v3(fill( 0, 99)) {}
std::mutex mtx;
const std::vector<int> v1;
const std::vector<int> v2;
const std::vector<int> v3;
private:
std::vector<int> fill(int from, int to)
{
Ensures(from < to);
std::vector<int> v(std::size_t(to - from));
std::iota(std::begin(v), std::end(v), from);
return v;
}
};
TEST(same_scope)
{
scope_test_fixture fixture;
std::vector<int> v;
gsl::raii_thread t1{[&]{
for(auto i: fixture.v1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
}
}};
for(auto i: fixture.v2)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
}
CHECK(v != fixture.v3);
}
TEST(different_scope)
{
scope_test_fixture fixture;
std::vector<int> v;
{
gsl::raii_thread t1{[&]{
for(auto i: fixture.v1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
}
}};
}
for(auto i: fixture.v2)
{
std::this_thread::sleep_for(std::chrono::milliseconds(random_number(0, 1)));
std::lock_guard<std::mutex> lock{fixture.mtx};
v.push_back(i);
}
CHECK(v == fixture.v3);
}
}
int main(int, const char* []) { return UnitTest::RunAllTests(); }