Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tom Niget
typon
Commits
99d581e5
Commit
99d581e5
authored
Apr 04, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add lazy coroutine task implementation
parents
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
226 additions
and
0 deletions
+226
-0
rt/coroutine.hpp
rt/coroutine.hpp
+28
-0
rt/result.hpp
rt/result.hpp
+111
-0
rt/task.hpp
rt/task.hpp
+87
-0
No files found.
rt/coroutine.hpp
0 → 100644
View file @
99d581e5
#ifndef __COROUTINE_HPP__
#define __COROUTINE_HPP__
#if defined(__clang__) && __clang__
#include <experimental/coroutine>
namespace
typon
{
using
std
::
experimental
::
coroutine_handle
;
using
std
::
experimental
::
suspend_always
;
using
std
::
experimental
::
suspend_never
;
using
std
::
experimental
::
noop_coroutine
;
}
#elif defined(__GNUC__) && __GNUC__
#include <coroutine>
namespace
typon
{
using
std
::
coroutine_handle
;
using
std
::
suspend_always
;
using
std
::
suspend_never
;
using
std
::
noop_coroutine
;
}
#endif
#endif // __COROUTINE_HPP__
rt/result.hpp
0 → 100644
View file @
99d581e5
#ifndef __RESULT_HPP__
#define __RESULT_HPP__
#include <exception>
#include <memory>
#include <type_traits>
namespace
typon
{
template
<
typename
T
>
struct
result
{
std
::
exception_ptr
_exception
;
union
{
T
_value
;
};
~
result
()
{
if
(
!
_exception
)
{
std
::
destroy_at
(
std
::
addressof
(
_value
));
}
}
template
<
typename
U
>
void
return_value
(
U
&&
expr
)
noexcept
(
std
::
is_nothrow_constructible_v
<
T
,
U
&&>
)
{
std
::
construct_at
(
std
::
addressof
(
_value
),
std
::
forward
<
U
>
(
expr
));
}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
T
&
get
()
&
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
return
_value
;
}
T
&&
get
()
&&
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
return
std
::
move
(
_value
);
}
};
template
<
typename
T
>
struct
result
<
T
&>
{
T
*
_value
;
std
::
exception_ptr
_exception
;
void
return_value
(
T
&
expr
)
noexcept
{
_value
=
std
::
addressof
(
expr
);
}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
T
&
get
()
&
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
return
*
_value
;
}
};
template
<
>
struct
result
<
void
>
{
std
::
exception_ptr
_exception
;
void
return_void
()
noexcept
{}
void
unhandled_exception
()
noexcept
{
_exception
=
std
::
current_exception
();
}
void
get
()
{
if
(
_exception
)
{
std
::
rethrow_exception
(
_exception
);
}
}
};
}
#endif // __RESULT_HPP__
rt/task.hpp
0 → 100644
View file @
99d581e5
#ifndef __TASK_HPP__
#define __TASK_HPP__
#include <coroutine.hpp>
#include <result.hpp>
namespace
typon
{
template
<
typename
T
=
void
>
struct
[[
nodiscard
]]
task
{
struct
promise_type
;
coroutine_handle
<
promise_type
>
_coroutine
;
~
task
()
{
_coroutine
.
destroy
();
}
struct
promise_type
:
result
<
T
>
{
coroutine_handle
<>
_continuation
;
task
get_return_object
()
noexcept
{
return
{
coroutine_handle
<
promise_type
>::
from_promise
(
*
this
)
};
}
suspend_always
initial_suspend
()
noexcept
{
return
{};
}
auto
final_suspend
()
noexcept
{
struct
awaitable
:
suspend_always
{
coroutine_handle
<>
await_suspend
(
coroutine_handle
<
promise_type
>
coroutine
)
noexcept
{
return
coroutine
.
promise
().
_continuation
;
}
};
return
awaitable
{};
}
};
auto
operator
co_await
()
&&
noexcept
{
struct
awaitable
{
coroutine_handle
<
promise_type
>
_coroutine
;
bool
await_ready
()
noexcept
{
return
false
;
}
coroutine_handle
<>
await_suspend
(
coroutine_handle
<>
awaiting_coroutine
)
noexcept
{
_coroutine
.
promise
().
_continuation
=
awaiting_coroutine
;
return
_coroutine
;
}
decltype
(
auto
)
await_resume
()
{
return
_coroutine
.
promise
().
get
();
}
};
return
awaitable
{
_coroutine
};
}
decltype
(
auto
)
call
()
&&
{
_coroutine
.
promise
().
_continuation
=
noop_coroutine
();
_coroutine
.
resume
();
return
_coroutine
.
promise
().
get
();
}
};
}
#endif // __TASK_HPP__
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment