dope_sys/thread/raw/mod.rs
1use std::{mem, ptr};
2
3use o3::base::marker::ThreadBound;
4
5use crate::{
6 Errno, kernel,
7 thread::{Attrs, Handle},
8};
9
10/// Returns the hardware admission, or None when the operating system must be queried.
11#[inline]
12pub fn crc32() -> Option<bool> {
13 cfg_select! {
14 target_arch = "x86_64" => {
15 let features = std::arch::x86_64::__cpuid(1);
16 Some(features.edx & (1 << 26) != 0 && features.ecx & (1 << 1) != 0)
17 }
18 target_arch = "aarch64" => {
19 None
20 }
21 _ => {
22 Some(false)
23 }
24 }
25}
26
27pub(super) fn entropy<S: kernel::raw::Source + ?Sized, const N: usize>(
28 bytes: &mut mem::MaybeUninit<[u8; N]>,
29) -> Result<&mut [u8; N], Errno> {
30 const { assert!(N <= 256) };
31 // SAFETY: bytes provides N writable bytes, within getentropy's 256-byte limit.
32 let result = unsafe { libc::getentropy(bytes.as_mut_ptr().cast(), N) };
33 // SAFETY: getentropy handles interruptions internally and returns zero or -1
34 // with unchanged native errno; success initializes the complete requested extent.
35 unsafe { kernel::raw::Status::new(result).try_zero::<S, Errno>() }?;
36 // SAFETY: success initializes all N bytes, including the empty array.
37 Ok(unsafe { bytes.assume_init_mut() })
38}
39
40/// # Safety
41/// Implementors access only their admitted context and resources, with the promised thread exclusivity.
42pub unsafe trait Task: Sized {
43 /// # Safety
44 /// The context and all resources it uses must retain their admitted access rights through this execution.
45 unsafe fn run(context: ptr::NonNull<Self>);
46}
47
48/// One typed context admitted for a single native execution.
49pub struct Entry<T: Task> {
50 context: ptr::NonNull<T>,
51}
52
53impl<T: Task> Entry<T> {
54 /// # Safety
55 /// Retain the initialized context and its borrowed resources at stable addresses through discard, creation failure, or successful join.
56 /// All accesses must satisfy T's thread exclusivity contract; forgetting the handle does not release these obligations.
57 pub const unsafe fn new(context: ptr::NonNull<T>) -> Self {
58 Self { context }
59 }
60
61 /// Transfers this admitted entry into one joinable native thread.
62 pub fn spawn(self, attrs: &Attrs) -> Result<Handle, Errno> {
63 let mut thread = mem::MaybeUninit::uninit();
64 // SAFETY: attrs and the writable handle outlive this call. The consumed
65 // entry preserves T's exact context and access through join.
66 // pthread_create returns zero or a positive error code directly.
67 unsafe {
68 kernel::raw::Status::new(libc::pthread_create(
69 thread.as_mut_ptr(),
70 attrs.as_ptr(),
71 Self::run,
72 self.context.as_ptr().cast(),
73 ))
74 .try_pthread()
75 }?;
76 // SAFETY: pthread_create wrote the handle before returning success.
77 Ok(Handle(unsafe { thread.assume_init() }, ThreadBound::NEW))
78 }
79
80 extern "C" fn run(context: *mut std::ffi::c_void) -> *mut std::ffi::c_void {
81 // SAFETY: spawn passes this admitted nonnull T context exactly once;
82 // its owner retains the allocation and access rights through join.
83 unsafe { T::run(ptr::NonNull::new_unchecked(context.cast())) };
84 ptr::null_mut()
85 }
86}