From 72e752090ec66fef761bca4a66d1d44ee250c20f Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Sat, 29 Aug 2026 14:46:33 -0700 Subject: [PATCH] Manual `Send` and `Sync` for `Hub` and `Command{Encoder,Buffer}` --- wgpu-core/src/command/mod.rs | 40 +++++++++++++++++++++++++ wgpu-core/src/hub.rs | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index b9c9a6f4d..44b47c721 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -534,6 +534,26 @@ crate::impl_labeled!(CommandEncoder); crate::impl_parent_device!(CommandEncoder); crate::impl_storage_item!(CommandEncoder); +#[cfg(send_sync)] +const _: () = { + // SAFETY: Bounds checked below + unsafe impl Send for CommandEncoder {} + // SAFETY: Bounds checked below + unsafe impl Sync for CommandEncoder {} + + fn _command_encoder_fields_are_send_sync(obj: &CommandEncoder) { + fn _check_bound(_: &T) {} + let CommandEncoder { + device, + label, + data, + } = obj; + _check_bound(device); + _check_bound(label); + _check_bound(data); + } +}; + impl Drop for CommandEncoder { fn drop(&mut self) { resource_log!("Drop {}", self.error_ident()); @@ -902,6 +922,26 @@ pub struct CommandBuffer { pub(crate) data: Mutex, } +#[cfg(send_sync)] +const _: () = { + // SAFETY: Bounds checked below + unsafe impl Send for CommandBuffer {} + // SAFETY: Bounds checked below + unsafe impl Sync for CommandBuffer {} + + fn _command_buffer_fields_are_send_sync(obj: &CommandBuffer) { + fn _check_bound(_: &T) {} + let CommandBuffer { + device, + label, + data, + } = obj; + _check_bound(device); + _check_bound(label); + _check_bound(data); + } +}; + impl Drop for CommandBuffer { fn drop(&mut self) { resource_log!("Drop {}", self.error_ident()); diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index d21e6565f..e61c8ea10 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -259,3 +259,61 @@ impl Hub { } } } + +#[cfg(send_sync)] +const _: () = { + // SAFETY: Bounds checked below + unsafe impl Send for Hub {} + // SAFETY: Bounds checked below + unsafe impl Sync for Hub {} + + fn _hub_fields_are_send_sync(hub: &Hub) { + fn _check_bound(_: &T) {} + let Hub { + adapters, + devices, + queues, + pipeline_layouts, + shader_modules, + bind_group_layouts, + bind_groups, + command_encoders, + command_buffers, + render_bundles, + render_pipelines, + compute_pipelines, + pipeline_caches, + query_sets, + buffers, + staging_buffers, + textures, + texture_views, + external_textures, + samplers, + blas_s, + tlas_s, + } = hub; + _check_bound(adapters); + _check_bound(devices); + _check_bound(queues); + _check_bound(pipeline_layouts); + _check_bound(shader_modules); + _check_bound(bind_group_layouts); + _check_bound(bind_groups); + _check_bound(command_encoders); + _check_bound(command_buffers); + _check_bound(render_bundles); + _check_bound(render_pipelines); + _check_bound(compute_pipelines); + _check_bound(pipeline_caches); + _check_bound(query_sets); + _check_bound(buffers); + _check_bound(staging_buffers); + _check_bound(textures); + _check_bound(texture_views); + _check_bound(external_textures); + _check_bound(samplers); + _check_bound(blas_s); + _check_bound(tlas_s); + } +};