Commit graph

83 commits

Author SHA1 Message Date
Lewis Baker
72d08e089a Modify variadic when_all_ready() to support arbitrary awaitables.
Now yields a tuple<when_all_task<T>...> when awaited which allows
the caller to retrieve individual results by calling .result()
without needing to co_await again. This should allow usage of the
result in non-coroutine contexts.
2017-10-01 21:50:16 +10:30
Lewis Baker
b4c78dc409 Remove workaround for Clang bug in when_all_awaitable2.
The bug where Clang was complaining about invalid return-type
of 'auto await_suspend()' when return-type is deduced to bool
should now be fixed.
2017-10-01 21:30:13 +10:30
Lewis Baker
414f3eb245 Tweak and document sync_wait() workaround for bug in MSVC.
Add generic implementation for use under Clang.

Use std::forward<> rather than static_cast<> to avoid warnings
about cast from lvalue to rvalue under MSVC in some cases.
2017-10-01 21:27:09 +10:30
Lewis Baker
5ea774d1f9 Make more functions work with arbitrary awaitables.
- Change fmap, schedule_on, resume_on, make_shared_task work with
  arbitrary awaitables.
- Add make_task to wrap an arbitrary awaitable in a task<T>.
- Remove specialisation of fmap() for task<T>, shared_task<T> as
  the generalised version obsoletes it.
2017-09-13 08:26:43 +09:30
Lewis Baker
a64b0e145d Add detail::remove_rvalue_reference<T> template metafunction. 2017-09-13 07:12:57 +09:30
Lewis Baker
f100b25869 Add cppcoro::is_awaitable_v<T> variable template. 2017-09-13 07:10:03 +09:30
Lewis Baker
2f8469bd96 Move is_awaiter<T> into cppcoro::detail namespace.
It is just an implementation detail of is_awaitable<T> and
shouldn't form part of the public API.
2017-09-11 21:16:59 +09:30
Lewis Baker
d0383c1cb3 Add noexcept to when_all_awaitable2<>::await_suspend(). 2017-09-06 21:32:27 +09:30
Lewis Baker
6bde4d56d1 Simplify member type for result pointer in when_all_task_promise. 2017-09-06 21:30:12 +09:30
Lewis Baker
e25cb7d38c Fix when_all_awaitable2 move constructor. 2017-09-06 21:21:35 +09:30
Lewis Baker
8c5d063957 Support variadic when_all() with a single void-returning awaitable. 2017-09-06 21:20:04 +09:30
Lewis Baker
7c5dcdba18 Fix when_all() implementation under MSVC.
I was relying on coroutine_handle() default constructor initialising
the handle to nullptr in when_all_awaitable_counter constructor.
But this is not the case with the current MSVC implementation.
Now explicitly initialise handle to nullptr.
2017-09-04 21:52:53 +09:30
Lewis Baker
2080b54c8c Added workaround for issue returning rvalue-references from co_await.
Now return prvalue rather than xvalue when co_awaiting a task<T>
rvalue when T is

This works around an issue with sync_wait() implementation under
MSVC that breaks for task<int>.
2017-09-04 21:51:43 +09:30
Lewis Baker
662f42edd9 Add workaround for Clang bug in when_all_awaitable2.
Clang was complaining about return-type of await_suspend()
when return-type is 'auto' and deduced to be 'bool',
saying that it must be 'bool' or 'void'.
2017-09-04 10:04:14 +09:30
Lewis Baker
fb45d02996 Fix member accessibility in when_all_task_promise<void>. 2017-09-04 10:01:45 +09:30
Lewis Baker
86a429aead WIP: generic variadic when_all() implementation. 2017-09-02 10:21:38 +09:30
Lewis Baker
d07d48eff0 Experimental implementation of generalised sync_wait().
Causes some test failures which I still need to look into.
2017-09-02 10:08:56 +09:30
Lewis Baker
e7870443d7 Add awaitable_traits, is_awaitable, is_awaiter template metafuntions 2017-09-02 09:58:15 +09:30
Lewis Baker
81de379342 Add schedule_on() and resume_on() functions. 2017-08-22 07:14:25 +09:30
Lewis Baker
8e728932be Improvements to fmap() implementation
- Added documentation to README
- Implemented fmap() for generator<T>, recursive_generator<T>,
  shared_task<T>.
- Centralised operator| overloads for fmap() in fmap.hpp.
- Now provide two-argument fmap() overloads as the type-specific
  implementations: fmap(func, value)
  This replaces the need for detail::apply_fmap() functions.
2017-08-19 23:12:34 +09:30
Lewis Baker
316ce6c7dd Modify task<T> to avoid potential for unbounded recursion.
The await_suspend() method of operator co_await() now returns bool
instead of void, to allow it to resume execution without suspending
if the task completes synchronously. However, this unfortunately
requires use of std::atomic operations to decide a potential race
between the awaiter suspending and the coroutine running to
completion.

This avoids a stack-overflow error when a coroutine awaits a
long sequence of task<T> values that complete synchronously.
eg. in a loop.
2017-08-17 22:23:59 +09:30
Lewis Baker
bf9eb2c581 Rename lazy_task -> task, shared_lazy_task -> shared_task.
No need to keep the 'lazy' term in the name to distinguish it
from eager tasks as all task are lazy now.
2017-08-17 21:14:34 +09:30
Lewis Baker
64b0a049f9 Remove eager task types task<T> and shared_task<T>.
These types make it difficult to write exception-safe code
when compared with their lazy-task counterparts.
2017-08-17 07:55:58 +09:30
Lewis Baker
3a581e633e Add cppcoro::sync_wait().
Allows synchronously waiting for a task to complete.
2017-08-14 09:14:20 +09:30
Lewis Baker
d6f005342d Make lightweight_manual_reset_event methods noexcept.
This is important to guarantee thread safety in some scenarios.
2017-08-10 22:37:11 +09:30
Lewis Baker
278c3181a6 Add detail::lightweight_manual_reset_event class.
Uses futex() on Linux, WaitOnAddress() on Win8+ and falls back to
Win32 events on earlier versions of Windows.
2017-08-10 07:34:09 +09:30
Lewis Baker
83ab6399ec Suppress some Clang warnings about unused return values.
Cast result to (void) where we are intentionally ignoring
the return value.
2017-08-03 00:12:47 +09:30
Lewis Baker
4c7569cb50 Merge branch 'master' of github.com:lewissbaker/cppcoro into clang
# Conflicts:
#	test/build.cake
2017-08-02 21:31:56 +09:30
Lewis Baker
7513d9cf0c Remove hack workaround for co_return <void-expr> under clang.
This bug has been fixed in clang-6.0.
2017-08-02 21:30:17 +09:30
Lewis Baker
67fea7fe53 Add when_all() and when_all_ready().
Add 'get_starter()' methods to all task types.
Add detail::unwrap_reference_t helper.
Add detail::when_all_awaitable helper.
2017-08-02 08:32:10 +09:30
Lewis Baker
671ac0b006 Rename 'resumer' class to 'continuation'.
The name 'continuation' much better matches the concept.
2017-08-02 08:32:10 +09:30
Lewis Baker
954588ccfa Enable make_shared_task(lazy_task<void>) workaround under clang. 2017-08-02 08:32:08 +09:30
Lewis Baker
5f5bfa9649 Add 'value_type' typedefs to each of the task-types.
This typedef just makes available the template parameter of the
task type. This can be useful when writing generic functions that
need to operate on any of the task-types.
2017-08-02 08:32:07 +09:30
Lewis Baker
c22bef61a9 Modify task classes to support storing callback and pointer.
This allows registering continuations that are not coroutine
handles which can be useful for building some operators.
This comes at the expense of storage of an extra pointer per
coroutine and/or awaiter.

Adds the cppcoro::detail::resumer to wrap something that can
either be a callback + state or a coroutine_handle.
2017-08-02 08:32:07 +09:30
Lewis Baker
2104e545f4 Merge branch 'master' of github.com:lewissbaker/cppcoro into clang
# Conflicts:
#	tools/cake
2017-07-28 23:07:34 +09:30
Lewis Baker
2af1b18c0f Add async_manual_reset_event. 2017-07-25 07:10:50 +09:30
Lewis Baker
4b20a243b2 Add async_auto_reset_event. 2017-07-21 23:04:56 +09:30
Lewis Baker
49ec4915b0 Add workarounds for broken 'co_return <void-expr>;' in Clang. 2017-07-18 22:21:34 +09:30
Lewis Baker
e5bf85c5e9 Fix bug in recursive_generator<T> when building under Clang.
The promise_type::get_return_object() method was returning a reference
to the promise object, expecting that reference to be passed to the
constructor of recursive_generator<T>.

However, under Clang the result of get_return_object() is assigned to
an 'auto' variable, which ends up taking a copy of the promise object
on the stack and passing that copy into the recursive_generator<T>
constructor.

Modified get_return_object() to return the already-constructed
recursive_generator<T> object.
2017-07-18 22:03:27 +09:30
Lewis Baker
1f6c7034c2 Fix get_return_object() to not require explict constructor call.
Clang doesn't allow calling explicit constructor to convert from
get_return_object() return-value to coroutine return-value.
2017-07-17 08:48:44 +09:30
Lewis Baker
528ba691e6 Add missing noexcept declaration to operation_cancelled::what(). 2017-07-17 07:40:35 +09:30
Lewis Baker
773a4be858 Add missing #includes to fmap.hpp
Also delcare fmap_transform constructor explict and add noexcept
declaration.
2017-07-17 07:38:38 +09:30
Lewis Baker
a590ac14ca Add missing #include in cancellation_registration.hpp
The definition of cancellation_token is required for inline
constructor when building under Clang.
2017-07-17 07:32:27 +09:30
Lewis Baker
0af7061608 Use std::result_of_t instead of std::invoke_result_t.
This helps building under clang as libc++ doesn't seem to have
std::invoke_result yet.
2017-07-17 07:29:40 +09:30
Lewis Baker
fab674f832 Add fmap() support to async_generator<T>. 2017-07-11 08:38:09 +09:30
Lewis Baker
1871e52a7f Add fmap() and implement transform for task<T> and lazy_task<T>. 2017-07-11 08:35:46 +09:30
Lewis Baker
f1349291ef Fixes to compile cleanly under MSVC warning-level 4 and /analyze.
- Add [[maybe_unused]] to various parameters/variables.
- Rework a few loops in test-cases to get around some warnings
  about unreachable code.
2017-07-06 22:30:05 +09:30
Lewis Baker
1140628b6e Make interface of async_mutex more explicit.
Provide both lock_async() and scoped_lock_async(), the latter
returning an async_mutex_lock.
2017-07-06 22:06:48 +09:30
Lewis Baker
178dcc3f81 Explicitly declare io_service move constructor/assignment deleted. 2017-07-03 20:42:07 +09:30
Lewis Baker
830c4d1d30 Add io_service::schedule_after() to allow delayed scheduling.
The io_service now launches a background thread on-demand for
keeping track of active timers. Background thread uses a waitable
timer event for performing timed waits and an auto-reset event
for signalling addition of new timers, timer cancellation and
thread shutdown.
2017-07-02 22:02:56 +09:30