You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current design for waitqueue has it be a subtype of (rec (type (struct (field (mut i32))))).0 so that normal struct accessors can be used to access its control word while still allowing the engine-internal implementation details to be co-located with that control word. One downside of this design is that it has a non-bottom abstract type as a subtype of a defined type, which has no precedent in the type system and would be difficult for V8 at least to implement.
An alternative design would be for waitqueue to be a packed type like i8 or i16. This would allow it to be "unboxed" directly into structs or arrays, where its control word could still be accessed directly with any struct or array accessor that is allowed to access i32s. The engine could still co-locate the implementation details with the control word since there's no way to observe the actual size of a packed type. We would need struct versions of wait and notify that take struct type and field index immediates, just like any other struct accessor, and array versions that take an array type immediate and an index operand, just like any other array accessor.
We discussed this in the threads subgroup. @rossberg had some concerns about how it would complicate the type algebra. V8 will go ahead and prototype this design, though, because the original design with a separate waitqueue heap type complicated the type algebra in a way that made implementation more difficult.
Yeah, this worries me in a couple of ways. One is just the complexity it adds to the type algebra, as mentioned. Even more serious is what I think @eqrion brought up at the meeting: such entries cannot be copied, at least not transparently.
That breaks a property that was never explicitly expressed about the Wasm data model, but nevertheless seems rather central to it. Namely, that all values and storage types can freely be moved and copied — either because they are transparent to boot (numbers, vectors) or because they are always by reference. Adding a new species of non-copyable reference type might have plenty of non-obvious larger-scale implications and complications down the line. Will this, say, force some variant of move semantics eventually?
One alternative mentioned in the meeting was to make waitqueue its own heap type, with dedicated instructions for accessing the control value. From my perspective, that would obviously be the cleanest approach. The downside you mentioned was that we'd need to add multiple get/set instructions to access the control word. To be honest, compared to the alternative above, that still looks vastly simpler to me. How many instructions are we talking?
No matter how we add waitqueue, its values will be able to be moved (unless I'm forgetting something about its implementation that would prevent that) but not copied. I don't think there's any way around that. If we do add it as a packed type, there are a couple possibilities:
We allow waitqueue in both structs and arrays. Array copies on waitqueue arrays are specified and implemented as copying control words and freshly initializing the internal part of the waitqueue.
We restrict its use to structs, since there is no copy operation on those anyway. Any future copy operation would have to be specified and implemented to copy only the control word (but I don't think it's likely that we would add such an operation, so this seems fine to me).
If we instead implement waitqueue as its own heap type, there are again a couple possibilities:
We make it a subtype of (rec (type (struct (field (mut i32))))).0 and reuse all the existing struct accessors for mutable i32 fields. This is the design currently documented in the Overview, and the one we are trying to avoid because it makes real implementations of the type system complicated.
We make it a new type in a new type hierarchy and add accessor instructions. I expect we would want full parity with struct i32 accessors, so that would be get, set, and a copy of all RMW operations. That's about 9 new instructions (times all the possible memory orderings) just for manipulating the control word. I would strongly prefer not going with this design.
No matter how we add waitqueue, its values will be able to be moved (unless I'm forgetting something about its implementation that would prevent that) but not copied. I don't think there's any way around that.
Hold on, as regular refs, the value is just the reference. That can be freely moved and copied, just like any other. It's only the pointed-to memory that can't, but that's no different from function references or the likes. That's unproblematic and consistent with the language.
If we instead implement waitqueue as its own heap type, there are again a couple possibilities
There is a third one I think: box the int field. The waitqueue is a new opaque heap type, but not with an int field. Instead it carries a reference to a struct containing the int field. That reference is immutable, so only needs a trivial accessor. The struct referenced otoh is a completely regular (final) struct, so existing accessors apply. The downside is that this involves another indirection.
And a fourth one: make waitqueue a defined type. Then the struct supertype isn't magic, but declared like all other concrete supertypes. The waitqueue reference then is a normal concrete heaptype reference, preserving the property that it has only a bottom subtype. This would actually by my preference — all non-trivial heap types should be defined types, which avoids most problems with the type system. (FWIW, we followed that route for continuation types as well.)
No matter how we add waitqueue, its values will be able to be moved (unless I'm forgetting something about its implementation that would prevent that) but not copied. I don't think there's any way around that.
Hold on, as regular refs, the value is just the reference. That can be freely moved and copied, just like any other. It's only the pointed-to memory that can't, but that's no different from function references or the likes. That's unproblematic and consistent with the language.
Yes, I agree, and it seems that having waitqueue as a packedtype in struct fields is unproblematic for the same reason.
rather than waitqueue being a name for an i32 field with an implicit internal field following it, waitqueue is now a magic name for just the implicit internal field that's being appended to the i32 field. Formally it's a new parameter-less "comptype", but in implementations it'll probably be interpreted as a shorthand for (struct (field i32) (field internal_waitqueue)) to keep things (somewhat) uniform.
restriction: waitqueue fields can only be used in structs (i.e. not in arrays).
restriction: when a struct has a waitqueue field, then that must be its last field (and must only be preceded by a single i32 field).
So, unless I'm missing something, this shouldn't make much of a difference for implementations compared to saying "waitqueue is a packed type that can only occur in structs and must be the only field in the struct". Seems doable.
We have a comptype that cannot exist autonomously and also can extend a comptype of different type.
We have to somehow link a field defined in the parent struct to the waitqueue to use as a control word.
I do not see how it addresses @rossberg's concerns. We still have a non-copyable field in a struct. It seems the same as the packed-type idea if we restrict it to structs only.
We have a comptype that cannot exist autonomously and also can extend a comptype of different type.
By "exist autonomously" you mean without a supertype? Actually, that isn't necessary, but when you provide no supertype then you simply cannot access the control field.
We have to somehow link a field defined in the parent struct to the waitqueue to use as a control word.
Yes, but that's just subtyping.
I do not see how it addresses @rossberg's concerns. We still have a non-copyable field in a struct. It seems the same as the packed-type idea if we restrict it to structs only.
No, we don't have non-copyable fields this way. The internal state is not part of any field and cannot be used as such. It cannot become part of a regular struct or array by construction, it remains encapsulated as private state in the waitqueue object. This completely avoids the introduction of any irregular values or field values, as well as any type system machinery tracking and restricting their presence.
So if I understand the state of a 'waitqueue', it's both:
A user mutable 'control word' which is part of the wait/notify semantics.
An engine hidden 'waiter queue'
So if this is a packed type, it can go in struct and array fields, but not in value types such as locals/globals/operand stack.
What happens if you struct.get the waitqueue type? That must return a value type. I believe the idea is that we just return the control word, not the engine hidden queue.
What happens if you array.copy an (array (mut waitqueue))? That's a direct packed type to packed type operation (no loss of information from going to val type), so does it copy the engine 'waiter queue'? Copying the queue in SM today is not feasible. Not copying the queue feels like you're not actually copying the field. In no way would the src/dest be equivalent after the operation. We'd also need specialized copying routines that skip over the internal fields.
I agree with Rossberg and would prefer the two options he listed in #102 (comment).
I actually prefer (4) over (3). Add a heaptype for 'waitqueueref' that is constructed to point at a struct or array that contains the control word. Then we don't need any new instructions for mutating the control word, or a new defined type. There's technically a layer of indirection, but I can't imagine that ever being significant. If we implemented it as a field we would likely have just as many indirections in our implementation.
If you have the waitqueue pointing at an array (or slice: WebAssembly/design#1555), I think we can make it symmetrical to how a shared memory has an associated queue and many different offsets to wait on. Having it point at a struct with a single field is fine too.
I actually prefer (4) over (3). Add a heaptype for 'waitqueueref' that is constructed to point at a struct or array that contains the control word.
Did you mean (3) over (4)? That certainly is the simplest solution in terms of type system machinery. It has an extra indirection, but if that were not to matter, it would perhaps be the best option.
I have an action item from the last threads subgroup meeting to make a table with all the design options and trade offs so far. But before that, I want to pitch a new design based on the realization towards the end of that meeting that the control word does not need to be bundled with the waitqueue in any way. I think this design is pretty much a slam dunk.
The idea is that there is a new waitqueue heap type that is totally opaque. You can notify a waitqueue and you can pass it as an operand to wait instructions that also receive the locations of control words to look at.
New waitqueue heap type hierarchy (with a similar structure to the exn hierarchy):
waitqueue
|
nowaitqueue
New instructions:
;; Allocate a waitqueue.
waitqueue.new : [] -> [(ref waitqueue)]
;; Wake up to a given number of waiters on a waitqueue.
;; Return the number woken.
waitqueue.notify : [(ref null waitqueue) i32] -> [i32]
;; Atomically check the value of a struct field against an expected value
;; and sleep for a given timeout if they match. Return value the same as
;; memory.atomic.wait32.
struct.wait x y : [(ref null x) (ref null waitqueue) i32 i64] -> [i32]
-- C.types[x] ~~ struct ft*
-- ft[y] = mut? i32
;; Same as struct.wait, but for a control word in an array.
array.wait x : [(ref null x) (ref null waitqueue) i32 i32 i64] -> [i32]
-- C.types[x] ~~ array mut? i32
I used i32 control words here for simplicity, but we should allow struct.wait and array.wait to accept control words of any field type that we allow with struct.atomic.rmw.cmpxchg and array.atomic.rmw.cmpxchg, namely i32, i64, and anything matching eqref.
Reacted by Ryan Hunt, Manos Koukoutos and Andreas Rossberg
Oh, indeed, that makes perfect sense and is universally better than anything else we've thrown around! (You can probably save the work of collecting trade-offs. :) )
Don't we also need table.wait for the complete set?
As usual, one benefit (in the abstract) of it getting its own hierarchy is that it's allowed to have a different representation. I don't know if anyone would benefit from that in practice, though. I don't think there's any need to expose these to JS, and having them in their own hierarchy makes it easier to exclude them on the JS boundary because you don't need to dynamically check for them when an anyref crosses over to JS.
A missing piece of this story is some version Atomics.waitAsync for waitqueues, but I have some ideas for how we might address that on the Wasm side as a follow-up to stack switching / JSPI, so perhaps we don't need to solve that problem here.
Theoretically, could we have a memory.get_waitqueue instruction which returns a ref to the underlying waitqueue for a linear memory? And then we could rationalize the existing wait instructions as first acquiring the singleton waitqueue for a memory, and then performing a wait using it (we could have new instructions for these too).
I don't think there's a pragmatic benefit to that except for conceptual simplicity. TBH, we'd probably internally refactor our implementation to work that way either way.
Oh yes, and we could of course have memory.wait variants, that take a waitqueueref instead of an address.
Actually letting users get a reference to the underlying waitqueue with memory.get_waitqueue sounds a little scary, though. For one thing, it would force implementations to do the refactoring you mentioned. It certainly seems nice that implementations could do that refactoring, but I don't know that we want to force them to do so.
Activity
tlively commentedon Feb 7, 2026
We discussed this in the threads subgroup. @rossberg had some concerns about how it would complicate the type algebra. V8 will go ahead and prototype this design, though, because the original design with a separate waitqueue heap type complicated the type algebra in a way that made implementation more difficult.
rossberg commentedon Feb 9, 2026
Yeah, this worries me in a couple of ways. One is just the complexity it adds to the type algebra, as mentioned. Even more serious is what I think @eqrion brought up at the meeting: such entries cannot be copied, at least not transparently.
That breaks a property that was never explicitly expressed about the Wasm data model, but nevertheless seems rather central to it. Namely, that all values and storage types can freely be moved and copied — either because they are transparent to boot (numbers, vectors) or because they are always by reference. Adding a new species of non-copyable reference type might have plenty of non-obvious larger-scale implications and complications down the line. Will this, say, force some variant of move semantics eventually?
One alternative mentioned in the meeting was to make waitqueue its own heap type, with dedicated instructions for accessing the control value. From my perspective, that would obviously be the cleanest approach. The downside you mentioned was that we'd need to add multiple get/set instructions to access the control word. To be honest, compared to the alternative above, that still looks vastly simpler to me. How many instructions are we talking?
[wasm][shared] Add waitqueue type and get/set
tlively commentedon Feb 9, 2026
No matter how we add waitqueue, its values will be able to be moved (unless I'm forgetting something about its implementation that would prevent that) but not copied. I don't think there's any way around that. If we do add it as a packed type, there are a couple possibilities:
If we instead implement waitqueue as its own heap type, there are again a couple possibilities:
(rec (type (struct (field (mut i32))))).0and reuse all the existing struct accessors for mutablei32fields. This is the design currently documented in the Overview, and the one we are trying to avoid because it makes real implementations of the type system complicated.rossberg commentedon Feb 9, 2026
@tlively:
Hold on, as regular refs, the value is just the reference. That can be freely moved and copied, just like any other. It's only the pointed-to memory that can't, but that's no different from function references or the likes. That's unproblematic and consistent with the language.
There is a third one I think: box the int field. The waitqueue is a new opaque heap type, but not with an int field. Instead it carries a reference to a struct containing the int field. That reference is immutable, so only needs a trivial accessor. The struct referenced otoh is a completely regular (final) struct, so existing accessors apply. The downside is that this involves another indirection.
And a fourth one: make waitqueue a defined type. Then the struct supertype isn't magic, but declared like all other concrete supertypes. The waitqueue reference then is a normal concrete heaptype reference, preserving the property that it has only a bottom subtype. This would actually by my preference — all non-trivial heap types should be defined types, which avoids most problems with the type system. (FWIW, we followed that route for continuation types as well.)
tlively commentedon Feb 9, 2026
Yes, I agree, and it seems that having waitqueue as a packedtype in struct fields is unproblematic for the same reason.
What would this look like? Maybe this:
And then the subtyping rules would be that the supertype could be any struct type with only a single (mutable?) i32 field?
That seems plausible. @jakobkummerow, @Liedtke, @manoskouk, wdyt?
rossberg commentedon Feb 9, 2026
No, because the field's value is not copyable. That's what breaks array.copy and possibly other things.
Yes. Still a slight hack, but at least a fairly small and confined one.
jakobkummerow commentedon Feb 9, 2026
IIUC, that's basically equivalent to the packed-type idea:
(type $my_waitqueue (sub $some-super (struct (field waitqueue))))except for these differences:
waitqueuebeing a name for ani32field with an implicit internal field following it,waitqueueis now a magic name for just the implicit internal field that's being appended to thei32field. Formally it's a new parameter-less "comptype", but in implementations it'll probably be interpreted as a shorthand for(struct (field i32) (field internal_waitqueue))to keep things (somewhat) uniform.waitqueuefields can only be used in structs (i.e. not in arrays).waitqueuefield, then that must be its last field (and must only be preceded by a singlei32field).So, unless I'm missing something, this shouldn't make much of a difference for implementations compared to saying "
waitqueueis a packed type that can only occur in structs and must be the only field in the struct". Seems doable.manoskouk commentedon Feb 10, 2026
I am not sure how this formulation is cleaner.
rossberg commentedon Feb 10, 2026
@manoskouk:
By "exist autonomously" you mean without a supertype? Actually, that isn't necessary, but when you provide no supertype then you simply cannot access the control field.
Yes, but that's just subtyping.
No, we don't have non-copyable fields this way. The internal state is not part of any field and cannot be used as such. It cannot become part of a regular struct or array by construction, it remains encapsulated as private state in the waitqueue object. This completely avoids the introduction of any irregular values or field values, as well as any type system machinery tracking and restricting their presence.
eqrion commentedon Mar 3, 2026
So if I understand the state of a 'waitqueue', it's both:
So if this is a packed type, it can go in struct and array fields, but not in value types such as locals/globals/operand stack.
What happens if you
struct.getthe waitqueue type? That must return a value type. I believe the idea is that we just return the control word, not the engine hidden queue.What happens if you
array.copyan(array (mut waitqueue))? That's a direct packed type to packed type operation (no loss of information from going to val type), so does it copy the engine 'waiter queue'? Copying the queue in SM today is not feasible. Not copying the queue feels like you're not actually copying the field. In no way would the src/dest be equivalent after the operation. We'd also need specialized copying routines that skip over the internal fields.I agree with Rossberg and would prefer the two options he listed in #102 (comment).
I actually prefer (4) over (3). Add a heaptype for 'waitqueueref' that is constructed to point at a struct or array that contains the control word. Then we don't need any new instructions for mutating the control word, or a new defined type. There's technically a layer of indirection, but I can't imagine that ever being significant. If we implemented it as a field we would likely have just as many indirections in our implementation.
If you have the waitqueue pointing at an array (or slice: WebAssembly/design#1555), I think we can make it symmetrical to how a shared memory has an associated queue and many different offsets to wait on. Having it point at a struct with a single field is fine too.
rossberg commentedon Mar 3, 2026
@eqrion:
Did you mean (3) over (4)? That certainly is the simplest solution in terms of type system machinery. It has an extra indirection, but if that were not to matter, it would perhaps be the best option.
Option 3:
waitqueue,nowaitqueuewaitqueue.get_ctrl $t : (ref null waitqueue) → (ref $t)— (if$t = (struct (mut i32)))Option 4:
waitqueuewaitqueue <: struct (mut i32)eqrion commentedon Mar 3, 2026
Ah yes, that was a typo. I edited the comment.
tlively commentedon Mar 11, 2026
I have an action item from the last threads subgroup meeting to make a table with all the design options and trade offs so far. But before that, I want to pitch a new design based on the realization towards the end of that meeting that the control word does not need to be bundled with the waitqueue in any way. I think this design is pretty much a slam dunk.
The idea is that there is a new
waitqueueheap type that is totally opaque. You can notify a waitqueue and you can pass it as an operand to wait instructions that also receive the locations of control words to look at.New
waitqueueheap type hierarchy (with a similar structure to theexnhierarchy):New instructions:
I used i32 control words here for simplicity, but we should allow
struct.waitandarray.waitto accept control words of any field type that we allow withstruct.atomic.rmw.cmpxchgandarray.atomic.rmw.cmpxchg, namelyi32,i64, and anything matchingeqref.rossberg commentedon Mar 12, 2026
Oh, indeed, that makes perfect sense and is universally better than anything else we've thrown around! (You can probably save the work of collecting trade-offs. :) )
Don't we also need
table.waitfor the complete set?tlively commentedon Mar 12, 2026
Ah yes, I was just thinking about shared GC objects, but we could add
table.waitandglobal.waitonce we have shared tables and globals, too.eqrion commentedon Mar 12, 2026
Is there a benefit to having it in its own hierarchy? Will these be exposed to JS?
I think a subtype of 'any' or 'eq' would make sense for this (unless we don't want these in JS, in which case they need their own hierarchy).
tlively commentedon Mar 12, 2026
As usual, one benefit (in the abstract) of it getting its own hierarchy is that it's allowed to have a different representation. I don't know if anyone would benefit from that in practice, though. I don't think there's any need to expose these to JS, and having them in their own hierarchy makes it easier to exclude them on the JS boundary because you don't need to dynamically check for them when an anyref crosses over to JS.
A missing piece of this story is some version
Atomics.waitAsyncfor waitqueues, but I have some ideas for how we might address that on the Wasm side as a follow-up to stack switching / JSPI, so perhaps we don't need to solve that problem here.eqrion commentedon Mar 12, 2026
Theoretically, could we have a
memory.get_waitqueueinstruction which returns a ref to the underlying waitqueue for a linear memory? And then we could rationalize the existing wait instructions as first acquiring the singleton waitqueue for a memory, and then performing a wait using it (we could have new instructions for these too).I don't think there's a pragmatic benefit to that except for conceptual simplicity. TBH, we'd probably internally refactor our implementation to work that way either way.
tlively commentedon Mar 12, 2026
Oh yes, and we could of course have
memory.waitvariants, that take awaitqueuerefinstead of an address.Actually letting users get a reference to the underlying waitqueue with
memory.get_waitqueuesounds a little scary, though. For one thing, it would force implementations to do the refactoring you mentioned. It certainly seems nice that implementations could do that refactoring, but I don't know that we want to force them to do so.Document the new waitqueue design
Document the new waitqueue design (#110)
Updated waitqueue support (#8672)