Skip to content

Conversation

@dotcarmen
Copy link

@dotcarmen dotcarmen commented Nov 8, 2025

This PR implements wgpuInstanceWaitAny and modifies already-implemented functions that rely on this functionality.

Apologies for the large diff. It's mostly contained to the first commit, which refactors Context to be a struct that contains wgc::global::Global instead of being an alias to that type.

Additionally, this PR somewhat replicates logic that's already implemented in wgc, but are hidden from the public api (aka declared using pub(crate) and are not re-exported elsewhere). Unfortunately, this includes wgc::lock, which I didn't duplicate here - instead, I simply rely on parking_lot without incorporating the feature-gated implementations from wgc.

@dotcarmen dotcarmen force-pushed the implement-futures branch 4 times, most recently from 3bf7093 to 57f9957 Compare November 8, 2025 16:33
@dotcarmen dotcarmen marked this pull request as ready for review November 8, 2025 16:42
@dotcarmen
Copy link
Author

i won't keep force-pushing now that it's open for review - was trying to keep the set of commits small for maintainers' convenience :) i recommend looking at the 2nd and 3rd commits to isolate away the changes from the refactor


pub type Context = wgc::global::Global;
pub struct Context {
global: wgc::global::Global,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can reduce the diff with impl Deref, let me know if this is preferred :)

Comment on lines +4792 to +4795
let now = std::time::Instant::now();
if now - start >= std::time::Duration::from_nanos(timeout_ns) {
return native::WGPUWaitStatus_TimedOut;
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i figured it's simple to keep this as a hot loop than to park the thread, but i can also replace the whole loop with:

let mut success = false;
{
  let registry = context.futures.read();
  for future in futures.iter_mut() {
    // ... the same
  }
}

if success {
  return native::WGPUWaitStatus_Success;
}

std::thread::sleep(timeout_ns);
let registry = context.futures.read();
for future in futures.iter_mut() {
  // ... the same
}

return if success { native::WGPUWaitStatus_Success } else { native::WGPUWaitStatus_TimedOut };

Comment on lines +643 to +644
self.futures
.resize_with(idx as usize + 1, || FutureElement::Vacant);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg

let mut reg = FutureRegistry::default();
for _ in 0..253 {
  reg.completed_future();
}

// still 0 allocations, `reg.futures.len() == 0`
reg.completed_future();
// 1 allocation, `reg.futures.len() == 255`
reg.incomplete_future();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait no, reg.futures.len() == 1 in the 2nd scenario since the underlying IdentityManager is able to only increment the epoch 😅

@dotcarmen
Copy link
Author

making this a draft again - i need to make sure to call poll functions appropriately

@dotcarmen dotcarmen marked this pull request as draft November 8, 2025 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant