Skip to content

Commit f66fea8

Browse files
author
Conor
committed
unions parent/exception types
1 parent c68df1f commit f66fea8

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/core/frame.cxx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module;
2+
#include "libfork/__impl/assume.hpp"
23
#include "libfork/__impl/utils.hpp"
34
export module libfork.core:frame;
45

@@ -16,7 +17,14 @@ export enum class category : std::uint8_t {
1617
fork,
1718
};
1819

19-
struct cancellation {};
20+
struct cancellation {
21+
cancellation *parent = nullptr;
22+
std::atomic<std::uint32_t> cancelled = 0;
23+
};
24+
25+
struct block_type {
26+
//
27+
};
2028

2129
// =================== Frame =================== //
2230

@@ -27,7 +35,21 @@ struct frame_type {
2735
using allocator_type = allocator_t<Context>;
2836
using checkpoint_type = checkpoint_t<allocator_type>;
2937

30-
frame_type *parent;
38+
union parent_union {
39+
frame_type *frame;
40+
block_type *block;
41+
};
42+
43+
struct except_type {
44+
parent_union stashed;
45+
std::exception_ptr exception;
46+
};
47+
48+
union {
49+
parent_union parent;
50+
except_type *except;
51+
};
52+
3153
cancellation *cancel;
3254

3355
[[no_unique_address]]

src/core/promise.cxx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,18 @@ template <worker_context Context>
6161
[[nodiscard]]
6262
constexpr auto final_suspend(frame_type<Context> *frame) noexcept -> coro<> {
6363

64+
// Validate final state
65+
LF_ASSUME(frame->steals == 0);
66+
LF_ASSUME(frame->joins == k_u16_max);
67+
LF_ASSUME(frame->exception_bit == 0);
68+
6469
defer _ = [frame] noexcept -> void {
6570
frame->handle().destroy();
6671
};
6772

6873
switch (not_null(frame)->kind) {
6974
case category::call:
70-
return not_null(frame->parent)->handle();
75+
return not_null(frame->parent.frame)->handle();
7176
case category::root:
7277
// TODO: root handling
7378
return std::noop_coroutine();
@@ -79,7 +84,7 @@ constexpr auto final_suspend(frame_type<Context> *frame) noexcept -> coro<> {
7984

8085
Context *context = not_null(thread_context<Context>);
8186

82-
frame_type<Context> *parent = not_null(frame->parent);
87+
frame_type<Context> *parent = not_null(frame->parent.frame);
8388

8489
if (frame_handle last_pushed = context->pop()) {
8590
// No-one stole continuation, we are the exclusive owner of parent, so we
@@ -186,7 +191,7 @@ struct awaitable : std::suspend_always {
186191
// TODO: handle cancellation
187192

188193
// Propagate parent->child relationships
189-
self.child->parent = &parent.promise().frame;
194+
self.child->parent.frame = &parent.promise().frame;
190195
self.child->cancel = parent.promise().frame.cancel;
191196
self.child->stack_ckpt = not_null(thread_context<Context>)->alloc().checkpoint();
192197
self.child->kind = Cat;

0 commit comments

Comments
 (0)