Can the fluid world protected by a arc mutex be shared between threads?
- Dominant language
- Rust
- Stars
- 693
- Forks
- 40
- PR merge metrics
- No merged PRs in 30d
Description
I'm testing nphysics, salva and legion (entity component system) together. I need all the physics data to be a legion resource (and that it means that it will be shared between threads). The problem is with the LiquidWorld that is non Send, so I can not included inside the physics bundle resource. If I wrap everything with a Arc> like the following code:
```
pub struct PhysicsResourceInner {
pub mechanical_world: DefaultMechanicalWorld,
pub geometrical_world: DefaultGeometricalWorld,
pub bodies: DefaultBodySet,
pub colliders: DefaultColliderSet,
pub joint_constraints: DefaultJointConstraintSet,
pub force_generators: DefaultForceGeneratorSet,
// Fluid
pub fluid_world: LiquidWorld::,
pub fluid_couplings: ColliderCouplingSet::,
}
#[derive(Clone)]
pub struct PhysicsResource {
pub inner: Arc>,
}
```
Then I can mark it as Send using an "unsafe" impl (like the above). Is this really safe or there is some hidden problems that will arise?
`unsafe impl Send for PhysicsResource {}`
EDIT: Not even the unsafe impl makes it compile. The important part of the error messages goes like:
```
error[E0277]: `(dyn salva2d::solver::pressure::pressure_solver::PressureSolver + 'static)` cannot be sent between threads safely
--> src/app/mod.rs:106:73
|
106 | if let Some(physics_resource) = &mut self.resources.get_mut::() {
| ^^^^^^^ `(dyn salva2d::solver::pressure::pressure_solver::PressureSolver + 'static)` cannot be sent between threads safely
|
= help: the trait `std::marker::Send` is not implemented for `(dyn salva2d::solver::pressure::pressure_solver::PressureSolver + 'static)`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<(dyn salva2d::solver::pressure::pressure_solver::PressureSolver + 'static)>`
= note: required because it appears within the type `std::boxed::Box<(dyn salva2d::solver::pressure::pressure_solver::PressureSolver + 'static)>`
= note: required because it appears within the type `salva2d::liquid_world::LiquidWorld`
= note: required because it appears within the type `resources::physics::PhysicsResourceInner`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::Arc>`
= note: required because it appears within the type `resources::physics::PhysicsResource`
= note: required because of the requirements on the impl of `legion_systems::resource::Resource` for `resources::physics::PhysicsResource`
```
Edit 2: If I change on the salva source "liquid_world.rs" (the two lines above), I can compile (without the unsafe impl). But I'm not sure if it ok or I'm missing something. Above the changes I made:
On the LiquidWorld struct definition add + Send to the solve:
`solver: Box + Send>,`
And also add + Send to the new function:
`solver: impl PressureSolver + 'static + Send,`
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.