Debian Codegraph @

third_party/rust/rustix/src/thread/mod.rs

raw
1 //! Thread-associated operations.
2
3 #[cfg(not(target_os = "redox"))]
4 mod clock;
5 #[cfg(linux_kernel)]
6 pub mod futex;
7 #[cfg(linux_kernel)]
8 mod id;
9 #[cfg(linux_kernel)]
10 mod libcap;
11 #[cfg(linux_kernel)]
12 mod prctl;
13 #[cfg(linux_kernel)]
14 mod setns;
15
16 #[allow(deprecated)]
17 #[cfg(linux_kernel)]
18 pub use crate::backend::thread::futex::FutexOperation;
19 #[cfg(linux_kernel)]
20 pub use crate::thread::futex::{
21 Flags as FutexFlags, OWNER_DIED as FUTEX_OWNER_DIED, WAITERS as FUTEX_WAITERS,
22 };
23 #[cfg(not(target_os = "redox"))]
24 pub use clock::*;
25 #[cfg(linux_kernel)]
26 pub use id::{
27 gettid, set_thread_gid, set_thread_groups, set_thread_res_gid, set_thread_res_uid,
28 set_thread_uid, Gid, Pid, RawGid, RawPid, RawUid, Uid,
29 };
30 #[cfg(linux_kernel)]
31 pub use libcap::{capabilities, set_capabilities, CapabilityFlags, CapabilitySets};
32 #[cfg(linux_kernel)]
33 pub use prctl::*;
34 #[cfg(linux_kernel)]
35 pub use setns::*;
36
37 /// DEPRECATED: There are now individual functions available to perform futex
38 /// operations with improved type safety. See the [futex module].
39 ///
40 /// `futex(uaddr, op, val, utime, uaddr2, val3)`
41 ///
42 /// # References
43 /// - [Linux `futex` system call]
44 /// - [Linux `futex` feature]
45 ///
46 /// # Safety
47 ///
48 /// This is a very low-level feature for implementing synchronization
49 /// primitives. See the references links above.
50 ///
51 /// [Linux `futex` system call]: https://man7.org/linux/man-pages/man2/futex.2.html
52 /// [Linux `futex` feature]: https://man7.org/linux/man-pages/man7/futex.7.html
53 /// [futex module]: mod@crate::thread::futex
54 #[cfg(linux_kernel)]
55 #[allow(unsafe_code, deprecated)]
56 #[inline]
57 pub unsafe fn futex(
58 uaddr: *mut u32,
59 op: FutexOperation,
60 flags: FutexFlags,
61 val: u32,
62 utime: *const Timespec,
63 uaddr2: *mut u32,
64 val3: u32,
65 ) -> crate::io::Result<usize> {
66 use crate::backend::thread::futex::Operation;
67 use crate::backend::thread::syscalls::{futex_timeout, futex_val2};
68 use core::mem::transmute;
69 use core::sync::atomic::AtomicU32;
70 use FutexOperation::*;
71
72 match op {
73 Wait | LockPi | WaitBitset => futex_timeout(
74 uaddr as *const AtomicU32,
75 transmute::<FutexOperation, Operation>(op),
76 flags,
77 val,
78 utime,
79 uaddr2 as *const AtomicU32,
80 val3,
81 ),
82 Wake | Fd | Requeue | CmpRequeue | WakeOp | UnlockPi | TrylockPi => futex_val2(
83 uaddr as *const AtomicU32,
84 transmute::<FutexOperation, Operation>(op),
85 flags,
86 val,
87 utime as usize as u32,
88 uaddr2 as *const AtomicU32,
89 val3,
90 ),
91 }
92 }