llvm-project/libcxx/test/std/thread/futures/futures.unique_future/wait_until.pass.cpp

130 lines
4.0 KiB
C++
Raw Normal View History

[libcxx] Mark most test/std/future tests as UNSUPPORTED in C++03 Summary: This patch marks *most* tests for `std::promise`, `std::future` and `std::shared_future` as unsupported in C++03. These tests fail in C++03 mode because they attempt to copy a `std::future` even though it is a `MoveOnly` type. AFAIK the missing move-semantics in `std::future` is the only reason these tests fail but without move semantics these classes are useless. For example even though `std::promise::set_value` and `std::promise::set_exception(...)` work in C++03 `std::promise` is still useless because we cannot call `std::promise::get_future(...)`. It might be possible to hack `std::move(...)` like we do for `std::unique_ptr` to make the move semantics work but I don't think it is worth the effort. Instead I think we should leave the `<future>` header as-is and mark the failing tests as `UNSUPPORTED`. I don't believe there are any users of `std::future` or `std::promise` in C++03 because they are so unusable. Therefore I am not concerned about losing test coverage and possibly breaking users. However because there are still parts of `<future>` that work in C++03 it would be wrong to `#ifdef` out the entire header. @mclow.lists Should we take further steps to prevent the use of `std::promise`, `std::future` and `std::shared_future` in C++03? Note: This patch also cleans up the tests and converts them to use `support/test_allocator.h` instead of a duplicate class in `test/std/futures/test_allocator.h`. Reviewers: mclow.lists Subscribers: vsk, mclow.lists, cfe-commits Differential Revision: http://reviews.llvm.org/D12135 llvm-svn: 246271
2015-08-28 13:06:04 +08:00
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
[libcxx] Mark most test/std/future tests as UNSUPPORTED in C++03 Summary: This patch marks *most* tests for `std::promise`, `std::future` and `std::shared_future` as unsupported in C++03. These tests fail in C++03 mode because they attempt to copy a `std::future` even though it is a `MoveOnly` type. AFAIK the missing move-semantics in `std::future` is the only reason these tests fail but without move semantics these classes are useless. For example even though `std::promise::set_value` and `std::promise::set_exception(...)` work in C++03 `std::promise` is still useless because we cannot call `std::promise::get_future(...)`. It might be possible to hack `std::move(...)` like we do for `std::unique_ptr` to make the move semantics work but I don't think it is worth the effort. Instead I think we should leave the `<future>` header as-is and mark the failing tests as `UNSUPPORTED`. I don't believe there are any users of `std::future` or `std::promise` in C++03 because they are so unusable. Therefore I am not concerned about losing test coverage and possibly breaking users. However because there are still parts of `<future>` that work in C++03 it would be wrong to `#ifdef` out the entire header. @mclow.lists Should we take further steps to prevent the use of `std::promise`, `std::future` and `std::shared_future` in C++03? Note: This patch also cleans up the tests and converts them to use `support/test_allocator.h` instead of a duplicate class in `test/std/futures/test_allocator.h`. Reviewers: mclow.lists Subscribers: vsk, mclow.lists, cfe-commits Differential Revision: http://reviews.llvm.org/D12135 llvm-svn: 246271
2015-08-28 13:06:04 +08:00
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03
// <future>
// class future<R>
// template <class Clock, class Duration>
// future_status
// wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
#include <future>
#include <atomic>
#include <cassert>
enum class WorkerThreadState { Uninitialized, AllowedToRun, Exiting };
typedef std::chrono::milliseconds ms;
std::atomic<WorkerThreadState> thread_state(WorkerThreadState::Uninitialized);
void set_worker_thread_state(WorkerThreadState state)
{
thread_state.store(state, std::memory_order_relaxed);
}
void wait_for_worker_thread_state(WorkerThreadState state)
{
while (thread_state.load(std::memory_order_relaxed) != state);
}
void func1(std::promise<int> p)
{
wait_for_worker_thread_state(WorkerThreadState::AllowedToRun);
p.set_value(3);
set_worker_thread_state(WorkerThreadState::Exiting);
}
int j = 0;
void func3(std::promise<int&> p)
{
wait_for_worker_thread_state(WorkerThreadState::AllowedToRun);
j = 5;
p.set_value(j);
set_worker_thread_state(WorkerThreadState::Exiting);
}
void func5(std::promise<void> p)
{
wait_for_worker_thread_state(WorkerThreadState::AllowedToRun);
p.set_value();
set_worker_thread_state(WorkerThreadState::Exiting);
}
int main()
{
typedef std::chrono::high_resolution_clock Clock;
{
typedef int T;
std::promise<T> p;
std::future<T> f = p.get_future();
std::thread(func1, std::move(p)).detach();
assert(f.valid());
assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
assert(f.valid());
// allow the worker thread to produce the result and wait until the worker is done
set_worker_thread_state(WorkerThreadState::AllowedToRun);
wait_for_worker_thread_state(WorkerThreadState::Exiting);
assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
assert(f.valid());
Clock::time_point t0 = Clock::now();
f.wait();
Clock::time_point t1 = Clock::now();
assert(f.valid());
assert(t1-t0 < ms(5));
}
{
typedef int& T;
std::promise<T> p;
std::future<T> f = p.get_future();
std::thread(func3, std::move(p)).detach();
assert(f.valid());
assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
assert(f.valid());
// allow the worker thread to produce the result and wait until the worker is done
set_worker_thread_state(WorkerThreadState::AllowedToRun);
wait_for_worker_thread_state(WorkerThreadState::Exiting);
assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
assert(f.valid());
Clock::time_point t0 = Clock::now();
f.wait();
Clock::time_point t1 = Clock::now();
assert(f.valid());
assert(t1-t0 < ms(5));
}
{
typedef void T;
std::promise<T> p;
std::future<T> f = p.get_future();
std::thread(func5, std::move(p)).detach();
assert(f.valid());
assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
assert(f.valid());
// allow the worker thread to produce the result and wait until the worker is done
set_worker_thread_state(WorkerThreadState::AllowedToRun);
wait_for_worker_thread_state(WorkerThreadState::Exiting);
assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
assert(f.valid());
Clock::time_point t0 = Clock::now();
f.wait();
Clock::time_point t1 = Clock::now();
assert(f.valid());
assert(t1-t0 < ms(5));
}
}