Skip to content

Commit

Permalink
Replaced parking_lot with std::sync (bevyengine#9545)
Browse files Browse the repository at this point in the history
# Objective

- Fixes bevyengine#4610 

## Solution

- Replaced all instances of `parking_lot` locks with equivalents from
`std::sync`. Acquiring locks within `std::sync` can fail, so
`.expect("Lock Poisoned")` statements were added where required.

## Comments

In [this
comment](bevyengine#4610 (comment)),
the lack of deadlock detection was mentioned as a potential reason to
not make this change. From what I can gather, Bevy doesn't appear to be
using this functionality within the engine. Unless it was expected that
a Bevy consumer was expected to enable and use this functionality, it
appears to be a feature lost without consequence.

Unfortunately, `cpal` and `wgpu` both still rely on `parking_lot`,
leaving it in the dependency graph even after this change.

From my basic experimentation, this change doesn't appear to have any
performance impacts, positive or negative. I tested this using
`bevymark` with 50,000 entities and observed 20ms of frame-time before
and after the change. More extensive testing with larger/real projects
should probably be done.
  • Loading branch information
bushrat011899 authored and ameknite committed Oct 3, 2023
1 parent 2b8e540 commit 33e4169
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 24 deletions.
1 change: 0 additions & 1 deletion crates/bevy_audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ bevy_utils = { path = "../bevy_utils", version = "0.12.0-dev" }

# other
rodio = { version = "0.17", default-features = false }
parking_lot = "0.12.1"

[target.'cfg(target_os = "android")'.dependencies]
oboe = { version = "0.5", optional = true }
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ bevy_ptr = { path = "../bevy_ptr", version = "0.12.0-dev" }
# other
erased-serde = "0.3"
downcast-rs = "1.2"
parking_lot = "0.12.1"
thiserror = "1.0"
once_cell = "1.11"
serde = "1"
Expand Down
20 changes: 15 additions & 5 deletions crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use crate::{serde::Serializable, Reflect, TypeInfo, Typed};
use bevy_ptr::{Ptr, PtrMut};
use bevy_utils::{HashMap, HashSet};
use downcast_rs::{impl_downcast, Downcast};
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use serde::Deserialize;
use std::{any::TypeId, fmt::Debug, sync::Arc};
use std::{
any::TypeId,
fmt::Debug,
sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

/// A registry of [reflected] types.
///
Expand Down Expand Up @@ -35,7 +38,12 @@ pub struct TypeRegistryArc {

impl Debug for TypeRegistryArc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.internal.read().full_name_to_id.keys().fmt(f)
self.internal
.read()
.unwrap_or_else(PoisonError::into_inner)
.full_name_to_id
.keys()
.fmt(f)
}
}

Expand Down Expand Up @@ -267,12 +275,14 @@ impl TypeRegistry {
impl TypeRegistryArc {
/// Takes a read lock on the underlying [`TypeRegistry`].
pub fn read(&self) -> RwLockReadGuard<'_, TypeRegistry> {
self.internal.read()
self.internal.read().unwrap_or_else(PoisonError::into_inner)
}

/// Takes a write lock on the underlying [`TypeRegistry`].
pub fn write(&self) -> RwLockWriteGuard<'_, TypeRegistry> {
self.internal.write()
self.internal
.write()
.unwrap_or_else(PoisonError::into_inner)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use crate::TypeInfo;
use bevy_utils::{FixedState, StableHashMap};
use once_cell::race::OnceBox;
use parking_lot::RwLock;
use std::{
any::{Any, TypeId},
hash::BuildHasher,
sync::{PoisonError, RwLock},
};

/// A type that can be stored in a ([`Non`])[`GenericTypeCell`].
Expand Down Expand Up @@ -236,15 +236,15 @@ impl<T: TypedProperty> GenericTypeCell<T> {
// since `f` might want to call `get_or_insert` recursively
// and we don't want a deadlock!
{
let mapping = self.0.read();
let mapping = self.0.read().unwrap_or_else(PoisonError::into_inner);
if let Some(info) = mapping.get(&type_id) {
return info;
}
}

let value = f();

let mut mapping = self.0.write();
let mut mapping = self.0.write().unwrap_or_else(PoisonError::into_inner);
mapping
.entry(type_id)
.insert({
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ thread_local = "1.1"
thiserror = "1.0"
futures-lite = "1.4.0"
hexasphere = "9.0"
parking_lot = "0.12.1"
ddsfile = { version = "0.5.0", optional = true }
ktx2 = { version = "0.3.0", optional = true }
naga_oil = "0.8"
Expand Down
14 changes: 10 additions & 4 deletions crates/bevy_render/src/render_phase/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ use bevy_ecs::{
world::World,
};
use bevy_utils::{all_tuples, HashMap};
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{any::TypeId, fmt::Debug, hash::Hash};
use std::{
any::TypeId,
fmt::Debug,
hash::Hash,
sync::{PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

/// A draw function used to draw [`PhaseItem`]s.
///
Expand Down Expand Up @@ -116,12 +120,14 @@ impl<P: PhaseItem> Default for DrawFunctions<P> {
impl<P: PhaseItem> DrawFunctions<P> {
/// Accesses the draw functions in read mode.
pub fn read(&self) -> RwLockReadGuard<'_, DrawFunctionsInternal<P>> {
self.internal.read()
self.internal.read().unwrap_or_else(PoisonError::into_inner)
}

/// Accesses the draw functions in write mode.
pub fn write(&self) -> RwLockWriteGuard<'_, DrawFunctionsInternal<P>> {
self.internal.write()
self.internal
.write()
.unwrap_or_else(PoisonError::into_inner)
}
}

Expand Down
24 changes: 19 additions & 5 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ use bevy_utils::{
Entry, HashMap, HashSet,
};
use naga::valid::Capabilities;
use parking_lot::Mutex;
use std::{borrow::Cow, hash::Hash, mem, ops::Deref};
use std::{
borrow::Cow,
hash::Hash,
mem,
ops::Deref,
sync::{Mutex, PoisonError},
};
use thiserror::Error;
#[cfg(feature = "shader_format_spirv")]
use wgpu::util::make_spirv;
Expand Down Expand Up @@ -587,7 +592,10 @@ impl PipelineCache {
&self,
descriptor: RenderPipelineDescriptor,
) -> CachedRenderPipelineId {
let mut new_pipelines = self.new_pipelines.lock();
let mut new_pipelines = self
.new_pipelines
.lock()
.unwrap_or_else(PoisonError::into_inner);
let id = CachedRenderPipelineId(self.pipelines.len() + new_pipelines.len());
new_pipelines.push(CachedPipeline {
descriptor: PipelineDescriptor::RenderPipelineDescriptor(Box::new(descriptor)),
Expand All @@ -613,7 +621,10 @@ impl PipelineCache {
&self,
descriptor: ComputePipelineDescriptor,
) -> CachedComputePipelineId {
let mut new_pipelines = self.new_pipelines.lock();
let mut new_pipelines = self
.new_pipelines
.lock()
.unwrap_or_else(PoisonError::into_inner);
let id = CachedComputePipelineId(self.pipelines.len() + new_pipelines.len());
new_pipelines.push(CachedPipeline {
descriptor: PipelineDescriptor::ComputePipelineDescriptor(Box::new(descriptor)),
Expand Down Expand Up @@ -773,7 +784,10 @@ impl PipelineCache {
let mut pipelines = mem::take(&mut self.pipelines);

{
let mut new_pipelines = self.new_pipelines.lock();
let mut new_pipelines = self
.new_pipelines
.lock()
.unwrap_or_else(PoisonError::into_inner);
for new_pipeline in new_pipelines.drain(..) {
let id = pipelines.len();
pipelines.push(new_pipeline);
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_render/src/view/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use bevy_utils::{default, tracing::debug, HashMap, HashSet};
use bevy_window::{
CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosed,
};
use std::ops::{Deref, DerefMut};
use std::{
ops::{Deref, DerefMut},
sync::PoisonError,
};
use wgpu::{BufferUsages, TextureFormat, TextureUsages, TextureViewDescriptor};

pub mod screenshot;
Expand Down Expand Up @@ -168,7 +171,12 @@ fn extract_windows(
// Even if a user had multiple copies of this system, since the system has a mutable resource access the two systems would never run
// at the same time
// TODO: since this is guaranteed, should the lock be replaced with an UnsafeCell to remove the overhead, or is it minor enough to be ignored?
for (window, screenshot_func) in screenshot_manager.callbacks.lock().drain() {
for (window, screenshot_func) in screenshot_manager
.callbacks
.lock()
.unwrap_or_else(PoisonError::into_inner)
.drain()
{
if let Some(window) = extracted_windows.get_mut(&window) {
window.screenshot_func = Some(screenshot_func);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_render/src/view/window/screenshot.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{borrow::Cow, path::Path};
use std::{borrow::Cow, path::Path, sync::PoisonError};

use bevy_app::Plugin;
use bevy_asset::{load_internal_asset, Handle};
use bevy_ecs::prelude::*;
use bevy_log::{error, info, info_span};
use bevy_tasks::AsyncComputeTaskPool;
use bevy_utils::HashMap;
use parking_lot::Mutex;
use std::sync::Mutex;
use thiserror::Error;
use wgpu::{
CommandEncoder, Extent3d, ImageDataLayout, TextureFormat, COPY_BYTES_PER_ROW_ALIGNMENT,
Expand Down Expand Up @@ -50,6 +50,7 @@ impl ScreenshotManager {
) -> Result<(), ScreenshotAlreadyRequestedError> {
self.callbacks
.get_mut()
.unwrap_or_else(PoisonError::into_inner)
.try_insert(window, Box::new(callback))
.map(|_| ())
.map_err(|_| ScreenshotAlreadyRequestedError)
Expand Down

0 comments on commit 33e4169

Please sign in to comment.