Skip to content

Commit

Permalink
Switch to parking_lot's Mutexes (#1951)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 authored Aug 11, 2022
1 parent 0096c75 commit 38ae563
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 59 deletions.
19 changes: 10 additions & 9 deletions vulkano/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub use crate::{
};
use ash::vk::Handle;
use once_cell::sync::OnceCell;
use parking_lot::{Mutex, MutexGuard};
use smallvec::SmallVec;
use std::{
cell::RefCell,
Expand All @@ -129,7 +130,7 @@ use std::{
mem::MaybeUninit,
ops::Deref,
ptr,
sync::{Arc, Mutex, MutexGuard},
sync::Arc,
};

pub(crate) mod extensions;
Expand Down Expand Up @@ -687,13 +688,13 @@ impl Drop for Device {
let fns = self.fns();

unsafe {
for &raw_fence in self.fence_pool.lock().unwrap().iter() {
for &raw_fence in self.fence_pool.lock().iter() {
(fns.v1_0.destroy_fence)(self.handle, raw_fence, ptr::null());
}
for &raw_sem in self.semaphore_pool.lock().unwrap().iter() {
for &raw_sem in self.semaphore_pool.lock().iter() {
(fns.v1_0.destroy_semaphore)(self.handle, raw_sem, ptr::null());
}
for &raw_event in self.event_pool.lock().unwrap().iter() {
for &raw_event in self.event_pool.lock().iter() {
(fns.v1_0.destroy_event)(self.handle, raw_event, ptr::null());
}
(fns.v1_0.destroy_device)(self.handle, ptr::null());
Expand Down Expand Up @@ -1010,7 +1011,7 @@ impl Queue {
pub fn wait(&self) -> Result<(), OomError> {
unsafe {
let fns = self.device.fns();
let handle = self.handle.lock().unwrap();
let handle = self.handle.lock();
check_errors((fns.v1_0.queue_wait_idle)(*handle))?;
Ok(())
}
Expand Down Expand Up @@ -1042,7 +1043,7 @@ impl Queue {

unsafe {
let fns = self.device.instance().fns();
let handle = self.handle.lock().unwrap();
let handle = self.handle.lock();
(fns.ext_debug_utils.queue_begin_debug_utils_label_ext)(*handle, &label_info);
}

Expand Down Expand Up @@ -1083,7 +1084,7 @@ impl Queue {

{
let fns = self.device.instance().fns();
let handle = self.handle.lock().unwrap();
let handle = self.handle.lock();
(fns.ext_debug_utils.queue_end_debug_utils_label_ext)(*handle);
}

Expand Down Expand Up @@ -1135,7 +1136,7 @@ impl Queue {

unsafe {
let fns = self.device.instance().fns();
let handle = self.handle.lock().unwrap();
let handle = self.handle.lock();
(fns.ext_debug_utils.queue_insert_debug_utils_label_ext)(*handle, &label_info);
}

Expand Down Expand Up @@ -1167,7 +1168,7 @@ unsafe impl SynchronizedVulkanObject for Queue {

#[inline]
fn internal_object_guard(&self) -> MutexGuard<Self::Object> {
self.handle.lock().unwrap()
self.handle.lock()
}
}

Expand Down
7 changes: 2 additions & 5 deletions vulkano/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@
pub use ash::vk::Handle;
pub use half;
pub use library::VulkanLibrary;
use std::{
error, fmt,
ops::Deref,
sync::{Arc, MutexGuard},
};
use parking_lot::MutexGuard;
use std::{error, fmt, ops::Deref, sync::Arc};
pub use version::Version;

#[macro_use]
Expand Down
19 changes: 8 additions & 11 deletions vulkano/src/memory/device_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
device::{physical::MemoryType, Device, DeviceOwned},
DeviceSize, Error, OomError, Version, VulkanObject,
};
use parking_lot::Mutex;
use std::{
error,
ffi::c_void,
Expand All @@ -22,7 +23,7 @@ use std::{
mem::MaybeUninit,
ops::{BitOr, Range},
ptr, slice,
sync::{Arc, Mutex},
sync::Arc,
};

/// Represents memory that has been allocated from the device.
Expand Down Expand Up @@ -370,7 +371,7 @@ impl DeviceMemory {
allocate_info = allocate_info.push_next(info);
}

let mut allocation_count = device.allocation_count().lock().expect("Poisoned mutex");
let mut allocation_count = device.allocation_count().lock();

// VUID-vkAllocateMemory-maxMemoryAllocationCount-04101
// This is technically validation, but it must be atomic with the `allocate_memory` call.
Expand Down Expand Up @@ -478,11 +479,7 @@ impl Drop for DeviceMemory {
unsafe {
let fns = self.device.fns();
(fns.v1_0.free_memory)(self.device.internal_object(), self.handle, ptr::null());
let mut allocation_count = self
.device
.allocation_count()
.lock()
.expect("Poisoned mutex");
let mut allocation_count = self.device.allocation_count().lock();
*allocation_count -= 1;
}
}
Expand Down Expand Up @@ -1573,7 +1570,7 @@ mod tests {
fn allocation_count() {
let (device, _) = gfx_dev_and_queue!();
let memory_type = device.physical_device().memory_types().next().unwrap();
assert_eq!(*device.allocation_count().lock().unwrap(), 0);
assert_eq!(*device.allocation_count().lock(), 0);
let mem1 = DeviceMemory::allocate(
device.clone(),
MemoryAllocateInfo {
Expand All @@ -1583,7 +1580,7 @@ mod tests {
},
)
.unwrap();
assert_eq!(*device.allocation_count().lock().unwrap(), 1);
assert_eq!(*device.allocation_count().lock(), 1);
{
let mem2 = DeviceMemory::allocate(
device.clone(),
Expand All @@ -1594,8 +1591,8 @@ mod tests {
},
)
.unwrap();
assert_eq!(*device.allocation_count().lock().unwrap(), 2);
assert_eq!(*device.allocation_count().lock(), 2);
}
assert_eq!(*device.allocation_count().lock().unwrap(), 1);
assert_eq!(*device.allocation_count().lock(), 1);
}
}
11 changes: 4 additions & 7 deletions vulkano/src/memory/pool/host_visible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ use crate::{
},
DeviceSize,
};
use std::{
cmp,
ops::Range,
sync::{Arc, Mutex},
};
use parking_lot::Mutex;
use std::{cmp, ops::Range, sync::Arc};

/// Memory pool that operates on a given memory type.
#[derive(Debug)]
Expand Down Expand Up @@ -82,7 +79,7 @@ impl StandardHostVisibleMemoryTypePool {
}

// Find a location.
let mut occupied = me.occupied.lock().unwrap();
let mut occupied = me.occupied.lock();

// Try finding an entry in already-allocated chunks.
for &mut (ref dev_mem, ref mut entries) in occupied.iter_mut() {
Expand Down Expand Up @@ -183,7 +180,7 @@ impl StandardHostVisibleMemoryTypePoolAlloc {

impl Drop for StandardHostVisibleMemoryTypePoolAlloc {
fn drop(&mut self) {
let mut occupied = self.pool.occupied.lock().unwrap();
let mut occupied = self.pool.occupied.lock();

let entries = occupied
.iter_mut()
Expand Down
11 changes: 4 additions & 7 deletions vulkano/src/memory/pool/non_host_visible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ use crate::{
memory::{device_memory::MemoryAllocateInfo, DeviceMemory, DeviceMemoryAllocationError},
DeviceSize,
};
use std::{
cmp,
ops::Range,
sync::{Arc, Mutex},
};
use parking_lot::Mutex;
use std::{cmp, ops::Range, sync::Arc};

/// Memory pool that operates on a given memory type.
#[derive(Debug)]
Expand Down Expand Up @@ -77,7 +74,7 @@ impl StandardNonHostVisibleMemoryTypePool {
}

// Find a location.
let mut occupied = me.occupied.lock().unwrap();
let mut occupied = me.occupied.lock();

// Try finding an entry in already-allocated chunks.
for &mut (ref dev_mem, ref mut entries) in occupied.iter_mut() {
Expand Down Expand Up @@ -177,7 +174,7 @@ impl StandardNonHostVisibleMemoryTypePoolAlloc {

impl Drop for StandardNonHostVisibleMemoryTypePoolAlloc {
fn drop(&mut self) {
let mut occupied = self.pool.occupied.lock().unwrap();
let mut occupied = self.pool.occupied.lock();

let entries = occupied
.iter_mut()
Expand Down
5 changes: 3 additions & 2 deletions vulkano/src/memory/pool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use crate::{
},
DeviceSize,
};
use parking_lot::Mutex;
use std::{
collections::{hash_map::Entry, HashMap},
sync::{Arc, Mutex},
sync::Arc,
};

#[derive(Debug)]
Expand Down Expand Up @@ -53,7 +54,7 @@ fn generic_allocation(
layout: AllocLayout,
map: MappingRequirement,
) -> Result<StandardMemoryPoolAlloc, DeviceMemoryAllocationError> {
let mut pools = mem_pool.pools.lock().unwrap();
let mut pools = mem_pool.pools.lock();

let memory_type_host_visible = memory_type.is_host_visible();
assert!(memory_type_host_visible || map == MappingRequirement::DoNotMap);
Expand Down
12 changes: 6 additions & 6 deletions vulkano/src/sync/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Event {
/// For most applications, using the event pool should be preferred,
/// in order to avoid creating new events every frame.
pub fn from_pool(device: Arc<Device>) -> Result<Event, OomError> {
let handle = device.event_pool().lock().unwrap().pop();
let handle = device.event_pool().lock().pop();
let event = match handle {
Some(handle) => {
unsafe {
Expand Down Expand Up @@ -181,7 +181,7 @@ impl Drop for Event {
unsafe {
if self.must_put_in_pool {
let raw_event = self.handle;
self.device.event_pool().lock().unwrap().push(raw_event);
self.device.event_pool().lock().push(raw_event);
} else {
let fns = self.device.fns();
(fns.v1_0.destroy_event)(self.device.internal_object(), self.handle, ptr::null());
Expand Down Expand Up @@ -274,16 +274,16 @@ mod tests {
fn event_pool() {
let (device, _) = gfx_dev_and_queue!();

assert_eq!(device.event_pool().lock().unwrap().len(), 0);
assert_eq!(device.event_pool().lock().len(), 0);
let event1_internal_obj = {
let event = Event::from_pool(device.clone()).unwrap();
assert_eq!(device.event_pool().lock().unwrap().len(), 0);
assert_eq!(device.event_pool().lock().len(), 0);
event.internal_object()
};

assert_eq!(device.event_pool().lock().unwrap().len(), 1);
assert_eq!(device.event_pool().lock().len(), 1);
let event2 = Event::from_pool(device.clone()).unwrap();
assert_eq!(device.event_pool().lock().unwrap().len(), 0);
assert_eq!(device.event_pool().lock().len(), 0);
assert_eq!(event2.internal_object(), event1_internal_obj);
}
}
12 changes: 6 additions & 6 deletions vulkano/src/sync/fence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Fence {
/// For most applications, using the fence pool should be preferred,
/// in order to avoid creating new fences every frame.
pub fn from_pool(device: Arc<Device>) -> Result<Fence, OomError> {
let handle = device.fence_pool().lock().unwrap().pop();
let handle = device.fence_pool().lock().pop();
let fence = match handle {
Some(handle) => {
unsafe {
Expand Down Expand Up @@ -332,7 +332,7 @@ impl Drop for Fence {
unsafe {
if self.must_put_in_pool {
let raw_fence = self.handle;
self.device.fence_pool().lock().unwrap().push(raw_fence);
self.device.fence_pool().lock().push(raw_fence);
} else {
let fns = self.device.fns();
(fns.v1_0.destroy_fence)(self.device.internal_object(), self.handle, ptr::null());
Expand Down Expand Up @@ -576,16 +576,16 @@ mod tests {
fn fence_pool() {
let (device, _) = gfx_dev_and_queue!();

assert_eq!(device.fence_pool().lock().unwrap().len(), 0);
assert_eq!(device.fence_pool().lock().len(), 0);
let fence1_internal_obj = {
let fence = Fence::from_pool(device.clone()).unwrap();
assert_eq!(device.fence_pool().lock().unwrap().len(), 0);
assert_eq!(device.fence_pool().lock().len(), 0);
fence.internal_object()
};

assert_eq!(device.fence_pool().lock().unwrap().len(), 1);
assert_eq!(device.fence_pool().lock().len(), 1);
let fence2 = Fence::from_pool(device.clone()).unwrap();
assert_eq!(device.fence_pool().lock().unwrap().len(), 0);
assert_eq!(device.fence_pool().lock().len(), 0);
assert_eq!(fence2.internal_object(), fence1_internal_obj);
}
}
12 changes: 6 additions & 6 deletions vulkano/src/sync/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Semaphore {
/// For most applications, using the pool should be preferred,
/// in order to avoid creating new semaphores every frame.
pub fn from_pool(device: Arc<Device>) -> Result<Semaphore, SemaphoreCreationError> {
let handle = device.semaphore_pool().lock().unwrap().pop();
let handle = device.semaphore_pool().lock().pop();
let semaphore = match handle {
Some(handle) => Semaphore {
device,
Expand Down Expand Up @@ -216,7 +216,7 @@ impl Drop for Semaphore {
unsafe {
if self.must_put_in_pool {
let raw_sem = self.handle;
self.device.semaphore_pool().lock().unwrap().push(raw_sem);
self.device.semaphore_pool().lock().push(raw_sem);
} else {
let fns = self.device.fns();
(fns.v1_0.destroy_semaphore)(
Expand Down Expand Up @@ -566,16 +566,16 @@ mod tests {
fn semaphore_pool() {
let (device, _) = gfx_dev_and_queue!();

assert_eq!(device.semaphore_pool().lock().unwrap().len(), 0);
assert_eq!(device.semaphore_pool().lock().len(), 0);
let sem1_internal_obj = {
let sem = Semaphore::from_pool(device.clone()).unwrap();
assert_eq!(device.semaphore_pool().lock().unwrap().len(), 0);
assert_eq!(device.semaphore_pool().lock().len(), 0);
sem.internal_object()
};

assert_eq!(device.semaphore_pool().lock().unwrap().len(), 1);
assert_eq!(device.semaphore_pool().lock().len(), 1);
let sem2 = Semaphore::from_pool(device.clone()).unwrap();
assert_eq!(device.semaphore_pool().lock().unwrap().len(), 0);
assert_eq!(device.semaphore_pool().lock().len(), 0);
assert_eq!(sem2.internal_object(), sem1_internal_obj);
}

Expand Down

0 comments on commit 38ae563

Please sign in to comment.