Skip to content

Commit 75cb572

Browse files
authored
Merge pull request #217 from wedsonaf/exports
Expose `signal_pending` and `cond_resched` to drivers.
2 parents 3b8575d + f51b971 commit 75cb572

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

rust/helpers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ void rust_helper_kunmap(struct page *page)
8181
}
8282
EXPORT_SYMBOL_GPL(rust_helper_kunmap);
8383

84+
int rust_helper_cond_resched(void)
85+
{
86+
return cond_resched();
87+
}
88+
EXPORT_SYMBOL_GPL(rust_helper_cond_resched);
89+
8490
#if !defined(CONFIG_ARM)
8591
// See https://github.com/rust-lang/rust-bindgen/issues/1671
8692
static_assert(__builtin_types_compatible_p(size_t, uintptr_t),

rust/kernel/sync/condvar.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
//! variable.
77
88
use super::{Guard, Lock, NeedsLockClass};
9-
use crate::{bindings, c_types, CStr};
9+
use crate::{bindings, CStr};
1010
use core::{cell::UnsafeCell, marker::PhantomPinned, mem::MaybeUninit, pin::Pin};
1111

1212
extern "C" {
1313
fn rust_helper_init_wait(wq: *mut bindings::wait_queue_entry);
14-
fn rust_helper_signal_pending() -> c_types::c_int;
1514
}
1615

1716
/// Safely initialises a [`CondVar`] with the given name, generating a new lock class.
@@ -92,8 +91,7 @@ impl CondVar {
9291
// SAFETY: Both `wait` and `wait_list` point to valid memory.
9392
unsafe { bindings::finish_wait(self.wait_list.get(), wait.as_mut_ptr()) };
9493

95-
// SAFETY: No arguments, just checks `current` for pending signals.
96-
unsafe { rust_helper_signal_pending() != 0 }
94+
super::signal_pending()
9795
}
9896

9997
/// Calls the kernel function to notify the appropriate number of threads with the given flags.

rust/kernel/sync/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! }
1818
//! ```
1919
20-
use crate::{bindings, CStr};
20+
use crate::{bindings, c_types, CStr};
2121
use core::pin::Pin;
2222

2323
mod arc;
@@ -34,6 +34,11 @@ pub use locked_by::LockedBy;
3434
pub use mutex::Mutex;
3535
pub use spinlock::SpinLock;
3636

37+
extern "C" {
38+
fn rust_helper_signal_pending() -> c_types::c_int;
39+
fn rust_helper_cond_resched() -> c_types::c_int;
40+
}
41+
3742
/// Safely initialises an object that has an `init` function that takes a name and a lock class as
3843
/// arguments, examples of these are [`Mutex`] and [`SpinLock`]. Each of them also provides a more
3944
/// specialised name that uses this macro.
@@ -66,3 +71,15 @@ pub trait NeedsLockClass {
6671
/// `key` must point to a valid memory location as it will be used by the kernel.
6772
unsafe fn init(self: Pin<&Self>, name: CStr<'static>, key: *mut bindings::lock_class_key);
6873
}
74+
75+
/// Determines if a signal is pending on the current process.
76+
pub fn signal_pending() -> bool {
77+
// SAFETY: No arguments, just checks `current` for pending signals.
78+
unsafe { rust_helper_signal_pending() != 0 }
79+
}
80+
81+
/// Reschedules the caller's task if needed.
82+
pub fn cond_resched() -> bool {
83+
// SAFETY: No arguments, reschedules `current` if needed.
84+
unsafe { rust_helper_cond_resched() != 0 }
85+
}

0 commit comments

Comments
 (0)