Skip to content

Remove parking_lot from bevy_asset and bevy_macro_utils #18996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ either = "1.13"
futures-io = "0.3"
futures-lite = "2.0.1"
blake3 = "1.5"
parking_lot = { version = "0.12", features = ["arc_lock", "send_guard"] }
ron = "0.8"
serde = { version = "1", features = ["derive"] }
thiserror = { version = "2", default-features = false }
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_ecs::{
resource::Resource,
system::{Res, ResMut, SystemChangeTick},
};
use bevy_platform::collections::HashMap;
use bevy_platform::{collections::HashMap, sync::PoisonError};
use bevy_reflect::{Reflect, TypePath};
use core::{any::TypeId, iter::Enumerate, marker::PhantomData, sync::atomic::AtomicU32};
use crossbeam_channel::{Receiver, Sender};
Expand Down Expand Up @@ -545,7 +545,11 @@ impl<A: Asset> Assets<A> {
// that `asset_server.load` calls that occur during it block, which ensures that
// re-loads are kicked off appropriately. This function must be "transactional" relative
// to other asset info operations
let mut infos = asset_server.data.infos.write();
let mut infos = asset_server
.data
.infos
.write()
.unwrap_or_else(PoisonError::into_inner);
while let Ok(drop_event) = assets.handle_provider.drop_receiver.try_recv() {
let id = drop_event.id.typed();

Expand Down
13 changes: 10 additions & 3 deletions crates/bevy_asset/src/io/embedded/embedded_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::io::{
AssetSourceEvent, AssetWatcher,
};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use bevy_platform::collections::HashMap;
use bevy_platform::{
collections::HashMap,
sync::{PoisonError, RwLock},
};
use core::time::Duration;
use notify_debouncer_full::{notify::RecommendedWatcher, Debouncer, RecommendedCache};
use parking_lot::RwLock;
use std::{
fs::File,
io::{BufReader, Read},
Expand Down Expand Up @@ -63,7 +65,12 @@ impl FilesystemEventHandler for EmbeddedEventHandler {

fn get_path(&self, absolute_path: &Path) -> Option<(PathBuf, bool)> {
let (local_path, is_meta) = get_asset_path(&self.root, absolute_path);
let final_path = self.root_paths.read().get(local_path.as_path())?.clone();
let final_path = self
.root_paths
.read()
.unwrap_or_else(PoisonError::into_inner)
.get(local_path.as_path())?
.clone();
if is_meta {
warn!("Meta file asset hot-reloading is not supported yet: {final_path:?}");
}
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_asset/src/io/embedded/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy_ecs::resource::Resource;
use std::path::{Path, PathBuf};

#[cfg(feature = "embedded_watcher")]
use alloc::borrow::ToOwned;
use {alloc::borrow::ToOwned, bevy_platform::sync::PoisonError};

/// The name of the `embedded` [`AssetSource`],
/// as stored in the [`AssetSourceBuilders`] resource.
Expand All @@ -29,7 +29,7 @@ pub struct EmbeddedAssetRegistry {
dir: Dir,
#[cfg(feature = "embedded_watcher")]
root_paths: alloc::sync::Arc<
parking_lot::RwLock<bevy_platform::collections::HashMap<Box<Path>, PathBuf>>,
bevy_platform::sync::RwLock<bevy_platform::collections::HashMap<Box<Path>, PathBuf>>,
>,
}

Expand All @@ -49,6 +49,7 @@ impl EmbeddedAssetRegistry {
#[cfg(feature = "embedded_watcher")]
self.root_paths
.write()
.unwrap_or_else(PoisonError::into_inner)
.insert(full_path.into(), asset_path.to_owned());
self.dir.insert_asset(asset_path, value);
}
Expand All @@ -68,6 +69,7 @@ impl EmbeddedAssetRegistry {
#[cfg(feature = "embedded_watcher")]
self.root_paths
.write()
.unwrap_or_else(PoisonError::into_inner)
.insert(full_path.into(), asset_path.to_owned());
self.dir.insert_meta(asset_path, value);
}
Expand Down
10 changes: 6 additions & 4 deletions crates/bevy_asset/src/io/gated.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
use alloc::{boxed::Box, sync::Arc};
use bevy_platform::collections::HashMap;
use bevy_platform::{
collections::HashMap,
sync::{PoisonError, RwLock},
};
use crossbeam_channel::{Receiver, Sender};
use parking_lot::RwLock;
use std::path::Path;

/// A "gated" reader that will prevent asset reads from returning until
Expand Down Expand Up @@ -32,7 +34,7 @@ impl GateOpener {
/// Opens the `path` "gate", allowing a _single_ [`AssetReader`] operation to return for that path.
/// If multiple operations are expected, call `open` the expected number of calls.
pub fn open<P: AsRef<Path>>(&self, path: P) {
let mut gates = self.gates.write();
let mut gates = self.gates.write().unwrap_or_else(PoisonError::into_inner);
let gates = gates
.entry_ref(path.as_ref())
.or_insert_with(crossbeam_channel::unbounded);
Expand All @@ -58,7 +60,7 @@ impl<R: AssetReader> GatedReader<R> {
impl<R: AssetReader> AssetReader for GatedReader<R> {
async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
let receiver = {
let mut gates = self.gates.write();
let mut gates = self.gates.write().unwrap_or_else(PoisonError::into_inner);
let gates = gates
.entry_ref(path.as_ref())
.or_insert_with(crossbeam_channel::unbounded);
Expand Down
86 changes: 61 additions & 25 deletions crates/bevy_asset/src/io/memory.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
use alloc::{borrow::ToOwned, boxed::Box, sync::Arc, vec::Vec};
use bevy_platform::collections::HashMap;
use bevy_platform::{
collections::HashMap,
sync::{PoisonError, RwLock},
};
use core::{pin::Pin, task::Poll};
use futures_io::AsyncRead;
use futures_lite::{ready, Stream};
use parking_lot::RwLock;
use std::path::{Path, PathBuf};

use super::AsyncSeekForward;
Expand Down Expand Up @@ -44,13 +46,17 @@ impl Dir {
if let Some(parent) = path.parent() {
dir = self.get_or_insert_dir(parent);
}
dir.0.write().assets.insert(
path.file_name().unwrap().to_string_lossy().into(),
Data {
value: value.into(),
path: path.to_owned(),
},
);
dir.0
.write()
.unwrap_or_else(PoisonError::into_inner)
.assets
.insert(
path.file_name().unwrap().to_string_lossy().into(),
Data {
value: value.into(),
path: path.to_owned(),
},
);
}

/// Removes the stored asset at `path` and returns the `Data` stored if found and otherwise `None`.
Expand All @@ -60,21 +66,29 @@ impl Dir {
dir = self.get_or_insert_dir(parent);
}
let key: Box<str> = path.file_name().unwrap().to_string_lossy().into();
dir.0.write().assets.remove(&key)
dir.0
.write()
.unwrap_or_else(PoisonError::into_inner)
.assets
.remove(&key)
}

pub fn insert_meta(&self, path: &Path, value: impl Into<Value>) {
let mut dir = self.clone();
if let Some(parent) = path.parent() {
dir = self.get_or_insert_dir(parent);
}
dir.0.write().metadata.insert(
path.file_name().unwrap().to_string_lossy().into(),
Data {
value: value.into(),
path: path.to_owned(),
},
);
dir.0
.write()
.unwrap_or_else(PoisonError::into_inner)
.metadata
.insert(
path.file_name().unwrap().to_string_lossy().into(),
Data {
value: value.into(),
path: path.to_owned(),
},
);
}

pub fn get_or_insert_dir(&self, path: &Path) -> Dir {
Expand All @@ -84,7 +98,7 @@ impl Dir {
full_path.push(c);
let name = c.as_os_str().to_string_lossy().into();
dir = {
let dirs = &mut dir.0.write().dirs;
let dirs = &mut dir.0.write().unwrap_or_else(PoisonError::into_inner).dirs;
dirs.entry(name)
.or_insert_with(|| Dir::new(full_path.clone()))
.clone()
Expand All @@ -98,7 +112,13 @@ impl Dir {
let mut dir = self.clone();
for p in path.components() {
let component = p.as_os_str().to_str().unwrap();
let next_dir = dir.0.read().dirs.get(component)?.clone();
let next_dir = dir
.0
.read()
.unwrap_or_else(PoisonError::into_inner)
.dirs
.get(component)?
.clone();
dir = next_dir;
}
Some(dir)
Expand All @@ -110,8 +130,14 @@ impl Dir {
dir = dir.get_dir(parent)?;
}

path.file_name()
.and_then(|f| dir.0.read().assets.get(f.to_str().unwrap()).cloned())
path.file_name().and_then(|f| {
dir.0
.read()
.unwrap_or_else(PoisonError::into_inner)
.assets
.get(f.to_str().unwrap())
.cloned()
})
}

pub fn get_metadata(&self, path: &Path) -> Option<Data> {
Expand All @@ -120,12 +146,22 @@ impl Dir {
dir = dir.get_dir(parent)?;
}

path.file_name()
.and_then(|f| dir.0.read().metadata.get(f.to_str().unwrap()).cloned())
path.file_name().and_then(|f| {
dir.0
.read()
.unwrap_or_else(PoisonError::into_inner)
.metadata
.get(f.to_str().unwrap())
.cloned()
})
}

pub fn path(&self) -> PathBuf {
self.0.read().path.to_owned()
self.0
.read()
.unwrap_or_else(PoisonError::into_inner)
.path
.to_owned()
}
}

Expand Down Expand Up @@ -153,7 +189,7 @@ impl Stream for DirStream {
_cx: &mut core::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
let dir = this.dir.0.read();
let dir = this.dir.0.read().unwrap_or_else(PoisonError::into_inner);

let dir_index = this.dir_index;
if let Some(dir_path) = dir
Expand Down
44 changes: 36 additions & 8 deletions crates/bevy_asset/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ use crate::{
};
use alloc::{borrow::ToOwned, boxed::Box, collections::VecDeque, sync::Arc, vec, vec::Vec};
use bevy_ecs::prelude::*;
use bevy_platform::collections::{HashMap, HashSet};
use bevy_platform::{
collections::{HashMap, HashSet},
sync::{PoisonError, RwLock},
};
use bevy_tasks::IoTaskPool;
use futures_io::ErrorKind;
use futures_lite::{AsyncReadExt, AsyncWriteExt, StreamExt};
use parking_lot::RwLock;
use std::path::{Path, PathBuf};
use thiserror::Error;
use tracing::{debug, error, trace, warn};
Expand Down Expand Up @@ -533,7 +535,12 @@ impl AssetProcessor {
async fn finish_processing_assets(&self) {
self.try_reprocessing_queued().await;
// clean up metadata in asset server
self.server.data.infos.write().consume_handle_drop_events();
self.server
.data
.infos
.write()
.unwrap_or_else(PoisonError::into_inner)
.consume_handle_drop_events();
self.set_state(ProcessorState::Finished).await;
}

Expand Down Expand Up @@ -581,28 +588,49 @@ impl AssetProcessor {

/// Register a new asset processor.
pub fn register_processor<P: Process>(&self, processor: P) {
let mut process_plans = self.data.processors.write();
let mut process_plans = self
.data
.processors
.write()
.unwrap_or_else(PoisonError::into_inner);
#[cfg(feature = "trace")]
let processor = InstrumentedAssetProcessor(processor);
process_plans.insert(core::any::type_name::<P>(), Arc::new(processor));
}

/// Set the default processor for the given `extension`. Make sure `P` is registered with [`AssetProcessor::register_processor`].
pub fn set_default_processor<P: Process>(&self, extension: &str) {
let mut default_processors = self.data.default_processors.write();
let mut default_processors = self
.data
.default_processors
.write()
.unwrap_or_else(PoisonError::into_inner);
default_processors.insert(extension.into(), core::any::type_name::<P>());
}

/// Returns the default processor for the given `extension`, if it exists.
pub fn get_default_processor(&self, extension: &str) -> Option<Arc<dyn ErasedProcessor>> {
let default_processors = self.data.default_processors.read();
let default_processors = self
.data
.default_processors
.read()
.unwrap_or_else(PoisonError::into_inner);
let key = default_processors.get(extension)?;
self.data.processors.read().get(key).cloned()
self.data
.processors
.read()
.unwrap_or_else(PoisonError::into_inner)
.get(key)
.cloned()
}

/// Returns the processor with the given `processor_type_name`, if it exists.
pub fn get_processor(&self, processor_type_name: &str) -> Option<Arc<dyn ErasedProcessor>> {
let processors = self.data.processors.read();
let processors = self
.data
.processors
.read()
.unwrap_or_else(PoisonError::into_inner);
processors.get(processor_type_name).cloned()
}

Expand Down
Loading
Loading