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
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
cython-plus
typon
Commits
70dca6f1
Commit
70dca6f1
authored
Aug 06, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor fork reclamation strategies into policies
parent
9483a46b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
113 additions
and
78 deletions
+113
-78
rt/include/typon/fork.hpp
rt/include/typon/fork.hpp
+77
-55
rt/include/typon/forked.hpp
rt/include/typon/forked.hpp
+26
-23
rt/include/typon/typon.hpp
rt/include/typon/typon.hpp
+10
-0
No files found.
rt/include/typon/fork.hpp
View file @
70dca6f1
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
#include <coroutine>
#include <coroutine>
#include <cstdint>
#include <cstdint>
#include <typon/defer.hpp>
#include <typon/forked.hpp>
#include <typon/forked.hpp>
#include <typon/result.hpp>
#include <typon/result.hpp>
#include <typon/scheduler.hpp>
#include <typon/scheduler.hpp>
...
@@ -14,7 +15,73 @@
...
@@ -14,7 +15,73 @@
namespace
typon
namespace
typon
{
{
template
<
typename
T
=
void
>
namespace
policy
{
struct
Bundle
{
void
on_final_suspend
(
std
::
coroutine_handle
<>
coroutine
)
noexcept
{
(
void
)
coroutine
;
}
struct
OnAwaitable
{
template
<
typename
Promise
>
void
on_await_suspend
(
std
::
coroutine_handle
<
Promise
>
coroutine
)
noexcept
{
(
void
)
coroutine
;
}
template
<
typename
Promise
>
auto
on_await_resume
(
std
::
coroutine_handle
<
Promise
>
coroutine
)
{
using
T
=
typename
Promise
::
value_type
;
auto
thefts
=
coroutine
.
promise
().
_span
->
_thefts
;
auto
rank
=
coroutine
.
promise
().
_rank
;
bool
ready
=
(
thefts
==
rank
);
if
(
!
ready
)
{
coroutine
.
promise
().
_span
->
_children
.
push_back
(
coroutine
);
}
return
Forked
<
T
>
(
coroutine
,
ready
,
nullptr
);
}
};
};
struct
Refcnt
{
ForkNode
_node
;
void
on_final_suspend
(
std
::
coroutine_handle
<>
coroutine
)
noexcept
{
(
void
)
coroutine
;
_node
.
decref
();
}
struct
OnAwaitable
{
template
<
typename
Promise
>
void
on_await_suspend
(
std
::
coroutine_handle
<
Promise
>
coroutine
)
noexcept
{
coroutine
.
promise
().
_policy
.
_node
.
_coroutine
=
coroutine
;
}
template
<
typename
Promise
>
auto
on_await_resume
(
std
::
coroutine_handle
<
Promise
>
coroutine
)
{
using
T
=
typename
Promise
::
value_type
;
auto
thefts
=
coroutine
.
promise
().
_span
->
_thefts
;
auto
rank
=
coroutine
.
promise
().
_rank
;
bool
ready
=
(
thefts
==
rank
);
auto
node
=
&
(
coroutine
.
promise
().
_policy
.
_node
);
return
Forked
<
T
>
(
coroutine
,
ready
,
node
);
}
};
};
}
template
<
typename
T
=
void
,
typename
Policy
=
policy
::
Refcnt
>
struct
[[
nodiscard
]]
Fork
struct
[[
nodiscard
]]
Fork
{
{
struct
promise_type
;
struct
promise_type
;
...
@@ -36,11 +103,7 @@ namespace typon
...
@@ -36,11 +103,7 @@ namespace typon
{
{
Span
*
_span
;
Span
*
_span
;
u64
_rank
;
u64
_rank
;
ForkNode
_node
;
[[
no_unique_address
]]
Policy
_policy
;
promise_type
()
noexcept
:
_node
{
std
::
coroutine_handle
<
promise_type
>::
from_promise
(
*
this
)
}
{}
Fork
get_return_object
()
noexcept
Fork
get_return_object
()
noexcept
{
{
...
@@ -63,19 +126,11 @@ namespace typon
...
@@ -63,19 +126,11 @@ namespace typon
{
{
return
span
->
_coroutine
;
return
span
->
_coroutine
;
}
}
auto
rank
=
coroutine
.
promise
().
_rank
;
if
(
auto
&
exception
=
coroutine
.
promise
().
_exception
)
if
(
auto
&
exception
=
coroutine
.
promise
().
_exception
)
{
{
span
->
set_exception
(
exception
,
rank
);
span
->
set_exception
(
exception
,
coroutine
.
promise
().
_rank
);
}
if
(
!
(
rank
&
1
))
{
auto
&
ref
=
coroutine
.
promise
().
_node
.
_ref
;
if
(
!
ref
.
exchange
(
false
,
std
::
memory_order_acq_rel
))
{
coroutine
.
destroy
();
}
}
}
coroutine
.
promise
().
_policy
.
on_final_suspend
(
coroutine
);
u64
n
=
span
->
_n
.
fetch_sub
(
1
,
std
::
memory_order_acq_rel
);
u64
n
=
span
->
_n
.
fetch_sub
(
1
,
std
::
memory_order_acq_rel
);
if
(
n
==
1
)
if
(
n
==
1
)
{
{
...
@@ -92,42 +147,16 @@ namespace typon
...
@@ -92,42 +147,16 @@ namespace typon
struct
awaitable
:
std
::
suspend_always
struct
awaitable
:
std
::
suspend_always
{
{
std
::
coroutine_handle
<
promise_type
>
_coroutine
;
std
::
coroutine_handle
<
promise_type
>
_coroutine
;
[[
no_unique_address
]]
Policy
::
OnAwaitable
_policy
;
template
<
typename
Promise
>
template
<
typename
Promise
>
auto
await_suspend
(
std
::
coroutine_handle
<
Promise
>
continuation
)
noexcept
auto
await_suspend
(
std
::
coroutine_handle
<
Promise
>
continuation
)
noexcept
{
{
Span
*
span
=
&
(
continuation
.
promise
().
_span
);
Span
*
span
=
&
(
continuation
.
promise
().
_span
);
_coroutine
.
promise
().
_span
=
span
;
_coroutine
.
promise
().
_span
=
span
;
_coroutine
.
promise
().
_rank
=
(
span
->
_thefts
<<
1
)
;
_coroutine
.
promise
().
_rank
=
span
->
_thefts
;
std
::
coroutine_handle
<>
on_stack_handle
=
_coroutine
;
_policy
.
on_await_suspend
(
_coroutine
);
Scheduler
::
push
(
span
);
return
on_stack_handle
;
}
auto
await_resume
()
{
auto
thefts
=
_coroutine
.
promise
().
_span
->
_thefts
;
auto
rank
=
_coroutine
.
promise
().
_rank
;
return
Forked
<
T
>
(
_coroutine
,
(
thefts
==
(
rank
>>
1
)),
true
);
}
};
auto
operator
co_await
()
&&
{
return
awaitable
{
{},
_coroutine
};
}
struct
noloop_awaitable
:
std
::
suspend_always
{
std
::
coroutine_handle
<
promise_type
>
_coroutine
;
template
<
typename
Promise
>
auto
await_suspend
(
std
::
coroutine_handle
<
Promise
>
continuation
)
noexcept
{
Span
*
span
=
&
(
continuation
.
promise
().
_span
);
_coroutine
.
promise
().
_span
=
span
;
_coroutine
.
promise
().
_rank
=
(
span
->
_thefts
<<
1
)
+
1
;
std
::
coroutine_handle
<>
on_stack_handle
=
_coroutine
;
std
::
coroutine_handle
<>
on_stack_handle
=
_coroutine
;
Scheduler
::
push
(
span
);
Scheduler
::
push
(
span
);
...
@@ -136,20 +165,13 @@ namespace typon
...
@@ -136,20 +165,13 @@ namespace typon
auto
await_resume
()
auto
await_resume
()
{
{
auto
thefts
=
_coroutine
.
promise
().
_span
->
_thefts
;
return
_policy
.
on_await_resume
(
_coroutine
);
auto
rank
=
_coroutine
.
promise
().
_rank
;
bool
ready
=
thefts
==
(
rank
>>
1
);
if
(
!
ready
)
{
_coroutine
.
promise
().
_span
->
_children
.
push_back
(
_coroutine
);
}
return
Forked
<
T
>
(
_coroutine
,
ready
,
false
);
}
}
};
};
auto
noloop
()
&&
auto
operator
co_await
()
&&
{
{
return
noloop_awaitable
{
{},
_coroutine
};
return
awaitable
{
{},
_coroutine
,
{}
};
}
}
};
};
...
...
rt/include/typon/forked.hpp
View file @
70dca6f1
...
@@ -17,6 +17,14 @@ namespace typon
...
@@ -17,6 +17,14 @@ namespace typon
{
{
std
::
coroutine_handle
<>
_coroutine
;
std
::
coroutine_handle
<>
_coroutine
;
std
::
atomic
<
bool
>
_ref
{
true
};
std
::
atomic
<
bool
>
_ref
{
true
};
void
decref
()
noexcept
{
if
(
!
_ref
.
exchange
(
false
,
std
::
memory_order_acq_rel
))
{
_coroutine
.
destroy
();
}
}
};
};
...
@@ -110,8 +118,8 @@ namespace typon
...
@@ -110,8 +118,8 @@ namespace typon
Result
<
T
>
*
_result
=
nullptr
;
Result
<
T
>
*
_result
=
nullptr
;
template
<
typename
Promis
e
>
template
<
typename
Coroutin
e
>
Forked
(
std
::
coroutine_handle
<
Promise
>
coroutine
,
bool
ready
,
bool
owning
)
Forked
(
Coroutine
coroutine
,
bool
ready
,
ForkNode
*
node
)
{
{
if
(
ready
)
if
(
ready
)
{
{
...
@@ -120,7 +128,7 @@ namespace typon
...
@@ -120,7 +128,7 @@ namespace typon
}
}
else
else
{
{
this
->
_node
=
owning
?
&
(
coroutine
.
promise
().
_node
)
:
nullptr
;
this
->
_node
=
node
;
_result
=
&
(
coroutine
.
promise
());
_result
=
&
(
coroutine
.
promise
());
}
}
}
}
...
@@ -163,10 +171,7 @@ namespace typon
...
@@ -163,10 +171,7 @@ namespace typon
{
{
if
(
auto
node
=
this
->
_node
)
if
(
auto
node
=
this
->
_node
)
{
{
if
(
!
node
->
_ref
.
exchange
(
false
,
std
::
memory_order_acq_rel
))
node
->
decref
();
{
node
->
_coroutine
.
destroy
();
}
}
}
}
}
else
else
...
@@ -194,20 +199,21 @@ namespace typon
...
@@ -194,20 +199,21 @@ namespace typon
bool
_ready
;
bool
_ready
;
Result
<
T
>
*
_result
;
Result
<
T
>
*
_result
;
ForkNode
*
_nod
e
;
void
*
_coroutin
e
;
template
<
typename
Promis
e
>
template
<
typename
Coroutin
e
>
Forked
(
std
::
coroutine_handle
<
Promise
>
coroutine
,
bool
ready
,
bool
owning
)
Forked
(
Coroutine
coroutine
,
bool
ready
,
ForkNode
*
node
)
{
{
_ready
=
ready
;
_ready
=
ready
;
_result
=
&
(
coroutine
.
promise
());
_result
=
&
(
coroutine
.
promise
());
_node
=
(
owning
|
ready
)
?
&
(
coroutine
.
promise
().
_node
)
:
nullptr
;
if
(
ready
)
if
(
ready
)
{
{
if
(
auto
&
exception
=
coroutine
.
promise
().
_exception
)
_coroutine
=
coroutine
.
address
();
{
coroutine
.
promise
().
get
();
std
::
rethrow_exception
(
exception
);
}
}
else
{
_coroutine
=
node
;
}
}
}
}
...
@@ -217,31 +223,28 @@ namespace typon
...
@@ -217,31 +223,28 @@ namespace typon
Forked
(
Forked
&&
other
)
noexcept
Forked
(
Forked
&&
other
)
noexcept
:
_ready
(
other
.
_ready
)
:
_ready
(
other
.
_ready
)
,
_result
(
other
.
_result
)
,
_result
(
other
.
_result
)
,
_
node
(
std
::
exchange
(
other
.
_nod
e
,
nullptr
))
,
_
coroutine
(
std
::
exchange
(
other
.
_coroutin
e
,
nullptr
))
{}
{}
Forked
&
operator
=
(
Forked
&&
other
)
noexcept
Forked
&
operator
=
(
Forked
&&
other
)
noexcept
{
{
std
::
swap
(
_ready
,
other
.
_ready
);
std
::
swap
(
_ready
,
other
.
_ready
);
std
::
swap
(
_node
,
other
.
_node
);
std
::
swap
(
_result
,
other
.
_result
);
std
::
swap
(
_result
,
other
.
_result
);
std
::
swap
(
_coroutine
,
other
.
_coroutine
);
return
*
this
;
return
*
this
;
}
}
~
Forked
()
~
Forked
()
{
{
if
(
_
nod
e
)
if
(
_
coroutin
e
)
{
{
if
(
_ready
)
if
(
_ready
)
{
{
_node
->
_coroutine
.
destroy
();
std
::
coroutine_handle
<
void
>::
from_address
(
_coroutine
)
.
destroy
();
}
}
else
else
{
{
if
(
!
_node
->
_ref
.
exchange
(
false
,
std
::
memory_order_acq_rel
))
reinterpret_cast
<
ForkNode
*>
(
_coroutine
)
->
decref
();
{
_node
->
_coroutine
.
destroy
();
}
}
}
}
}
}
}
...
...
rt/include/typon/typon.hpp
View file @
70dca6f1
...
@@ -26,6 +26,16 @@ namespace typon
...
@@ -26,6 +26,16 @@ namespace typon
}
}
template
<
typename
Policy
,
typename
Task
>
Fork
<
typename
Task
::
promise_type
::
value_type
,
Policy
>
fork
(
Task
task
)
{
// Put the task in a local variable to ensure its destructor will
// be called on co_return instead of only on coroutine destruction.
Task
local_task
=
std
::
move
(
task
);
co_return
co_await
std
::
move
(
local_task
);
}
template
<
typename
Task
>
template
<
typename
Task
>
Future
<
typename
Task
::
promise_type
::
value_type
>
future
(
Task
task
)
Future
<
typename
Task
::
promise_type
::
value_type
>
future
(
Task
task
)
{
{
...
...
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