1
0
Fork 0
forked from wry/wry

autocommit 2022-03-13 19:41:04 CET

This commit is contained in:
Julian Orth 2022-03-13 19:41:04 +01:00
parent e18be65210
commit 156bd5b042
8 changed files with 171 additions and 96 deletions

47
src/utils/fdcloser.rs Normal file
View file

@ -0,0 +1,47 @@
use std::mem;
use std::rc::Rc;
use std::sync::Arc;
use parking_lot::{Condvar, Mutex};
use uapi::OwnedFd;
pub struct FdCloser {
fds: Mutex<Vec<OwnedFd>>,
cv: Condvar,
}
impl FdCloser {
pub fn new() -> Arc<Self> {
let slf = Arc::new(Self {
fds: Mutex::new(Vec::new()),
cv: Condvar::new(),
});
let slf2 = slf.clone();
std::thread::spawn(move || {
let mut fds = vec![];
let mut lock = slf2.fds.lock();
loop {
mem::swap(&mut *lock, &mut fds);
if fds.len() > 0 {
drop(lock);
fds.clear();
lock = slf2.fds.lock();
} else {
slf2.cv.wait(&mut lock);
}
}
});
slf
}
pub fn close(&self, fd: Rc<OwnedFd>) {
match Rc::try_unwrap(fd) {
Ok(fd) => {
self.fds.lock().push(fd);
self.cv.notify_all();
},
Err(_e) => {
log::warn!("Could not close file descriptor in separate thread. There are still references.");
}
}
}
}