Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ladnir committed May 12, 2024
1 parent 5a7b060 commit ca04f29
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 76 deletions.
38 changes: 6 additions & 32 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ jobs:
submodules: recursive

# Runs a set of commands using the runners shell
- name: build library cpp14
- name: build library cpp204
run: |
python3 build.py -DMACORO_CPP_VER=14
python3 build.py -DMACORO_CPP_VER=20
- name: unit test cpp14
- name: unit test cpp20
run: |
./out/build/linux/frontend/macoroFrontend -u
Expand Down Expand Up @@ -73,16 +73,6 @@ jobs:
./out/main
rm -rf out/
cd ../..
- name: build library cpp20
run: |
rm -rf out/build
rm -rf out/install
python3 build.py -DMACORO_CPP_VER=20
- name: unit test cpp20
run: |
./out/build/linux/frontend/macoroFrontend -u
# This workflow contains a single job called "build"
build-osx:
Expand All @@ -105,14 +95,6 @@ jobs:
run: |
./out/build/osx/frontend/macoroFrontend -u
- name: build library 14
run: |
python3 build.py -DMACORO_CPP_VER=14
- name: unit test 14
run: |
./out/build/osx/frontend/macoroFrontend -u
- name: find source tree
run: |
cd cmake/testProject
Expand Down Expand Up @@ -167,11 +149,11 @@ jobs:


# Runs a set of commands using the runners shell
- name: build library cpp14
- name: build library cpp20
run: |
python3 build.py -G Ninja -DMACORO_CPP_VER=14
python3 build.py -G Ninja -DMACORO_CPP_VER=20
- name: unit test cpp14
- name: unit test cpp20
run: |
./out/build/x64-Release/frontend/macoroFrontend.exe -u
Expand Down Expand Up @@ -204,12 +186,4 @@ jobs:
rm -r -fo out/
cd ../..
- name: build library cpp20
run: |
rm -re -fo out/
python3 build.py -G Ninja -DMACORO_CPP_VER=20
- name: unit test cpp20
run: |
./out/build/x64-Release/frontend/macoroFrontend.exe -u
4 changes: 4 additions & 0 deletions macoro/async_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace macoro
{
// a scoped_task is eagerly started but has its lifetime
// tied to some scoped object, e.g. async_scope or
// when_all_scope. scoped_task can be fire and forget but
// will always be joined then the scope object is awaited.
template<typename T = void>
struct scoped_task
{
Expand Down
12 changes: 12 additions & 0 deletions macoro/detail/scoped_task_promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ namespace macoro
public basic_traceable
{
public:
//// can be used to check if it allocates
//void* operator new(std::size_t n) noexcept
//{
// std::cout << "scoped_task_promise alloc" << std::endl;
// return std::malloc(n);
//}
//void operator delete(void* ptr, std::size_t sz)
//{
// std::free(ptr);
//}


using value_type = T;

friend struct final_awaitable;
Expand Down
2 changes: 1 addition & 1 deletion macoro/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ namespace macoro

template<
typename VALUE,
typename = enable_if_t<std::is_convertible<VALUE&&, T>::value>>
typename = enable_if_t<std::is_constructible<T, VALUE&&>::value>>
void return_value(VALUE&& value)
noexcept(std::is_nothrow_constructible<T, VALUE&&>::value)
{
Expand Down
75 changes: 52 additions & 23 deletions macoro/when_all_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@

namespace macoro
{
template<typename awaitable>
struct scoped_t;

template<>
struct scoped_t<void>
{};

template<typename awaitable>
struct scoped_t
{
using awaitable_type = awaitable;
using storage_type = remove_rvalue_reference_t<awaitable>;

storage_type m_awaitable;
};

inline scoped_t<void> scoped() { return {}; }

template<typename awaitable>
auto scoped(awaitable&& a)
{
return scoped_t<awaitable>(std::forward<awaitable>(a));
}

template<typename awaitable>
decltype(auto) operator|(awaitable&& a, scoped_t<void>)
{
return scoped(std::forward<awaitable>(a));
}

struct when_all_scope
{
Expand Down Expand Up @@ -40,16 +69,16 @@ namespace macoro

void return_void() noexcept {};

//template<typename A>
//decltype(auto) await_transform(A&& a) noexcept
//{
// return std::forward<A>(a);
//}
template<typename A>
decltype(auto) await_transform(A&& a) noexcept
{
return std::forward<A>(a);
}

template<typename A>
decltype(auto) await_transform(A&& a, std::source_location loc = std::source_location::current()) noexcept
decltype(auto) await_transform(scoped_t<A>&& a, std::source_location loc = std::source_location::current()) noexcept
{
using scoped_task_of_a = scoped_task<int>;// decltype(m_scope.add(std::forward<A>(a)));
using scoped_task_of_a = decltype(m_scope.add(std::forward<typename scoped_t<A>::awaitable_type>(a.m_awaitable), loc));
struct awaiter
{
scoped_task_of_a m_scoped_task;
Expand All @@ -60,25 +89,25 @@ namespace macoro
return std::move(m_scoped_task);
}
};
return awaiter{ m_scope.add(std::forward<A>(a), loc) };
return awaiter{ m_scope.add(std::forward<typename scoped_t<A>::awaitable_type>(a.m_awaitable), loc) };
}


template<typename T>
decltype(auto) await_transform(scoped_task<T>& a)
{
if (a.m_coroutine.promise().m_scope != &m_scope)
throw std::runtime_error("in a when_all_scope, only awaiting scoped_task's from this scope is supported.");
return a;
}
template<typename T>
decltype(auto) await_transform(scoped_task<T>&& a)
{
auto& prom = a.m_coroutine.promise();
if (prom.m_scope != &m_scope)
throw std::runtime_error("in a when_all_scope, only awaiting scoped_task's from this scope is supported.");
return std::move(a);
}
//template<typename T>
//decltype(auto) await_transform(scoped_task<T>& a)
//{
// if (a.m_coroutine.promise().m_scope != &m_scope)
// throw std::runtime_error("in a when_all_scope, only awaiting scoped_task's from this scope is supported.");
// return a;
//}
//template<typename T>
//decltype(auto) await_transform(scoped_task<T>&& a)
//{
// auto& prom = a.m_coroutine.promise();
// if (prom.m_scope != &m_scope)
// throw std::runtime_error("in a when_all_scope, only awaiting scoped_task's from this scope is supported.");
// return std::move(a);
//}

traceable* get_traceable()
{
Expand Down
68 changes: 48 additions & 20 deletions tests/when_all_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,54 @@ namespace macoro
}


void when_all_scope_test()
{

auto tr = []()->when_all_scope {

scoped_task<int> r = co_await f();

int i = co_await std::move(r);
co_return;

}
();

sync_wait(std::move(tr));
using namespace macoro;
using Awaitable = task<int>;
Awaitable a = f();
auto t = detail::make_when_all_task<Awaitable>(std::move(a));

}
void when_all_scope_test()
{
auto f = []() -> task<int> {
co_return 42;
};

auto g = []() -> task<std::unique_ptr<int>> {
co_return new int{ 10 };
};

// a coroutine with RAII eager subtasks.
// the eager subtasks will be joined before
// the scope exits.
auto t = [&]()->task<> {

int q = co_await f();

// start a eager scope. All scoped
// tasks in the when_all_scope will
// be joined before scope exit.
co_await [&]()->when_all_scope {

// start the work eagerly
scoped_task<int> r = co_await scoped(f());

// start the work eagerly
scoped_task<std::unique_ptr<int>> h = co_await scoped(g());

// join one of them
int i = co_await std::move(r);

if (i == 42)
throw std::runtime_error("meaning of life");

// h wont be joined explicitly if i == 42. However,
// when_all_scope will suspend until h completes.
// exceptions will be propegated out of the scope.
std::unique_ptr<int> j = co_await std::move(h);
}
();

// h has been joined and an exception containing
// "meaning of life" will be thrown.
}
();

sync_wait(std::move(t));
}
}
}

0 comments on commit ca04f29

Please sign in to comment.