|
|
|
@ -2,7 +2,7 @@ use crate::Error;
@@ -2,7 +2,7 @@ use crate::Error;
|
|
|
|
|
use std::sync::Mutex; |
|
|
|
|
pub(crate) struct ServiceReference { |
|
|
|
|
counter: &'static Mutex<usize>, |
|
|
|
|
close: Box<dyn Fn()>, |
|
|
|
|
close: Box<dyn Fn() + Send + Sync>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl ServiceReference { |
|
|
|
@ -14,9 +14,9 @@ impl ServiceReference {
@@ -14,9 +14,9 @@ impl ServiceReference {
|
|
|
|
|
) -> crate::Result<Self> |
|
|
|
|
where |
|
|
|
|
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 { |
|
|
|
|
start()?; |
|
|
|
@ -35,7 +35,7 @@ impl ServiceReference {
@@ -35,7 +35,7 @@ impl ServiceReference {
|
|
|
|
|
|
|
|
|
|
impl Drop for ServiceReference { |
|
|
|
|
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; |
|
|
|
|
if *value == 0 { |
|
|
|
|
(self.close)(); |
|
|
|
|