From 57839425aa4802986acbb17392ca697ee76aa633 Mon Sep 17 00:00:00 2001 From: Brian Gesiak Date: Tue, 26 Mar 2019 17:46:06 +0000 Subject: [coroutines] Add std::experimental::task type Summary: Adds the coroutine `std::experimental::task` type described in proposal P1056R0. See https://wg21.link/P1056R0. This implementation allows customization of the allocator used to allocate the coroutine frame by passing std::allocator_arg as the first argument, followed by the allocator to use. This supports co_awaiting the same task multiple times. The second and subsequent times it returns a reference to the already-computed value. This diff also adds some implementations of other utilities that have potential for standardization as helpers within the test/... area: - `sync_wait(awaitable)` - See P1171R0 - `manual_reset_event` Move the definition of the __aligned_allocation_size helper function from to so it can be more widely used without pulling in memory_resource. Outstanding work: - Use C++14 keywords directly rather than macro versions eg. use `noexcept` instead of `_NOEXCEPT`). - Add support for overaligned coroutine frames. This may need wording in the Coroutines TS to support passing the extra `std::align_val_t`. - Eliminate use of `if constexpr` if we want it to compile under C++14. Patch by @lewissbaker (Lewis Baker). llvm-svn: 357010 --- .../task/task.basic/task_of_void.pass.cpp | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 libcxx/test/std/experimental/task/task.basic/task_of_void.pass.cpp (limited to 'libcxx/test/std/experimental/task/task.basic/task_of_void.pass.cpp') diff --git a/libcxx/test/std/experimental/task/task.basic/task_of_void.pass.cpp b/libcxx/test/std/experimental/task/task.basic/task_of_void.pass.cpp new file mode 100644 index 00000000000..de860b5db6a --- /dev/null +++ b/libcxx/test/std/experimental/task/task.basic/task_of_void.pass.cpp @@ -0,0 +1,96 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include "../manual_reset_event.hpp" +#include "../sync_wait.hpp" + +#include +#include + +namespace coro = std::experimental::coroutines_v1; + +static bool has_f_executed = false; + +static coro::task f() +{ + has_f_executed = true; + co_return; +} + +static void test_coroutine_executes_lazily() +{ + coro::task t = f(); + assert(!has_f_executed); + coro::sync_wait(t); + assert(has_f_executed); +} + +static std::optional last_value_passed_to_g; + +static coro::task g(int a) +{ + last_value_passed_to_g = a; + co_return; +} + +void test_coroutine_accepts_arguments() +{ + auto t = g(123); + assert(!last_value_passed_to_g); + coro::sync_wait(t); + assert(last_value_passed_to_g); + assert(*last_value_passed_to_g == 123); +} + +int shared_value = 0; +int read_value = 0; + +coro::task consume_async(manual_reset_event& event) +{ + co_await event; + read_value = shared_value; +} + +void produce(manual_reset_event& event) +{ + shared_value = 101; + event.set(); +} + +void test_async_completion() +{ + manual_reset_event e; + std::thread t1{ [&e] + { + sync_wait(consume_async(e)); + }}; + + assert(read_value == 0); + + std::thread t2{ [&e] { produce(e); }}; + + t1.join(); + + assert(read_value == 101); + + t2.join(); +} + +int main() +{ + test_coroutine_executes_lazily(); + test_coroutine_accepts_arguments(); + test_async_completion(); + + return 0; +} -- cgit v1.2.3