Skip to content

Waitqueue as a packed type #102

Closed

Description

@tlively

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.

Activity

  1. tlively commented on Feb 7, 2026

    @tlively
    MemberAuthor

    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.

  2. rossberg commented on Feb 9, 2026

    @rossberg
    Member

    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?

  3. tlively commented on Feb 9, 2026

    @tlively
    MemberAuthor

    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:

    1. 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.
    2. 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:

    1. 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.
    2. 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.
  4. rossberg commented on Feb 9, 2026

    @rossberg
    Member

    @tlively:

    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.)

  5. tlively commented on Feb 9, 2026

    @tlively
    MemberAuthor

    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.

    make waitqueue a defined type.

    What would this look like? Maybe this:

    (type $my_waitqueue (sub $one-i32-field waitqueue))
    

    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?

  6. rossberg commented on Feb 9, 2026

    @rossberg
    Member

    Yes, I agree, and it seems that having waitqueue as a packedtype in struct fields is unproblematic for the same reason.

    No, because the field's value is not copyable. That's what breaks array.copy and possibly other things.

    What would this look like? Maybe this:

    (type $my_waitqueue (sub $one-i32-field waitqueue))
    

    And then the subtyping rules would be that the supertype could be any struct type with only a single (mutable?) i32 field?

    Yes. Still a slight hack, but at least a fairly small and confined one.

  7. jakobkummerow commented on Feb 9, 2026

    @jakobkummerow
    Contributor

    (type $my_waitqueue (sub $one-i32-field waitqueue))

    IIUC, that's basically equivalent to the packed-type idea:

    (type $my_waitqueue (sub $some-super (struct (field waitqueue))))

    except for these differences:

    • 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.

  8. manoskouk commented on Feb 10, 2026

    @manoskouk

    I am not sure how this formulation is cleaner.

    1. We have a comptype that cannot exist autonomously and also can extend a comptype of different type.
    2. We have to somehow link a field defined in the parent struct to the waitqueue to use as a control word.
    3. 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.
  9. rossberg commented on Feb 10, 2026

    @rossberg
    Member

    @manoskouk:

    1. 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.

    1. 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.

    1. 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.

  10. eqrion commented on Mar 3, 2026

    @eqrion
    Contributor

    So if I understand the state of a 'waitqueue', it's both:

    1. A user mutable 'control word' which is part of the wait/notify semantics.
    2. 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.

  11. rossberg commented on Mar 3, 2026

    @rossberg
    Member

    @eqrion:

    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.

    Option 3:

    • new heaptypes waitqueue, nowaitqueue
    • new instruction waitqueue.get_ctrl $t : (ref null waitqueue) → (ref $t) — (if $t = (struct (mut i32)))
    • for example:
      (type $ctrl (struct (field (mut i32)))
      (func $read_ctrl (param $wq (ref waitqueue)) (result i32)
          (struct.get $ctrl 0 (waitqueue.get_ctrl $ctrl (local.get $wq)))
      )
      (func $write_ctrl (param $wq (ref waitqueue)) (param $c i32)
          (struct.put $ctrl 0 (waitqueue.get_ctrl $ctrl (local.get $wq)) (local.get $c))
      )
      

    Option 4:

    • new deftype waitqueue
    • new subtype relation on deftypes waitqueue <: struct (mut i32)
    • for example:
      (type $ctrl (sub struct (field (mut i32))))
      (type $waitqueue (sub $ctrl waitqueue))
      (func $read_ctrl (param $wq (ref $waitqueue)) (result i32)
          (struct.get $ctrl 0 (local.get $wq))
      )
      (func $write_ctrl (param $wq (ref $waitqueue)) (param $c i32)
          (struct.put $ctrl 0 (local.get $wq) (local.get $c))
      )
      
  12. eqrion commented on Mar 3, 2026

    @eqrion
    Contributor

    Ah yes, that was a typo. I edited the comment.

  13. tlively commented on Mar 11, 2026

    @tlively
    MemberAuthor

    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.

  14. rossberg commented on Mar 12, 2026

    @rossberg
    Member

    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?

  15. tlively commented on Mar 12, 2026

    @tlively
    MemberAuthor

    Ah yes, I was just thinking about shared GC objects, but we could add table.wait and global.wait once we have shared tables and globals, too.

  16. eqrion commented on Mar 12, 2026

    @eqrion
    Contributor

    New waitqueue heap type hierarchy (with a similar structure to the exn hierarchy

    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).

  17. tlively commented on Mar 12, 2026

    @tlively
    MemberAuthor

    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.

  18. eqrion commented on Mar 12, 2026

    @eqrion
    Contributor

    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.

  19. tlively commented on Mar 12, 2026

    @tlively
    MemberAuthor

    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.

  20. added a commit that references this issue on Apr 9, 2026
    50eb3fb
  21. added a commit that references this issue on Apr 28, 2026
    4f73b3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions