Skip to content

Commit 3f50c40

Browse files
authored
Component model threading intrinsic updates (#2430)
* v2 component model threading * fmt
1 parent 17fe672 commit 3f50c40

23 files changed

Lines changed: 368 additions & 202 deletions

File tree

crates/wasm-encoder/src/component/builder.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,17 @@ impl ComponentBuilder {
708708
self.core_funcs.add(Some("thread.new-indirect"))
709709
}
710710

711-
/// Declares a new `thread.switch-to` intrinsic.
712-
pub fn thread_switch_to(&mut self, cancellable: bool) -> u32 {
713-
self.canonical_functions().thread_switch_to(cancellable);
714-
self.core_funcs.add(Some("thread.switch-to"))
711+
/// Declares a new `thread.suspend-to-suspended` intrinsic.
712+
pub fn thread_suspend_to_suspended(&mut self, cancellable: bool) -> u32 {
713+
self.canonical_functions()
714+
.thread_suspend_to_suspended(cancellable);
715+
self.core_funcs.add(Some("thread.suspend-to-suspended"))
716+
}
717+
718+
/// Declares a new `thread.suspend-to` intrinsic.
719+
pub fn thread_suspend_to(&mut self, cancellable: bool) -> u32 {
720+
self.canonical_functions().thread_suspend_to(cancellable);
721+
self.core_funcs.add(Some("thread.suspend-to"))
715722
}
716723

717724
/// Declares a new `thread.suspend` intrinsic.
@@ -720,16 +727,17 @@ impl ComponentBuilder {
720727
self.core_funcs.add(Some("thread.suspend"))
721728
}
722729

723-
/// Declares a new `thread.resume-later` intrinsic.
724-
pub fn thread_resume_later(&mut self) -> u32 {
725-
self.canonical_functions().thread_resume_later();
726-
self.core_funcs.add(Some("thread.resume-later"))
730+
/// Declares a new `thread.unsuspend` intrinsic.
731+
pub fn thread_unsuspend(&mut self) -> u32 {
732+
self.canonical_functions().thread_unsuspend();
733+
self.core_funcs.add(Some("thread.unsuspend"))
727734
}
728735

729-
/// Declares a new `thread.yield-to` intrinsic.
730-
pub fn thread_yield_to(&mut self, cancellable: bool) -> u32 {
731-
self.canonical_functions().thread_yield_to(cancellable);
732-
self.core_funcs.add(Some("thread.yield-to"))
736+
/// Declares a new `thread.yield-to-suspended` intrinsic.
737+
pub fn thread_yield_to_suspended(&mut self, cancellable: bool) -> u32 {
738+
self.canonical_functions()
739+
.thread_yield_to_suspended(cancellable);
740+
self.core_funcs.add(Some("thread.yield-to-suspended"))
733741
}
734742

735743
/// Adds a new custom section to this component.

crates/wasm-encoder/src/component/canonicals.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,9 @@ impl CanonicalFunctionSection {
522522
self
523523
}
524524

525-
/// Declare a new `thread.switch-to` intrinsic, used to switch execution to
526-
/// another thread.
527-
pub fn thread_switch_to(&mut self, cancellable: bool) -> &mut Self {
525+
/// Declare a new `thread.suspend-to-suspended` intrinsic, used to switch execution to
526+
/// another suspended thread.
527+
pub fn thread_suspend_to_suspended(&mut self, cancellable: bool) -> &mut Self {
528528
self.bytes.push(0x28);
529529
self.bytes.push(if cancellable { 1 } else { 0 });
530530
self.num_added += 1;
@@ -540,17 +540,26 @@ impl CanonicalFunctionSection {
540540
self
541541
}
542542

543-
/// Declare a new `thread.resume-later` intrinsic, used to resume execution
543+
/// Declare a new `thread.suspend-to` intrinsic, used to suspend the current
544+
/// thread and switch to another thread that might not be suspended.
545+
pub fn thread_suspend_to(&mut self, cancellable: bool) -> &mut Self {
546+
self.bytes.push(0x2c);
547+
self.bytes.push(if cancellable { 1 } else { 0 });
548+
self.num_added += 1;
549+
self
550+
}
551+
552+
/// Declare a new `thread.unsuspend` intrinsic, used to resume execution
544553
/// of the given thread.
545-
pub fn thread_resume_later(&mut self) -> &mut Self {
554+
pub fn thread_unsuspend(&mut self) -> &mut Self {
546555
self.bytes.push(0x2a);
547556
self.num_added += 1;
548557
self
549558
}
550559

551-
/// Declare a new `thread.yield-to` intrinsic, used to yield execution to
552-
/// a given thread.
553-
pub fn thread_yield_to(&mut self, cancellable: bool) -> &mut Self {
560+
/// Declare a new `thread.yield-to-suspended` intrinsic, used to yield execution to
561+
/// a given suspended thread.
562+
pub fn thread_yield_to_suspended(&mut self, cancellable: bool) -> &mut Self {
554563
self.bytes.push(0x2b);
555564
self.bytes.push(if cancellable { 1 } else { 0 });
556565
self.num_added += 1;

crates/wasm-encoder/src/reencode/component.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,17 +1118,20 @@ pub mod component_utils {
11181118
let table_index = reencoder.table_index(table_index)?;
11191119
section.thread_new_indirect(func_ty, table_index);
11201120
}
1121-
wasmparser::CanonicalFunction::ThreadSwitchTo { cancellable } => {
1122-
section.thread_switch_to(cancellable);
1121+
wasmparser::CanonicalFunction::ThreadSuspendToSuspended { cancellable } => {
1122+
section.thread_suspend_to_suspended(cancellable);
11231123
}
11241124
wasmparser::CanonicalFunction::ThreadSuspend { cancellable } => {
11251125
section.thread_suspend(cancellable);
11261126
}
1127-
wasmparser::CanonicalFunction::ThreadResumeLater => {
1128-
section.thread_resume_later();
1127+
wasmparser::CanonicalFunction::ThreadSuspendTo { cancellable } => {
1128+
section.thread_suspend_to(cancellable);
11291129
}
1130-
wasmparser::CanonicalFunction::ThreadYieldTo { cancellable } => {
1131-
section.thread_yield_to(cancellable);
1130+
wasmparser::CanonicalFunction::ThreadUnsuspend => {
1131+
section.thread_unsuspend();
1132+
}
1133+
wasmparser::CanonicalFunction::ThreadYieldToSuspended { cancellable } => {
1134+
section.thread_yield_to_suspended(cancellable);
11321135
}
11331136
}
11341137
Ok(())

crates/wasmparser/src/readers/component/canonicals.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ pub enum CanonicalFunction {
274274
/// The index of the table to use.
275275
table_index: u32,
276276
},
277-
/// A function to suspend the current thread and switch to the given thread.
278-
ThreadSwitchTo {
277+
/// A function to suspend the current thread and switch to the given suspended thread.
278+
ThreadSuspendToSuspended {
279279
/// Whether or not the thread can be cancelled while awaiting resumption.
280280
cancellable: bool,
281281
},
@@ -284,10 +284,15 @@ pub enum CanonicalFunction {
284284
/// Whether or not the thread can be cancelled while suspended.
285285
cancellable: bool,
286286
},
287+
/// A function to suspend the current thread and switch to another thread.
288+
ThreadSuspendTo {
289+
/// Whether or not the thread can be cancelled while suspended.
290+
cancellable: bool,
291+
},
287292
/// A function to schedule the given thread to be resumed later.
288-
ThreadResumeLater,
289-
/// A function to suspend the current thread and switch to the given thread.
290-
ThreadYieldTo {
293+
ThreadUnsuspend,
294+
/// A function to yield to the given suspended thread.
295+
ThreadYieldToSuspended {
291296
/// Whether or not the thread can be cancelled while yielding.
292297
cancellable: bool,
293298
},
@@ -406,14 +411,17 @@ impl<'a> FromReader<'a> for CanonicalFunction {
406411
func_ty_index: reader.read()?,
407412
table_index: reader.read()?,
408413
},
409-
0x28 => CanonicalFunction::ThreadSwitchTo {
414+
0x28 => CanonicalFunction::ThreadSuspendToSuspended {
410415
cancellable: reader.read()?,
411416
},
412417
0x29 => CanonicalFunction::ThreadSuspend {
413418
cancellable: reader.read()?,
414419
},
415-
0x2a => CanonicalFunction::ThreadResumeLater,
416-
0x2b => CanonicalFunction::ThreadYieldTo {
420+
0x2a => CanonicalFunction::ThreadUnsuspend,
421+
0x2b => CanonicalFunction::ThreadYieldToSuspended {
422+
cancellable: reader.read()?,
423+
},
424+
0x2c => CanonicalFunction::ThreadSuspendTo {
417425
cancellable: reader.read()?,
418426
},
419427
0x06 => CanonicalFunction::SubtaskCancel {

crates/wasmparser/src/validator/component.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,16 +1262,19 @@ impl ComponentState {
12621262
func_ty_index,
12631263
table_index,
12641264
} => self.thread_new_indirect(func_ty_index, table_index, types, offset),
1265-
CanonicalFunction::ThreadSwitchTo { cancellable } => {
1266-
self.thread_switch_to(cancellable, types, offset)
1265+
CanonicalFunction::ThreadSuspendToSuspended { cancellable } => {
1266+
self.thread_suspend_to_suspended(cancellable, types, offset)
12671267
}
12681268
CanonicalFunction::ThreadSuspend { cancellable } => {
12691269
self.thread_suspend(cancellable, types, offset)
12701270
}
1271-
CanonicalFunction::ThreadResumeLater => self.thread_resume_later(types, offset),
1271+
CanonicalFunction::ThreadSuspendTo { cancellable } => {
1272+
self.thread_suspend_to(cancellable, types, offset)
1273+
}
1274+
CanonicalFunction::ThreadUnsuspend => self.thread_unsuspend(types, offset),
12721275

1273-
CanonicalFunction::ThreadYieldTo { cancellable } => {
1274-
self.thread_yield_to(cancellable, types, offset)
1276+
CanonicalFunction::ThreadYieldToSuspended { cancellable } => {
1277+
self.thread_yield_to_suspended(cancellable, types, offset)
12751278
}
12761279
}
12771280
}
@@ -2176,7 +2179,7 @@ impl ComponentState {
21762179
Ok(())
21772180
}
21782181

2179-
fn thread_switch_to(
2182+
fn thread_suspend_to_suspended(
21802183
&mut self,
21812184
_cancellable: bool,
21822185
types: &mut TypeAlloc,
@@ -2185,7 +2188,7 @@ impl ComponentState {
21852188
if !self.features.cm_threading() {
21862189
bail!(
21872190
offset,
2188-
"`thread.switch_to` requires the component model threading feature"
2191+
"`thread.suspend_to_suspended` requires the component model threading feature"
21892192
)
21902193
}
21912194

@@ -2211,19 +2214,36 @@ impl ComponentState {
22112214
Ok(())
22122215
}
22132216

2214-
fn thread_resume_later(&mut self, types: &mut TypeAlloc, offset: usize) -> Result<()> {
2217+
fn thread_suspend_to(
2218+
&mut self,
2219+
_cancellable: bool,
2220+
types: &mut TypeAlloc,
2221+
offset: usize,
2222+
) -> Result<()> {
2223+
if !self.features.cm_threading() {
2224+
bail!(
2225+
offset,
2226+
"`thread.suspend_to` requires the component model threading feature"
2227+
)
2228+
}
2229+
self.core_funcs
2230+
.push(types.intern_func_type(FuncType::new([ValType::I32], [ValType::I32]), offset));
2231+
Ok(())
2232+
}
2233+
2234+
fn thread_unsuspend(&mut self, types: &mut TypeAlloc, offset: usize) -> Result<()> {
22152235
if !self.features.cm_threading() {
22162236
bail!(
22172237
offset,
2218-
"`thread.resume_later` requires the component model threading feature"
2238+
"`thread.unsuspend` requires the component model threading feature"
22192239
)
22202240
}
22212241
self.core_funcs
22222242
.push(types.intern_func_type(FuncType::new([ValType::I32], []), offset));
22232243
Ok(())
22242244
}
22252245

2226-
fn thread_yield_to(
2246+
fn thread_yield_to_suspended(
22272247
&mut self,
22282248
_cancellable: bool,
22292249
types: &mut TypeAlloc,
@@ -2232,7 +2252,7 @@ impl ComponentState {
22322252
if !self.features.cm_threading() {
22332253
bail!(
22342254
offset,
2235-
"`thread.yield_to` requires the component model threading feature"
2255+
"`thread.yield_to_suspended` requires the component model threading feature"
22362256
)
22372257
}
22382258
self.core_funcs

crates/wasmprinter/src/component.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,8 +1163,8 @@ impl Printer<'_, '_> {
11631163
me.end_group()
11641164
})?;
11651165
}
1166-
CanonicalFunction::ThreadSwitchTo { cancellable } => {
1167-
self.print_intrinsic(state, "canon thread.switch-to", &|me, _| {
1166+
CanonicalFunction::ThreadSuspendToSuspended { cancellable } => {
1167+
self.print_intrinsic(state, "canon thread.suspend-to-suspended", &|me, _| {
11681168
if cancellable {
11691169
me.result.write_str(" cancellable")?;
11701170
}
@@ -1179,11 +1179,19 @@ impl Printer<'_, '_> {
11791179
Ok(())
11801180
})?;
11811181
}
1182-
CanonicalFunction::ThreadResumeLater => {
1183-
self.print_intrinsic(state, "canon thread.resume-later", &|_, _| Ok(()))?;
1182+
CanonicalFunction::ThreadSuspendTo { cancellable } => {
1183+
self.print_intrinsic(state, "canon thread.suspend-to", &|me, _| {
1184+
if cancellable {
1185+
me.result.write_str(" cancellable")?;
1186+
}
1187+
Ok(())
1188+
})?;
1189+
}
1190+
CanonicalFunction::ThreadUnsuspend => {
1191+
self.print_intrinsic(state, "canon thread.unsuspend", &|_, _| Ok(()))?;
11841192
}
1185-
CanonicalFunction::ThreadYieldTo { cancellable } => {
1186-
self.print_intrinsic(state, "canon thread.yield-to", &|me, _| {
1193+
CanonicalFunction::ThreadYieldToSuspended { cancellable } => {
1194+
self.print_intrinsic(state, "canon thread.yield-to-suspended", &|me, _| {
11871195
if cancellable {
11881196
me.result.write_str(" cancellable")?;
11891197
}

crates/wast/src/component/binary.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,21 +517,25 @@ impl<'a> Encoder<'a> {
517517
self.funcs
518518
.thread_new_indirect(info.ty.into(), info.table.idx.into());
519519
}
520-
CoreFuncKind::ThreadSwitchTo(info) => {
520+
CoreFuncKind::ThreadSuspendToSuspended(info) => {
521521
self.core_func_names.push(name);
522-
self.funcs.thread_switch_to(info.cancellable);
522+
self.funcs.thread_suspend_to_suspended(info.cancellable);
523523
}
524524
CoreFuncKind::ThreadSuspend(info) => {
525525
self.core_func_names.push(name);
526526
self.funcs.thread_suspend(info.cancellable);
527527
}
528-
CoreFuncKind::ThreadResumeLater => {
528+
CoreFuncKind::ThreadSuspendTo(info) => {
529529
self.core_func_names.push(name);
530-
self.funcs.thread_resume_later();
530+
self.funcs.thread_suspend_to(info.cancellable);
531531
}
532-
CoreFuncKind::ThreadYieldTo(info) => {
532+
CoreFuncKind::ThreadUnsuspend => {
533533
self.core_func_names.push(name);
534-
self.funcs.thread_yield_to(info.cancellable);
534+
self.funcs.thread_unsuspend();
535+
}
536+
CoreFuncKind::ThreadYieldToSuspended(info) => {
537+
self.core_func_names.push(name);
538+
self.funcs.thread_yield_to_suspended(info.cancellable);
535539
}
536540
},
537541
}

0 commit comments

Comments
 (0)