Skip to content

Commit 1e99cd8

Browse files
committed
Don't implement Send or Sync for Wasm types
1 parent bf8e6fe commit 1e99cd8

File tree

16 files changed

+481
-452
lines changed

16 files changed

+481
-452
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ By @cwfitzgerald in [#3671](https://github.com/gfx-rs/wgpu/pull/3671).
144144
- Don't include ANSI terminal color escape sequences in shader module validation error messages. By @jimblandy in [#3591](https://github.com/gfx-rs/wgpu/pull/3591)
145145
- Report error messages from DXC compile. By @Davidster in [#3632](https://github.com/gfx-rs/wgpu/pull/3632)
146146
- Error in native when using a filterable `TextureSampleType::Float` on a multisample `BindingType::Texture`. By @mockersf in [#3686](https://github.com/gfx-rs/wgpu/pull/3686)
147+
- On Web, types don't implement `Send` or `Sync` anymore. By @daxpedda in [#3691](https://github.com/gfx-rs/wgpu/pull/3691)
147148

148149
#### WebGPU
149150

wgpu-core/src/command/bundle.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,9 @@ pub struct RenderBundle<A: HalApi> {
743743
pub(crate) life_guard: LifeGuard,
744744
}
745745

746+
#[cfg(not(target_arch = "wasm32"))]
746747
unsafe impl<A: HalApi> Send for RenderBundle<A> {}
748+
#[cfg(not(target_arch = "wasm32"))]
747749
unsafe impl<A: HalApi> Sync for RenderBundle<A> {}
748750

749751
impl<A: HalApi> RenderBundle<A> {

wgpu-core/src/device/queue.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct SubmittedWorkDoneClosureC {
3434
pub user_data: *mut u8,
3535
}
3636

37+
#[cfg(not(target_arch = "wasm32"))]
3738
unsafe impl Send for SubmittedWorkDoneClosureC {}
3839

3940
pub struct SubmittedWorkDoneClosure {
@@ -42,17 +43,18 @@ pub struct SubmittedWorkDoneClosure {
4243
inner: SubmittedWorkDoneClosureInner,
4344
}
4445

46+
#[cfg(not(target_arch = "wasm32"))]
47+
type SubmittedWorkDoneCallback = Box<dyn FnOnce() + Send + 'static>;
48+
#[cfg(target_arch = "wasm32")]
49+
type SubmittedWorkDoneCallback = Box<dyn FnOnce() + 'static>;
50+
4551
enum SubmittedWorkDoneClosureInner {
46-
Rust {
47-
callback: Box<dyn FnOnce() + Send + 'static>,
48-
},
49-
C {
50-
inner: SubmittedWorkDoneClosureC,
51-
},
52+
Rust { callback: SubmittedWorkDoneCallback },
53+
C { inner: SubmittedWorkDoneClosureC },
5254
}
5355

5456
impl SubmittedWorkDoneClosure {
55-
pub fn from_rust(callback: Box<dyn FnOnce() + Send + 'static>) -> Self {
57+
pub fn from_rust(callback: SubmittedWorkDoneCallback) -> Self {
5658
Self {
5759
inner: SubmittedWorkDoneClosureInner::Rust { callback },
5860
}

wgpu-core/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ pub fn format_pretty_any(
165165
#[derive(Debug)]
166166
pub struct ContextError {
167167
pub string: &'static str,
168+
#[cfg(not(target_arch = "wasm32"))]
168169
pub cause: Box<dyn Error + Send + Sync + 'static>,
170+
#[cfg(target_arch = "wasm32")]
171+
pub cause: Box<dyn Error + 'static>,
169172
pub label_key: &'static str,
170173
pub label: String,
171174
}

wgpu-core/src/resource.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ pub(crate) enum BufferMapState<A: hal::Api> {
6565
Idle,
6666
}
6767

68+
#[cfg(not(target_arch = "wasm32"))]
6869
unsafe impl<A: hal::Api> Send for BufferMapState<A> {}
70+
#[cfg(not(target_arch = "wasm32"))]
6971
unsafe impl<A: hal::Api> Sync for BufferMapState<A> {}
7072

7173
#[repr(C)]
@@ -74,6 +76,7 @@ pub struct BufferMapCallbackC {
7476
pub user_data: *mut u8,
7577
}
7678

79+
#[cfg(not(target_arch = "wasm32"))]
7780
unsafe impl Send for BufferMapCallbackC {}
7881

7982
pub struct BufferMapCallback {
@@ -82,17 +85,18 @@ pub struct BufferMapCallback {
8285
inner: Option<BufferMapCallbackInner>,
8386
}
8487

88+
#[cfg(not(target_arch = "wasm32"))]
89+
type BufferMapCallbackCallback = Box<dyn FnOnce(BufferAccessResult) + Send + 'static>;
90+
#[cfg(target_arch = "wasm32")]
91+
type BufferMapCallbackCallback = Box<dyn FnOnce(BufferAccessResult) + 'static>;
92+
8593
enum BufferMapCallbackInner {
86-
Rust {
87-
callback: Box<dyn FnOnce(BufferAccessResult) + Send + 'static>,
88-
},
89-
C {
90-
inner: BufferMapCallbackC,
91-
},
94+
Rust { callback: BufferMapCallbackCallback },
95+
C { inner: BufferMapCallbackC },
9296
}
9397

9498
impl BufferMapCallback {
95-
pub fn from_rust(callback: Box<dyn FnOnce(BufferAccessResult) + Send + 'static>) -> Self {
99+
pub fn from_rust(callback: BufferMapCallbackCallback) -> Self {
96100
Self {
97101
inner: Some(BufferMapCallbackInner::Rust { callback }),
98102
}

wgpu-hal/src/gles/adapter.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,12 +934,6 @@ impl super::AdapterShared {
934934
}
935935
}
936936

937-
// SAFE: WASM doesn't have threads
938-
#[cfg(target_arch = "wasm32")]
939-
unsafe impl Sync for super::Adapter {}
940-
#[cfg(target_arch = "wasm32")]
941-
unsafe impl Send for super::Adapter {}
942-
943937
#[cfg(test)]
944938
mod tests {
945939
use super::super::Adapter;

wgpu-hal/src/gles/device.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,9 +1320,3 @@ impl crate::Device<super::Api> for super::Device {
13201320
}
13211321
}
13221322
}
1323-
1324-
// SAFE: WASM doesn't have threads
1325-
#[cfg(target_arch = "wasm32")]
1326-
unsafe impl Sync for super::Device {}
1327-
#[cfg(target_arch = "wasm32")]
1328-
unsafe impl Send for super::Device {}

wgpu-hal/src/gles/mod.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,6 @@ pub struct Buffer {
246246
data: Option<Arc<std::sync::Mutex<Vec<u8>>>>,
247247
}
248248

249-
// Safe: WASM doesn't have threads
250-
#[cfg(target_arch = "wasm32")]
251-
unsafe impl Sync for Buffer {}
252-
#[cfg(target_arch = "wasm32")]
253-
unsafe impl Send for Buffer {}
254-
255249
#[derive(Clone, Debug)]
256250
pub enum TextureInner {
257251
Renderbuffer {
@@ -268,12 +262,6 @@ pub enum TextureInner {
268262
},
269263
}
270264

271-
// SAFE: WASM doesn't have threads
272-
#[cfg(target_arch = "wasm32")]
273-
unsafe impl Send for TextureInner {}
274-
#[cfg(target_arch = "wasm32")]
275-
unsafe impl Sync for TextureInner {}
276-
277265
impl TextureInner {
278266
fn as_native(&self) -> (glow::Texture, BindTarget) {
279267
match *self {
@@ -462,12 +450,6 @@ struct UniformDesc {
462450
utype: u32,
463451
}
464452

465-
// Safe: WASM doesn't have threads
466-
#[cfg(target_arch = "wasm32")]
467-
unsafe impl Sync for UniformDesc {}
468-
#[cfg(target_arch = "wasm32")]
469-
unsafe impl Send for UniformDesc {}
470-
471453
/// For each texture in the pipeline layout, store the index of the only
472454
/// sampler (in this layout) that the texture is used with.
473455
type SamplerBindMap = [Option<u8>; MAX_TEXTURE_SLOTS];
@@ -530,22 +512,10 @@ pub struct RenderPipeline {
530512
alpha_to_coverage_enabled: bool,
531513
}
532514

533-
// SAFE: WASM doesn't have threads
534-
#[cfg(target_arch = "wasm32")]
535-
unsafe impl Send for RenderPipeline {}
536-
#[cfg(target_arch = "wasm32")]
537-
unsafe impl Sync for RenderPipeline {}
538-
539515
pub struct ComputePipeline {
540516
inner: Arc<PipelineInner>,
541517
}
542518

543-
// SAFE: WASM doesn't have threads
544-
#[cfg(target_arch = "wasm32")]
545-
unsafe impl Send for ComputePipeline {}
546-
#[cfg(target_arch = "wasm32")]
547-
unsafe impl Sync for ComputePipeline {}
548-
549519
#[derive(Debug)]
550520
pub struct QuerySet {
551521
queries: Box<[glow::Query]>,
@@ -558,7 +528,9 @@ pub struct Fence {
558528
pending: Vec<(crate::FenceValue, glow::Fence)>,
559529
}
560530

531+
#[cfg(not(target_arch = "wasm32"))]
561532
unsafe impl Send for Fence {}
533+
#[cfg(not(target_arch = "wasm32"))]
562534
unsafe impl Sync for Fence {}
563535

564536
impl Fence {

wgpu-hal/src/gles/queue.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,9 +1524,3 @@ impl crate::Queue<super::Api> for super::Queue {
15241524
1.0
15251525
}
15261526
}
1527-
1528-
// SAFE: WASM doesn't have threads
1529-
#[cfg(target_arch = "wasm32")]
1530-
unsafe impl Sync for super::Queue {}
1531-
#[cfg(target_arch = "wasm32")]
1532-
unsafe impl Send for super::Queue {}

wgpu-hal/src/gles/web.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ impl Instance {
104104
}
105105
}
106106

107-
// SAFE: WASM doesn't have threads
108-
unsafe impl Sync for Instance {}
109-
unsafe impl Send for Instance {}
110-
111107
impl crate::Instance<super::Api> for Instance {
112108
unsafe fn init(_desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> {
113109
Ok(Instance {
@@ -168,10 +164,6 @@ pub struct Surface {
168164
srgb_present_program: Option<glow::Program>,
169165
}
170166

171-
// SAFE: Because web doesn't have threads ( yet )
172-
unsafe impl Sync for Surface {}
173-
unsafe impl Send for Surface {}
174-
175167
#[derive(Clone, Debug)]
176168
pub struct Swapchain {
177169
pub(crate) extent: wgt::Extent3d,

wgpu-hal/src/lib.rs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,25 @@ pub trait Api: Clone + Sized {
161161

162162
type Queue: Queue<Self>;
163163
type CommandEncoder: CommandEncoder<Self>;
164-
type CommandBuffer: Send + Sync + fmt::Debug;
164+
type CommandBuffer: MaybeSend + MaybeSync + fmt::Debug;
165165

166-
type Buffer: fmt::Debug + Send + Sync + 'static;
167-
type Texture: fmt::Debug + Send + Sync + 'static;
168-
type SurfaceTexture: fmt::Debug + Send + Sync + Borrow<Self::Texture>;
169-
type TextureView: fmt::Debug + Send + Sync;
170-
type Sampler: fmt::Debug + Send + Sync;
171-
type QuerySet: fmt::Debug + Send + Sync;
172-
type Fence: fmt::Debug + Send + Sync;
166+
type Buffer: fmt::Debug + MaybeSend + MaybeSync + 'static;
167+
type Texture: fmt::Debug + MaybeSend + MaybeSync + 'static;
168+
type SurfaceTexture: fmt::Debug + MaybeSend + MaybeSync + Borrow<Self::Texture>;
169+
type TextureView: fmt::Debug + MaybeSend + MaybeSync;
170+
type Sampler: fmt::Debug + MaybeSend + MaybeSync;
171+
type QuerySet: fmt::Debug + MaybeSend + MaybeSync;
172+
type Fence: fmt::Debug + MaybeSend + MaybeSync;
173173

174-
type BindGroupLayout: Send + Sync;
175-
type BindGroup: fmt::Debug + Send + Sync;
176-
type PipelineLayout: Send + Sync;
177-
type ShaderModule: fmt::Debug + Send + Sync;
178-
type RenderPipeline: Send + Sync;
179-
type ComputePipeline: Send + Sync;
174+
type BindGroupLayout: MaybeSend + MaybeSync;
175+
type BindGroup: fmt::Debug + MaybeSend + MaybeSync;
176+
type PipelineLayout: MaybeSend + MaybeSync;
177+
type ShaderModule: fmt::Debug + MaybeSend + MaybeSync;
178+
type RenderPipeline: MaybeSend + MaybeSync;
179+
type ComputePipeline: MaybeSend + MaybeSync;
180180
}
181181

182-
pub trait Instance<A: Api>: Sized + Send + Sync {
182+
pub trait Instance<A: Api>: Sized + MaybeSend + MaybeSync {
183183
unsafe fn init(desc: &InstanceDescriptor) -> Result<Self, InstanceError>;
184184
unsafe fn create_surface(
185185
&self,
@@ -190,7 +190,7 @@ pub trait Instance<A: Api>: Sized + Send + Sync {
190190
unsafe fn enumerate_adapters(&self) -> Vec<ExposedAdapter<A>>;
191191
}
192192

193-
pub trait Surface<A: Api>: Send + Sync {
193+
pub trait Surface<A: Api>: MaybeSend + MaybeSync {
194194
unsafe fn configure(
195195
&mut self,
196196
device: &A::Device,
@@ -216,7 +216,7 @@ pub trait Surface<A: Api>: Send + Sync {
216216
unsafe fn discard_texture(&mut self, texture: A::SurfaceTexture);
217217
}
218218

219-
pub trait Adapter<A: Api>: Send + Sync {
219+
pub trait Adapter<A: Api>: MaybeSend + MaybeSync {
220220
unsafe fn open(
221221
&self,
222222
features: wgt::Features,
@@ -240,7 +240,7 @@ pub trait Adapter<A: Api>: Send + Sync {
240240
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;
241241
}
242242

243-
pub trait Device<A: Api>: Send + Sync {
243+
pub trait Device<A: Api>: MaybeSend + MaybeSync {
244244
/// Exit connection to this logical device.
245245
unsafe fn exit(self, queue: A::Queue);
246246
/// Creates a new buffer.
@@ -336,7 +336,7 @@ pub trait Device<A: Api>: Send + Sync {
336336
unsafe fn stop_capture(&self);
337337
}
338338

339-
pub trait Queue<A: Api>: Send + Sync {
339+
pub trait Queue<A: Api>: MaybeSend + MaybeSync {
340340
/// Submits the command buffers for execution on GPU.
341341
///
342342
/// Valid usage:
@@ -360,7 +360,7 @@ pub trait Queue<A: Api>: Send + Sync {
360360
/// Serves as a parent for all the encoded command buffers.
361361
/// Works in bursts of action: one or more command buffers are recorded,
362362
/// then submitted to a queue, and then it needs to be `reset_all()`.
363-
pub trait CommandEncoder<A: Api>: Send + Sync + fmt::Debug {
363+
pub trait CommandEncoder<A: Api>: MaybeSend + MaybeSync + fmt::Debug {
364364
/// Begin encoding a new command buffer.
365365
unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;
366366
/// Discard currently recorded list, if any.
@@ -1309,6 +1309,28 @@ impl ValidationCanary {
13091309
}
13101310
}
13111311

1312+
use send_sync::*;
1313+
1314+
mod send_sync {
1315+
#[cfg(not(target_arch = "wasm32"))]
1316+
pub trait MaybeSend: Send {}
1317+
#[cfg(not(target_arch = "wasm32"))]
1318+
impl<T: Send> MaybeSend for T {}
1319+
#[cfg(target_arch = "wasm32")]
1320+
pub trait MaybeSend {}
1321+
#[cfg(target_arch = "wasm32")]
1322+
impl<T> MaybeSend for T {}
1323+
1324+
#[cfg(not(target_arch = "wasm32"))]
1325+
pub trait MaybeSync: Sync {}
1326+
#[cfg(not(target_arch = "wasm32"))]
1327+
impl<T: Sync> MaybeSync for T {}
1328+
#[cfg(target_arch = "wasm32")]
1329+
pub trait MaybeSync {}
1330+
#[cfg(target_arch = "wasm32")]
1331+
impl<T> MaybeSync for T {}
1332+
}
1333+
13121334
#[test]
13131335
fn test_default_limits() {
13141336
let limits = wgt::Limits::default();

wgpu-types/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5765,11 +5765,6 @@ impl std::ops::Deref for ExternalImageSource {
57655765
}
57665766
}
57675767

5768-
#[cfg(target_arch = "wasm32")]
5769-
unsafe impl Send for ExternalImageSource {}
5770-
#[cfg(target_arch = "wasm32")]
5771-
unsafe impl Sync for ExternalImageSource {}
5772-
57735768
/// Color spaces supported on the web.
57745769
///
57755770
/// Corresponds to [HTML Canvas `PredefinedColorSpace`](

0 commit comments

Comments
 (0)