Skip to content

Commit 88f18ed

Browse files
Don't implement Send or Sync on Wasm (#3691)
Co-authored-by: Connor Fitzgerald <[email protected]>
1 parent 7c34cc8 commit 88f18ed

File tree

22 files changed

+908
-138
lines changed

22 files changed

+908
-138
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ jobs:
138138
set -e
139139
140140
# build for WebGPU
141+
cargo clippy --target ${{ matrix.target }} --tests --features glsl,spirv,fragile-send-sync-non-atomic-wasm
141142
cargo clippy --target ${{ matrix.target }} --tests --features glsl,spirv
142143
cargo doc --target ${{ matrix.target }} --no-deps --features glsl,spirv
143144

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Bottom level categories:
6868
- Document feature requirements for `DEPTH32FLOAT_STENCIL8` by @ErichDonGubler in [#3734](https://github.com/gfx-rs/wgpu/pull/3734).
6969
- Flesh out docs. for `AdapterInfo::{device,vendor}` by @ErichDonGubler in [#3763](https://github.com/gfx-rs/wgpu/pull/3763).
7070
- Spell out which sizes are in bytes. By @jimblandy in [#3773](https://github.com/gfx-rs/wgpu/pull/3773).
71+
- On Web, types don't implement `Send` or `Sync` anymore. By @daxpedda in [#3691](https://github.com/gfx-rs/wgpu/pull/3691)
7172

7273
### Bug Fixes
7374

wgpu-core/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ serial-pass = ["serde", "wgt/serde", "arrayvec/serde"]
5151
id32 = []
5252
# Enable `ShaderModuleSource::Wgsl`
5353
wgsl = ["naga/wgsl-in"]
54+
# Implement `Send` and `Sync` on Wasm.
55+
fragile-send-sync-non-atomic-wasm = ["hal/fragile-send-sync-non-atomic-wasm", "wgt/fragile-send-sync-non-atomic-wasm"]
5456

5557
[dependencies]
5658
arrayvec = "0.7"

wgpu-core/src/command/bundle.rs

+14
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,21 @@ pub struct RenderBundle<A: HalApi> {
746746
pub(crate) life_guard: LifeGuard,
747747
}
748748

749+
#[cfg(any(
750+
not(target_arch = "wasm32"),
751+
all(
752+
feature = "fragile-send-sync-non-atomic-wasm",
753+
not(target_feature = "atomics")
754+
)
755+
))]
749756
unsafe impl<A: HalApi> Send for RenderBundle<A> {}
757+
#[cfg(any(
758+
not(target_arch = "wasm32"),
759+
all(
760+
feature = "fragile-send-sync-non-atomic-wasm",
761+
not(target_feature = "atomics")
762+
)
763+
))]
750764
unsafe impl<A: HalApi> Sync for RenderBundle<A> {}
751765

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

wgpu-core/src/device/queue.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ pub struct SubmittedWorkDoneClosureC {
3737
pub user_data: *mut u8,
3838
}
3939

40+
#[cfg(any(
41+
not(target_arch = "wasm32"),
42+
all(
43+
feature = "fragile-send-sync-non-atomic-wasm",
44+
not(target_feature = "atomics")
45+
)
46+
))]
4047
unsafe impl Send for SubmittedWorkDoneClosureC {}
4148

4249
pub struct SubmittedWorkDoneClosure {
@@ -45,17 +52,30 @@ pub struct SubmittedWorkDoneClosure {
4552
inner: SubmittedWorkDoneClosureInner,
4653
}
4754

55+
#[cfg(any(
56+
not(target_arch = "wasm32"),
57+
all(
58+
feature = "fragile-send-sync-non-atomic-wasm",
59+
not(target_feature = "atomics")
60+
)
61+
))]
62+
type SubmittedWorkDoneCallback = Box<dyn FnOnce() + Send + 'static>;
63+
#[cfg(not(any(
64+
not(target_arch = "wasm32"),
65+
all(
66+
feature = "fragile-send-sync-non-atomic-wasm",
67+
not(target_feature = "atomics")
68+
)
69+
)))]
70+
type SubmittedWorkDoneCallback = Box<dyn FnOnce() + 'static>;
71+
4872
enum SubmittedWorkDoneClosureInner {
49-
Rust {
50-
callback: Box<dyn FnOnce() + Send + 'static>,
51-
},
52-
C {
53-
inner: SubmittedWorkDoneClosureC,
54-
},
73+
Rust { callback: SubmittedWorkDoneCallback },
74+
C { inner: SubmittedWorkDoneClosureC },
5575
}
5676

5777
impl SubmittedWorkDoneClosure {
58-
pub fn from_rust(callback: Box<dyn FnOnce() + Send + 'static>) -> Self {
78+
pub fn from_rust(callback: SubmittedWorkDoneCallback) -> Self {
5979
Self {
6080
inner: SubmittedWorkDoneClosureInner::Rust { callback },
6181
}

wgpu-core/src/error.rs

+15
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,22 @@ pub fn format_pretty_any(
162162
#[derive(Debug)]
163163
pub struct ContextError {
164164
pub string: &'static str,
165+
#[cfg(any(
166+
not(target_arch = "wasm32"),
167+
all(
168+
feature = "fragile-send-sync-non-atomic-wasm",
169+
not(target_feature = "atomics")
170+
)
171+
))]
165172
pub cause: Box<dyn Error + Send + Sync + 'static>,
173+
#[cfg(not(any(
174+
not(target_arch = "wasm32"),
175+
all(
176+
feature = "fragile-send-sync-non-atomic-wasm",
177+
not(target_feature = "atomics")
178+
)
179+
)))]
180+
pub cause: Box<dyn Error + 'static>,
166181
pub label_key: &'static str,
167182
pub label: String,
168183
}

wgpu-core/src/global.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@ impl<G: GlobalIdentityHandlerFactory> Drop for Global<G> {
155155
}
156156
}
157157

158-
#[cfg(test)]
158+
#[cfg(all(
159+
test,
160+
any(
161+
not(target_arch = "wasm32"),
162+
all(
163+
feature = "fragile-send-sync-non-atomic-wasm",
164+
not(target_feature = "atomics")
165+
)
166+
)
167+
))]
159168
fn _test_send_sync(global: &Global<crate::identity::IdentityManagerFactory>) {
160169
fn test_internal<T: Send + Sync>(_: T) {}
161170
test_internal(global)

wgpu-core/src/resource.rs

+41-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,21 @@ pub(crate) enum BufferMapState<A: hal::Api> {
7979
Idle,
8080
}
8181

82+
#[cfg(any(
83+
not(target_arch = "wasm32"),
84+
all(
85+
feature = "fragile-send-sync-non-atomic-wasm",
86+
not(target_feature = "atomics")
87+
)
88+
))]
8289
unsafe impl<A: hal::Api> Send for BufferMapState<A> {}
90+
#[cfg(any(
91+
not(target_arch = "wasm32"),
92+
all(
93+
feature = "fragile-send-sync-non-atomic-wasm",
94+
not(target_feature = "atomics")
95+
)
96+
))]
8397
unsafe impl<A: hal::Api> Sync for BufferMapState<A> {}
8498

8599
#[repr(C)]
@@ -88,6 +102,13 @@ pub struct BufferMapCallbackC {
88102
pub user_data: *mut u8,
89103
}
90104

105+
#[cfg(any(
106+
not(target_arch = "wasm32"),
107+
all(
108+
feature = "fragile-send-sync-non-atomic-wasm",
109+
not(target_feature = "atomics")
110+
)
111+
))]
91112
unsafe impl Send for BufferMapCallbackC {}
92113

93114
pub struct BufferMapCallback {
@@ -96,17 +117,30 @@ pub struct BufferMapCallback {
96117
inner: Option<BufferMapCallbackInner>,
97118
}
98119

120+
#[cfg(any(
121+
not(target_arch = "wasm32"),
122+
all(
123+
feature = "fragile-send-sync-non-atomic-wasm",
124+
not(target_feature = "atomics")
125+
)
126+
))]
127+
type BufferMapCallbackCallback = Box<dyn FnOnce(BufferAccessResult) + Send + 'static>;
128+
#[cfg(not(any(
129+
not(target_arch = "wasm32"),
130+
all(
131+
feature = "fragile-send-sync-non-atomic-wasm",
132+
not(target_feature = "atomics")
133+
)
134+
)))]
135+
type BufferMapCallbackCallback = Box<dyn FnOnce(BufferAccessResult) + 'static>;
136+
99137
enum BufferMapCallbackInner {
100-
Rust {
101-
callback: Box<dyn FnOnce(BufferAccessResult) + Send + 'static>,
102-
},
103-
C {
104-
inner: BufferMapCallbackC,
105-
},
138+
Rust { callback: BufferMapCallbackCallback },
139+
C { inner: BufferMapCallbackC },
106140
}
107141

108142
impl BufferMapCallback {
109-
pub fn from_rust(callback: Box<dyn FnOnce(BufferAccessResult) + Send + 'static>) -> Self {
143+
pub fn from_rust(callback: BufferMapCallbackCallback) -> Self {
110144
Self {
111145
inner: Some(BufferMapCallbackInner::Rust { callback }),
112146
}

wgpu-hal/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dx12 = ["naga/hlsl-out", "d3d12", "bit-set", "range-alloc", "winapi/std", "winap
4343
windows_rs = ["gpu-allocator"]
4444
dxc_shader_compiler = ["hassle-rs"]
4545
renderdoc = ["libloading", "renderdoc-sys"]
46+
fragile-send-sync-non-atomic-wasm = ["wgt/fragile-send-sync-non-atomic-wasm"]
4647
link = ["metal/link"]
4748

4849
[[example]]

wgpu-hal/src/gles/adapter.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,17 @@ impl super::AdapterShared {
936936
}
937937
}
938938

939-
// SAFE: Wasm doesn't have threads
940-
#[cfg(target_arch = "wasm32")]
939+
#[cfg(all(
940+
target_arch = "wasm32",
941+
feature = "fragile-send-sync-non-atomic-wasm",
942+
not(target_feature = "atomics")
943+
))]
941944
unsafe impl Sync for super::Adapter {}
942-
#[cfg(target_arch = "wasm32")]
945+
#[cfg(all(
946+
target_arch = "wasm32",
947+
feature = "fragile-send-sync-non-atomic-wasm",
948+
not(target_feature = "atomics")
949+
))]
943950
unsafe impl Send for super::Adapter {}
944951

945952
#[cfg(test)]

wgpu-hal/src/gles/device.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,15 @@ impl crate::Device<super::Api> for super::Device {
13211321
}
13221322
}
13231323

1324-
// SAFE: Wasm doesn't have threads
1325-
#[cfg(target_arch = "wasm32")]
1324+
#[cfg(all(
1325+
target_arch = "wasm32",
1326+
feature = "fragile-send-sync-non-atomic-wasm",
1327+
not(target_feature = "atomics")
1328+
))]
13261329
unsafe impl Sync for super::Device {}
1327-
#[cfg(target_arch = "wasm32")]
1330+
#[cfg(all(
1331+
target_arch = "wasm32",
1332+
feature = "fragile-send-sync-non-atomic-wasm",
1333+
not(target_feature = "atomics")
1334+
))]
13281335
unsafe impl Send for super::Device {}

wgpu-hal/src/gles/mod.rs

+67-18
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,17 @@ 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")]
249+
#[cfg(all(
250+
target_arch = "wasm32",
251+
feature = "fragile-send-sync-non-atomic-wasm",
252+
not(target_feature = "atomics")
253+
))]
251254
unsafe impl Sync for Buffer {}
252-
#[cfg(target_arch = "wasm32")]
255+
#[cfg(all(
256+
target_arch = "wasm32",
257+
feature = "fragile-send-sync-non-atomic-wasm",
258+
not(target_feature = "atomics")
259+
))]
253260
unsafe impl Send for Buffer {}
254261

255262
#[derive(Clone, Debug)]
@@ -268,11 +275,18 @@ pub enum TextureInner {
268275
},
269276
}
270277

271-
// SAFE: Wasm doesn't have threads
272-
#[cfg(target_arch = "wasm32")]
273-
unsafe impl Send for TextureInner {}
274-
#[cfg(target_arch = "wasm32")]
278+
#[cfg(all(
279+
target_arch = "wasm32",
280+
feature = "fragile-send-sync-non-atomic-wasm",
281+
not(target_feature = "atomics")
282+
))]
275283
unsafe impl Sync for TextureInner {}
284+
#[cfg(all(
285+
target_arch = "wasm32",
286+
feature = "fragile-send-sync-non-atomic-wasm",
287+
not(target_feature = "atomics")
288+
))]
289+
unsafe impl Send for TextureInner {}
276290

277291
impl TextureInner {
278292
fn as_native(&self) -> (glow::Texture, BindTarget) {
@@ -462,10 +476,17 @@ struct UniformDesc {
462476
utype: u32,
463477
}
464478

465-
// Safe: Wasm doesn't have threads
466-
#[cfg(target_arch = "wasm32")]
479+
#[cfg(all(
480+
target_arch = "wasm32",
481+
feature = "fragile-send-sync-non-atomic-wasm",
482+
not(target_feature = "atomics")
483+
))]
467484
unsafe impl Sync for UniformDesc {}
468-
#[cfg(target_arch = "wasm32")]
485+
#[cfg(all(
486+
target_arch = "wasm32",
487+
feature = "fragile-send-sync-non-atomic-wasm",
488+
not(target_feature = "atomics")
489+
))]
469490
unsafe impl Send for UniformDesc {}
470491

471492
/// For each texture in the pipeline layout, store the index of the only
@@ -530,21 +551,35 @@ pub struct RenderPipeline {
530551
alpha_to_coverage_enabled: bool,
531552
}
532553

533-
// SAFE: Wasm doesn't have threads
534-
#[cfg(target_arch = "wasm32")]
535-
unsafe impl Send for RenderPipeline {}
536-
#[cfg(target_arch = "wasm32")]
554+
#[cfg(all(
555+
target_arch = "wasm32",
556+
feature = "fragile-send-sync-non-atomic-wasm",
557+
not(target_feature = "atomics")
558+
))]
537559
unsafe impl Sync for RenderPipeline {}
560+
#[cfg(all(
561+
target_arch = "wasm32",
562+
feature = "fragile-send-sync-non-atomic-wasm",
563+
not(target_feature = "atomics")
564+
))]
565+
unsafe impl Send for RenderPipeline {}
538566

539567
pub struct ComputePipeline {
540568
inner: Arc<PipelineInner>,
541569
}
542570

543-
// SAFE: Wasm doesn't have threads
544-
#[cfg(target_arch = "wasm32")]
545-
unsafe impl Send for ComputePipeline {}
546-
#[cfg(target_arch = "wasm32")]
571+
#[cfg(all(
572+
target_arch = "wasm32",
573+
feature = "fragile-send-sync-non-atomic-wasm",
574+
not(target_feature = "atomics")
575+
))]
547576
unsafe impl Sync for ComputePipeline {}
577+
#[cfg(all(
578+
target_arch = "wasm32",
579+
feature = "fragile-send-sync-non-atomic-wasm",
580+
not(target_feature = "atomics")
581+
))]
582+
unsafe impl Send for ComputePipeline {}
548583

549584
#[derive(Debug)]
550585
pub struct QuerySet {
@@ -558,7 +593,21 @@ pub struct Fence {
558593
pending: Vec<(crate::FenceValue, glow::Fence)>,
559594
}
560595

596+
#[cfg(any(
597+
not(target_arch = "wasm32"),
598+
all(
599+
feature = "fragile-send-sync-non-atomic-wasm",
600+
not(target_feature = "atomics")
601+
)
602+
))]
561603
unsafe impl Send for Fence {}
604+
#[cfg(any(
605+
not(target_arch = "wasm32"),
606+
all(
607+
feature = "fragile-send-sync-non-atomic-wasm",
608+
not(target_feature = "atomics")
609+
)
610+
))]
562611
unsafe impl Sync for Fence {}
563612

564613
impl Fence {

0 commit comments

Comments
 (0)