|
|
@ -2,7 +2,7 @@ use crate::Error; |
|
|
|
use std::sync::Mutex; |
|
|
|
use std::sync::Mutex; |
|
|
|
pub(crate) struct ServiceReference { |
|
|
|
pub(crate) struct ServiceReference { |
|
|
|
counter: &'static Mutex<usize>, |
|
|
|
counter: &'static Mutex<usize>, |
|
|
|
close: Box<dyn Fn()>, |
|
|
|
close: Box<dyn Fn() + Send + Sync>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl ServiceReference { |
|
|
|
impl ServiceReference { |
|
|
@ -14,9 +14,9 @@ impl ServiceReference { |
|
|
|
) -> crate::Result<Self> |
|
|
|
) -> crate::Result<Self> |
|
|
|
where |
|
|
|
where |
|
|
|
S: FnOnce() -> crate::Result<()>, |
|
|
|
S: FnOnce() -> crate::Result<()>, |
|
|
|
E: Fn() + 'static, |
|
|
|
E: Fn() + Send + Sync + 'static, |
|
|
|
{ |
|
|
|
{ |
|
|
|
let mut value = counter.lock().unwrap(); // todo: handle poisoning
|
|
|
|
let mut value = counter.lock().expect("Mutex Counter for ServiceReference is poisoned"); // todo: handle poisoning
|
|
|
|
|
|
|
|
|
|
|
|
if *value == 0 { |
|
|
|
if *value == 0 { |
|
|
|
start()?; |
|
|
|
start()?; |
|
|
@ -35,7 +35,7 @@ impl ServiceReference { |
|
|
|
|
|
|
|
|
|
|
|
impl Drop for ServiceReference { |
|
|
|
impl Drop for ServiceReference { |
|
|
|
fn drop(&mut self) { |
|
|
|
fn drop(&mut self) { |
|
|
|
let mut value = self.counter.lock().unwrap(); // should probably handle poisoning - could just map_err to ignore it.
|
|
|
|
let mut value = self.counter.lock().expect("Mutex Counter for ServiceReference is poisoned"); // todo: handle poisoning
|
|
|
|
*value -= 1; |
|
|
|
*value -= 1; |
|
|
|
if *value == 0 { |
|
|
|
if *value == 0 { |
|
|
|
(self.close)(); |
|
|
|
(self.close)(); |
|
|
|