rust/kernel/sync/condvar.rs

Source file repositories/reference/linux-study-clean/rust/kernel/sync/condvar.rs

File Facts

System
Linux kernel
Corpus path
rust/kernel/sync/condvar.rs
Extension
.rs
Size
9585 bytes
Lines
259
Domain
Rust Kernel Layer
Bucket
Rust API Membrane
Inferred role
Rust Kernel Layer: implementation source
Status
source implementation candidate

Why This File Exists

Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.

Dependency Surface

Detected Declarations

Annotated Snippet

match (res as Jiffies, crate::current!().signal_pending()) {
            (jiffies, true) => CondVarTimeoutResult::Signal { jiffies },
            (0, false) => CondVarTimeoutResult::Timeout,
            (jiffies, false) => CondVarTimeoutResult::Woken { jiffies },
        }
    }

    /// Calls the kernel function to notify the appropriate number of threads.
    fn notify(&self, count: c_int) {
        // SAFETY: `wait_queue_head` points to valid memory.
        unsafe {
            bindings::__wake_up(
                self.wait_queue_head.get(),
                TASK_NORMAL,
                count,
                ptr::null_mut(),
            )
        };
    }

    /// Calls the kernel function to notify one thread synchronously.
    ///
    /// This method behaves like `notify_one`, except that it hints to the scheduler that the
    /// current thread is about to go to sleep, so it should schedule the target thread on the same
    /// CPU.
    #[inline]
    pub fn notify_sync(&self) {
        // SAFETY: `wait_queue_head` points to valid memory.
        unsafe { bindings::__wake_up_sync(self.wait_queue_head.get(), TASK_NORMAL) };
    }

    /// Wakes a single waiter up, if any.
    ///
    /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost
    /// completely (as opposed to automatically waking up the next waiter).
    #[inline]
    pub fn notify_one(&self) {
        self.notify(1);
    }

    /// Wakes all waiters up, if any.
    ///
    /// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost
    /// completely (as opposed to automatically waking up the next waiter).
    #[inline]
    pub fn notify_all(&self) {
        self.notify(0);
    }
}

/// The return type of `wait_timeout`.
pub enum CondVarTimeoutResult {
    /// The timeout was reached.
    Timeout,
    /// Somebody woke us up.
    Woken {
        /// Remaining sleep duration.
        jiffies: Jiffies,
    },
    /// A signal occurred.
    Signal {
        /// Remaining sleep duration.
        jiffies: Jiffies,
    },
}

Annotation

Implementation Notes