S2: SurfacePool uses RefCell; the pool is Rc-shared on one thread, so the Mutex poison branches were dead
Browse files at this commit6b476c55bc81d8d8f5232459d0266d12bdca1151Parent: a7849451 changed file +11 -25
- crates/ec-hw/src/pool.rs +11 -25
crates/ec-hw/src/pool.rsmodified+11 -25
| @@ -13,12 +13,12 @@ | |||
| 13 | use ec_va::{Surface, SurfaceSpec}; | 13 | use ec_va::{Surface, SurfaceSpec}; |
| 14 | use std::ops::Deref; | 14 | use std::ops::Deref; |
| 15 | use std::rc::{Rc, Weak}; | 15 | use std::rc::{Rc, Weak}; |
| 16 | -use std::sync::Mutex; | 16 | +use std::cell::RefCell; |
| 17 | 17 | ||
| 18 | struct PoolInner { | 18 | struct PoolInner { |
| 19 | /// Every surface, in allocation order. | 19 | /// Every surface, in allocation order. |
| 20 | - all: Mutex<Vec<Rc<Surface>>>, | 20 | + all: RefCell<Vec<Rc<Surface>>>, |
| 21 | - free: Mutex<Vec<Rc<Surface>>>, | 21 | + free: RefCell<Vec<Rc<Surface>>>, |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | /// A fixed-size pool of identically specified surfaces. | 24 | /// A fixed-size pool of identically specified surfaces. |
| @@ -36,8 +36,8 @@ impl SurfacePool { | |||
| 36 | let all: Vec<Rc<Surface>> = surfaces.into_iter().map(Rc::new).collect(); | 36 | let all: Vec<Rc<Surface>> = surfaces.into_iter().map(Rc::new).collect(); |
| 37 | SurfacePool { | 37 | SurfacePool { |
| 38 | inner: Rc::new(PoolInner { | 38 | inner: Rc::new(PoolInner { |
| 39 | - free: Mutex::new(all.clone()), | 39 | + free: RefCell::new(all.clone()), |
| 40 | - all: Mutex::new(all), | 40 | + all: RefCell::new(all), |
| 41 | }), | 41 | }), |
| 42 | spec: *spec, | 42 | spec: *spec, |
| 43 | } | 43 | } |
| @@ -50,7 +50,7 @@ impl SurfacePool { | |||
| 50 | 50 | ||
| 51 | /// Total surfaces in the pool. | 51 | /// Total surfaces in the pool. |
| 52 | pub fn len(&self) -> usize { | 52 | pub fn len(&self) -> usize { |
| 53 | - self.inner.all.lock().map(|all| all.len()).unwrap_or(0) | 53 | + self.inner.all.borrow().len() |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | /// True when the pool holds no surfaces at all. | 56 | /// True when the pool holds no surfaces at all. |
| @@ -61,17 +61,13 @@ impl SurfacePool { | |||
| 61 | /// Add surfaces allocated with the pool's spec, free for [`SurfacePool::acquire`]. | 61 | /// Add surfaces allocated with the pool's spec, free for [`SurfacePool::acquire`]. |
| 62 | pub fn grow(&self, surfaces: Vec<Surface>) { | 62 | pub fn grow(&self, surfaces: Vec<Surface>) { |
| 63 | let surfaces: Vec<Rc<Surface>> = surfaces.into_iter().map(Rc::new).collect(); | 63 | let surfaces: Vec<Rc<Surface>> = surfaces.into_iter().map(Rc::new).collect(); |
| 64 | - if let Ok(mut all) = self.inner.all.lock() { | 64 | + self.inner.all.borrow_mut().extend(surfaces.iter().cloned()); |
| 65 | - all.extend(surfaces.iter().cloned()); | 65 | + self.inner.free.borrow_mut().extend(surfaces); |
| 66 | - } | ||
| 67 | - if let Ok(mut free) = self.inner.free.lock() { | ||
| 68 | - free.extend(surfaces); | ||
| 69 | - } | ||
| 70 | } | 66 | } |
| 71 | 67 | ||
| 72 | /// Surfaces currently available. | 68 | /// Surfaces currently available. |
| 73 | pub fn available(&self) -> usize { | 69 | pub fn available(&self) -> usize { |
| 74 | - self.inner.free.lock().map(|f| f.len()).unwrap_or(0) | 70 | + self.inner.free.borrow().len() |
| 75 | } | 71 | } |
| 76 | 72 | ||
| 77 | /// Take a free surface, or `None` when every one is still in use. | 73 | /// Take a free surface, or `None` when every one is still in use. |
| @@ -79,13 +75,7 @@ impl SurfacePool { | |||
| 79 | /// A `None` is the caller's cue to release output frames, not an error: a | 75 | /// A `None` is the caller's cue to release output frames, not an error: a |
| 80 | /// decoder that has handed out its whole pool is waiting on the consumer. | 76 | /// decoder that has handed out its whole pool is waiting on the consumer. |
| 81 | pub fn acquire(&self) -> Option<Rc<PooledSurface>> { | 77 | pub fn acquire(&self) -> Option<Rc<PooledSurface>> { |
| 82 | - // A poisoned lock means another thread panicked while holding it; the | 78 | + let surface = self.inner.free.borrow_mut().pop()?; |
| 83 | - // free list is a plain Vec, so its contents are still consistent. | ||
| 84 | - let mut free = match self.inner.free.lock() { | ||
| 85 | - Ok(free) => free, | ||
| 86 | - Err(poisoned) => poisoned.into_inner(), | ||
| 87 | - }; | ||
| 88 | - let surface = free.pop()?; | ||
| 89 | Some(Rc::new(PooledSurface { | 79 | Some(Rc::new(PooledSurface { |
| 90 | surface, | 80 | surface, |
| 91 | pool: Rc::downgrade(&self.inner), | 81 | pool: Rc::downgrade(&self.inner), |
| @@ -128,10 +118,6 @@ impl Drop for PooledSurface { | |||
| 128 | // The pool is gone, so the surface simply drops with this handle. | 118 | // The pool is gone, so the surface simply drops with this handle. |
| 129 | return; | 119 | return; |
| 130 | }; | 120 | }; |
| 131 | - let mut free = match pool.free.lock() { | 121 | + pool.free.borrow_mut().push(Rc::clone(&self.surface)); |
| 132 | - Ok(free) => free, | ||
| 133 | - Err(poisoned) => poisoned.into_inner(), | ||
| 134 | - }; | ||
| 135 | - free.push(Rc::clone(&self.surface)); | ||
| 136 | } | 122 | } |
| 137 | } | 123 | } |