autocommit 2022-03-13 19:41:04 CET
This commit is contained in:
parent
e18be65210
commit
156bd5b042
8 changed files with 171 additions and 96 deletions
47
src/utils/fdcloser.rs
Normal file
47
src/utils/fdcloser.rs
Normal 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -22,3 +22,4 @@ pub mod tri;
|
|||
pub mod vasprintf;
|
||||
pub mod vec_ext;
|
||||
pub mod vecstorage;
|
||||
pub mod fdcloser;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue