authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-28 01:22:51-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-28 01:22:51-04:00
log6fed777637daafc077ec2c079b0557f3c8bdce51
tree493fb56a8b782b5da3325a1433f1479331acb424
parentf0c049d02b68a8740096df633a41b78d90fd34cd

cancel detects if the target handle has already returned


1 files changed, 15 insertions(+), 0 deletions(-)

src/ir.cpp+15
......@@ -6672,6 +6672,8 @@ static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node)
66726672
66736673 IrBasicBlock *done_block = ir_create_basic_block(irb, scope, "CancelDone");
66746674 IrBasicBlock *not_canceled_block = ir_create_basic_block(irb, scope, "NotCanceled");
6675 IrBasicBlock *post_return_block = ir_create_basic_block(irb, scope, "PostReturn");
6676 IrBasicBlock *do_cancel_block = ir_create_basic_block(irb, scope, "DoCancel");
66756677
66766678 IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
66776679 IrInstruction *usize_type_val = ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_usize);
......@@ -6679,6 +6681,9 @@ static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node)
66796681 IrInstruction *is_canceled_mask = ir_build_const_usize(irb, scope, node, 0x1); // 0b001
66806682 IrInstruction *promise_T_type_val = ir_build_const_type(irb, scope, node,
66816683 get_promise_type(irb->codegen, irb->codegen->builtin_types.entry_void));
6684 IrInstruction *inverted_ptr_mask = ir_build_const_usize(irb, scope, node, 0x7); // 0b111
6685 IrInstruction *ptr_mask = ir_build_un_op(irb, scope, node, IrUnOpBinNot, inverted_ptr_mask); // 0b111...000
6686 IrInstruction *await_mask = ir_build_const_usize(irb, scope, node, 0x4); // 0b100
66826687
66836688 // TODO relies on Zig not re-ordering fields
66846689 IrInstruction *casted_target_inst = ir_build_ptr_cast(irb, scope, node, promise_T_type_val, target_inst);
......@@ -6697,6 +6702,16 @@ static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *scope, AstNode *node)
66976702 ir_build_cond_br(irb, scope, node, is_canceled_bool, done_block, not_canceled_block, is_comptime);
66986703
66996704 ir_set_cursor_at_end_and_append_block(irb, not_canceled_block);
6705 IrInstruction *awaiter_addr = ir_build_bin_op(irb, scope, node, IrBinOpBinAnd, prev_atomic_value, ptr_mask, false);
6706 IrInstruction *is_returned_bool = ir_build_bin_op(irb, scope, node, IrBinOpCmpEq, awaiter_addr, ptr_mask, false);
6707 ir_build_cond_br(irb, scope, node, is_returned_bool, post_return_block, do_cancel_block, is_comptime);
6708
6709 ir_set_cursor_at_end_and_append_block(irb, post_return_block);
6710 IrInstruction *is_awaited_value = ir_build_bin_op(irb, scope, node, IrBinOpBinAnd, prev_atomic_value, await_mask, false);
6711 IrInstruction *is_awaited_bool = ir_build_bin_op(irb, scope, node, IrBinOpCmpNotEq, is_awaited_value, zero, false);
6712 ir_build_cond_br(irb, scope, node, is_awaited_bool, done_block, do_cancel_block, is_comptime);
6713
6714 ir_set_cursor_at_end_and_append_block(irb, do_cancel_block);
67006715 ir_build_cancel(irb, scope, node, target_inst);
67016716 ir_build_br(irb, scope, node, done_block, is_comptime);
67026717