List threads per process
Thanks for a great package! I am currently trying to add macOS support to Linux-only application using it, and discovered it's not possible to get the threads for a process on macOS.
This is possible to do, e.g. here's a Python psutil example:
>>> import psutil
>>> import threading
>>> import time
>>> threading.Thread(target=lambda: time.sleep(1000000)).start()
>>> p = psutil.Process()
>>> p.threads(
>>> p.threads()
[pthread(id=1, user_time=0.098023, system_time=0.035946), pthread(id=2, user_time=0.000124, system_time=3.4e-05)]
The relevant code in psutil can be seen here: https://github.com/giampaolo/psutil/blob/bea3cf2d16899251b4b5f6b2609db9881645ea2d/psutil/_psutil_osx.c#L888
The Python psutil library does not include some info that I would like (thread scheduler status) but I suspect that is still obtainable. In particular, the thread_basic_info struct that it uses appears to have a run_state flag, based on some googling (https://developer.apple.com/documentation/kernel/thread_basic_info_t). So it seems like sysinfo could include much of the info it already includes for each thread on Linux.
If this is something you'd be happy to accept, I can try to submit a PR.
The big question is: is it possible to get this information on all sysinfo supported systems? If it's possible for mac, the code should be more or less the same for freebsd, and I assume linux shouldn't be too complicated either. But the big unknown is windows. :)
It's already provided on Linux, the tasks field of the Process struct: https://github.com/GuillaumeGomez/sysinfo/blob/d9746a2280f42833f0ed984ede2d944f8bcd3aca/src/linux/process.rs#L85
psutil has code for FreeBSD (https://github.com/giampaolo/psutil/blob/f716afc81d4c9ded08b23d376612e1491d3ea5da/psutil/arch/freebsd/proc.c#L278) and Windows (https://github.com/giampaolo/psutil/blob/bea3cf2d16899251b4b5f6b2609db9881645ea2d/psutil/_psutil_windows.c#L689) so I assume it's possible there as well, but I personally only care about macOS this month.
(Of course, the presumption that threads are the same as processes is kinda Linux-specific, so if you wanted a consistent thread information API it might look somewhat different. And e.g. scheduler status is definitely available on Linux and macOS, but not sure about FreeBSD and Windows...)
If available on macOS, it'll be very likely available on freebsd as well. For windows, big unknown as usual.
And yes, if we provide it for all systems, at this point we can directly provide a unified API for it.
Getting the threads of a specific process is easy in Windows. You just call CreateToolhelp32Snapshot to get a snapshot handle, then you can enumerate the threads with Thread32First and Thread32Next.
I have a working implementation in a library I never finished writing (the windows part works though, since that's what I actually needed)