|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! A condition variable. |
| 4 | +//! |
| 5 | +//! This module allows Rust code to use the kernel's [`struct wait_queue_head`] as a condition |
| 6 | +//! variable. |
| 7 | +
|
| 8 | +use super::{Guard, Lock, NeedsLockClass}; |
| 9 | +use crate::{bindings, c_types, CStr}; |
| 10 | +use core::{cell::UnsafeCell, marker::PhantomPinned, mem::MaybeUninit, pin::Pin}; |
| 11 | + |
| 12 | +extern "C" { |
| 13 | + fn rust_helper_init_wait(wq: *mut bindings::wait_queue_entry); |
| 14 | + fn rust_helper_signal_pending() -> c_types::c_int; |
| 15 | +} |
| 16 | + |
| 17 | +/// Safely initialises a [`CondVar`] with the given name, generating a new lock class. |
| 18 | +#[macro_export] |
| 19 | +macro_rules! condvar_init { |
| 20 | + ($condvar:expr, $name:literal) => { |
| 21 | + $crate::init_with_lockdep!($condvar, $name) |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +// TODO: `bindgen` is not generating this constant. Figure out why. |
| 26 | +const POLLFREE: u32 = 0x4000; |
| 27 | + |
| 28 | +/// Exposes the kernel's [`struct wait_queue_head`] as a condition variable. It allows the caller to |
| 29 | +/// atomically release the given lock and go to sleep. It reacquires the lock when it wakes up. And |
| 30 | +/// it wakes up when notified by another thread (via [`CondVar::notify_one`] or |
| 31 | +/// [`CondVar::notify_all`]) or because the thread received a signal. |
| 32 | +/// |
| 33 | +/// [`struct wait_queue_head`]: ../../../include/linux/wait.h |
| 34 | +pub struct CondVar { |
| 35 | + pub(crate) wait_list: UnsafeCell<bindings::wait_queue_head>, |
| 36 | + |
| 37 | + /// A condvar needs to be pinned because it contains a [`struct list_head`] that is |
| 38 | + /// self-referential, so it cannot be safely moved once it is initialised. |
| 39 | + _pin: PhantomPinned, |
| 40 | +} |
| 41 | + |
| 42 | +// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on any thread. |
| 43 | +unsafe impl Send for CondVar {} |
| 44 | + |
| 45 | +// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on multiple threads |
| 46 | +// concurrently. |
| 47 | +unsafe impl Sync for CondVar {} |
| 48 | + |
| 49 | +impl CondVar { |
| 50 | + /// Constructs a new conditional variable. |
| 51 | + /// |
| 52 | + /// # Safety |
| 53 | + /// |
| 54 | + /// The caller must call `CondVar::init` before using the conditional variable. |
| 55 | + pub unsafe fn new() -> Self { |
| 56 | + Self { |
| 57 | + wait_list: UnsafeCell::new(bindings::wait_queue_head::default()), |
| 58 | + _pin: PhantomPinned, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the |
| 63 | + /// thread to sleep. It wakes up when notified by [`CondVar::notify_one`] or |
| 64 | + /// [`CondVar::notify_all`], or when the thread receives a signal. |
| 65 | + /// |
| 66 | + /// Returns whether there is a signal pending. |
| 67 | + pub fn wait<L: Lock>(&self, g: &Guard<L>) -> bool { |
| 68 | + let l = g.lock; |
| 69 | + let mut wait = MaybeUninit::<bindings::wait_queue_entry>::uninit(); |
| 70 | + |
| 71 | + // SAFETY: `wait` points to valid memory. |
| 72 | + unsafe { rust_helper_init_wait(wait.as_mut_ptr()) }; |
| 73 | + |
| 74 | + // SAFETY: Both `wait` and `wait_list` point to valid memory. |
| 75 | + unsafe { |
| 76 | + bindings::prepare_to_wait_exclusive( |
| 77 | + self.wait_list.get(), |
| 78 | + wait.as_mut_ptr(), |
| 79 | + bindings::TASK_INTERRUPTIBLE as _, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + // SAFETY: The guard is evidence that the caller owns the lock. |
| 84 | + unsafe { l.unlock() }; |
| 85 | + |
| 86 | + // SAFETY: No arguments, switches to another thread. |
| 87 | + unsafe { bindings::schedule() }; |
| 88 | + |
| 89 | + l.lock_noguard(); |
| 90 | + |
| 91 | + // SAFETY: Both `wait` and `wait_list` point to valid memory. |
| 92 | + unsafe { bindings::finish_wait(self.wait_list.get(), wait.as_mut_ptr()) }; |
| 93 | + |
| 94 | + // SAFETY: No arguments, just checks `current` for pending signals. |
| 95 | + unsafe { rust_helper_signal_pending() != 0 } |
| 96 | + } |
| 97 | + |
| 98 | + /// Calls the kernel function to notify the appropriate number of threads with the given flags. |
| 99 | + fn notify(&self, count: i32, flags: u32) { |
| 100 | + // SAFETY: `wait_list` points to valid memory. |
| 101 | + unsafe { |
| 102 | + bindings::__wake_up( |
| 103 | + self.wait_list.get(), |
| 104 | + bindings::TASK_NORMAL, |
| 105 | + count, |
| 106 | + flags as _, |
| 107 | + ) |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + /// Wakes a single waiter up, if any. This is not 'sticky' in the sense that if no thread is |
| 112 | + /// waiting, the notification is lost completely (as opposed to automatically waking up the |
| 113 | + /// next waiter). |
| 114 | + pub fn notify_one(&self) { |
| 115 | + self.notify(1, 0); |
| 116 | + } |
| 117 | + |
| 118 | + /// Wakes all waiters up, if any. This is not 'sticky' in the sense that if no thread is |
| 119 | + /// waiting, the notification is lost completely (as opposed to automatically waking up the |
| 120 | + /// next waiter). |
| 121 | + pub fn notify_all(&self) { |
| 122 | + self.notify(0, 0); |
| 123 | + } |
| 124 | + |
| 125 | + /// Wakes all waiters up. If they were added by `epoll`, they are also removed from the list of |
| 126 | + /// waiters. This is useful when cleaning up a condition variable that may be waited on by |
| 127 | + /// threads that use `epoll`. |
| 128 | + pub fn free_waiters(&self) { |
| 129 | + self.notify(1, bindings::POLLHUP | POLLFREE); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl NeedsLockClass for CondVar { |
| 134 | + unsafe fn init(self: Pin<&Self>, name: CStr<'static>, key: *mut bindings::lock_class_key) { |
| 135 | + bindings::__init_waitqueue_head(self.wait_list.get(), name.as_ptr() as _, key); |
| 136 | + } |
| 137 | +} |
0 commit comments