diff options
| author | Brian Gesiak <modocache@gmail.com> | 2019-03-26 17:46:06 +0000 |
|---|---|---|
| committer | Brian Gesiak <modocache@gmail.com> | 2019-03-26 17:46:06 +0000 |
| commit | 57839425aa4802986acbb17392ca697ee76aa633 (patch) | |
| tree | 9e173f9d545ee0dd2a54e5a7fca96a216130cc39 /libcxx/test/std/experimental/task/task.lifetime/task_parameter_lifetime.pass.cpp | |
| parent | 52221d56bcf3d087bb88d44b56c682fd27f59a13 (diff) | |
| download | bcm5719-llvm-57839425aa4802986acbb17392ca697ee76aa633.tar.gz bcm5719-llvm-57839425aa4802986acbb17392ca697ee76aa633.zip | |
[coroutines] Add std::experimental::task<T> type
Summary:
Adds the coroutine `std::experimental::task<T>` 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 <experimental/memory_resource> to <experimental/__memory>
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
Diffstat (limited to 'libcxx/test/std/experimental/task/task.lifetime/task_parameter_lifetime.pass.cpp')
| -rw-r--r-- | libcxx/test/std/experimental/task/task.lifetime/task_parameter_lifetime.pass.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libcxx/test/std/experimental/task/task.lifetime/task_parameter_lifetime.pass.cpp b/libcxx/test/std/experimental/task/task.lifetime/task_parameter_lifetime.pass.cpp new file mode 100644 index 00000000000..bc1bd432244 --- /dev/null +++ b/libcxx/test/std/experimental/task/task.lifetime/task_parameter_lifetime.pass.cpp @@ -0,0 +1,57 @@ +// -*- 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 <experimental/task> +#include <cassert> + +#include "../counted.hpp" +#include "../sync_wait.hpp" + +DEFINE_COUNTED_VARIABLES(); + +void test_parameter_lifetime() +{ + counted::reset(); + + auto f = [](counted c) -> std::experimental::task<std::size_t> + { + co_return c.id(); + }; + + { + auto t = f({}); + + assert(counted::active_instance_count() == 1); + assert(counted::copy_constructor_count() == 0); + assert(counted::move_constructor_count() <= 2); // Ideally <= 1 + + auto id = sync_wait(t); + assert(id == 1); + + assert(counted::active_instance_count() == 1); + assert(counted::copy_constructor_count() == 0); + + // We are relying on C++17 copy-elision when passing the temporary counter + // into f(). Then f() must move the parameter into the coroutine frame by + // calling the move-constructor. This move could also potentially be + // elided by the + assert(counted::move_constructor_count() <= 1); + } + + assert(counted::active_instance_count() == 0); +} + +int main() +{ + test_parameter_lifetime(); + return 0; +} |

