mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
uucore: Sync thread_ids() method from procps
This commit is contained in:
parent
9c0ff86caa
commit
16c174d826
1 changed files with 47 additions and 1 deletions
|
@ -129,6 +129,8 @@ pub struct ProcessInformation {
|
|||
cached_stat: Option<Rc<Vec<String>>>,
|
||||
|
||||
cached_start_time: Option<u64>,
|
||||
|
||||
cached_thread_ids: Option<Rc<Vec<usize>>>,
|
||||
}
|
||||
|
||||
impl ProcessInformation {
|
||||
|
@ -271,8 +273,33 @@ impl ProcessInformation {
|
|||
|
||||
Teletype::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
pub fn thread_ids(&mut self) -> Rc<Vec<usize>> {
|
||||
if let Some(c) = &self.cached_thread_ids {
|
||||
return Rc::clone(c);
|
||||
}
|
||||
|
||||
let thread_ids_dir = format!("/proc/{}/task", self.pid);
|
||||
let result = Rc::new(
|
||||
WalkDir::new(thread_ids_dir)
|
||||
.min_depth(1)
|
||||
.max_depth(1)
|
||||
.follow_links(false)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flat_map(|it| {
|
||||
it.path()
|
||||
.file_name()
|
||||
.and_then(|it| it.to_str())
|
||||
.and_then(|it| it.parse::<usize>().ok())
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
self.cached_thread_ids = Some(Rc::clone(&result));
|
||||
Rc::clone(&result)
|
||||
}
|
||||
}
|
||||
impl TryFrom<DirEntry> for ProcessInformation {
|
||||
type Error = io::Error;
|
||||
|
||||
|
@ -399,6 +426,25 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_thread_ids() {
|
||||
let main_tid = unsafe { uucore::libc::gettid() };
|
||||
std::thread::spawn(move || {
|
||||
let mut pid_entry = ProcessInformation::try_new(
|
||||
PathBuf::from_str(&format!("/proc/{}", current_pid())).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let thread_ids = pid_entry.thread_ids();
|
||||
|
||||
assert!(thread_ids.contains(&(main_tid as usize)));
|
||||
|
||||
let new_thread_tid = unsafe { uucore::libc::gettid() };
|
||||
assert!(thread_ids.contains(&(new_thread_tid as usize)));
|
||||
})
|
||||
.join()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stat_split() {
|
||||
let case = "32 (idle_inject/3) S 2 0 0 0 -1 69238848 0 0 0 0 0 0 0 0 -51 0 1 0 34 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 3 50 1 0 0 0 0 0 0 0 0 0 0 0";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue