Skip to content

Commit af15008

Browse files
committed
zephyr: clippy: Add Safety sections to documentation
As flagged by clippy, add `# Safety` sections to public documentation of functions that are marked as `unsafe`. Signed-off-by: David Brown <[email protected]>
1 parent b87c07f commit af15008

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

zephyr/src/device/gpio.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ pub struct GpioToken(());
2424
static GPIO_TOKEN: Unique = Unique::new();
2525

2626
impl GpioToken {
27-
/// Retrieves the gpio token. This is unsafe because lots of code in zephyr operates on the
28-
/// gpio drivers.
27+
/// Retrieves the gpio token.
28+
///
29+
/// # Safety
30+
/// This is unsafe because lots of code in zephyr operates on the gpio drivers. The user of the
31+
/// gpio subsystem, in general should either coordinate all gpio access across the system (the
32+
/// token coordinates this only within Rust code), or verify that the particular gpio driver and
33+
/// methods are thread safe.
2934
pub unsafe fn get_instance() -> Option<GpioToken> {
3035
if !GPIO_TOKEN.once() {
3136
return None;
@@ -112,6 +117,12 @@ impl GpioPin {
112117
}
113118

114119
/// Configure a single pin.
120+
///
121+
/// # Safety
122+
///
123+
/// The `_token` enforces single threaded use of gpios from Rust code. However, many drivers
124+
/// within Zephyr use GPIOs, and to use gpios safely, the caller must ensure that there is
125+
/// either not simultaneous use, or the gpio driver in question is thread safe.
115126
pub unsafe fn configure(&mut self, _token: &mut GpioToken, extra_flags: raw::gpio_flags_t) {
116127
// TODO: Error?
117128
unsafe {
@@ -124,6 +135,12 @@ impl GpioPin {
124135
}
125136

126137
/// Toggle pin level.
138+
///
139+
/// # Safety
140+
///
141+
/// The `_token` enforces single threaded use of gpios from Rust code. However, many drivers
142+
/// within Zephyr use GPIOs, and to use gpios safely, the caller must ensure that there is
143+
/// either not simultaneous use, or the gpio driver in question is thread safe.
127144
pub unsafe fn toggle_pin(&mut self, _token: &mut GpioToken) {
128145
// TODO: Error?
129146
unsafe {

zephyr/src/logging/impl_printk.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ static PRINTK_LOGGER: PrintkLogger = PrintkLogger;
3535

3636
/// Set the log handler to log messages through printk in Zephyr.
3737
///
38+
/// # Safety
39+
///
3840
/// This is unsafe due to racy issues in the log framework on targets that do not support atomic
39-
/// pointers.
41+
/// pointers. As long as this is called ever by a single thread, it is safe to use.
4042
pub unsafe fn set_logger() -> Result<(), SetLoggerError> {
4143
super::set_logger_internal(&PRINTK_LOGGER)
4244
}

zephyr/src/object.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,13 @@ impl<T> StaticKernelObject<T>
167167
where
168168
StaticKernelObject<T>: Wrapped,
169169
{
170-
/// Construct an empty of these objects, with the zephyr data zero-filled. This is safe in the
171-
/// sense that Zephyr we track the initialization, they start in the uninitialized state, and
172-
/// the zero value of the initialize atomic indicates that it is uninitialized.
170+
/// Construct an empty of these objects, with the zephyr data zero-filled.
171+
///
172+
/// # Safety
173+
///
174+
/// This is safe in the sense that Zephyr we track the initialization, they start in the
175+
/// uninitialized state, and the zero value of the initialize atomic indicates that it is
176+
/// uninitialized.
173177
pub const unsafe fn new() -> StaticKernelObject<T> {
174178
StaticKernelObject {
175179
value: unsafe { mem::zeroed() },

zephyr/src/sys/queue.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ impl Queue {
5050
/// safely.
5151
///
5252
/// [`Message`]: crate::sync::channel::Message
53+
///
54+
/// # Safety
55+
///
56+
/// Zephyr has specific requirements on the memory given in data, which can be summarized as:
57+
/// - The memory must remain valid until after the data is returned, via recv.
58+
/// - The first `usize` in the pointed data will be mutated by Zephyr to manage structures.
59+
/// - This first field must not be modified by any code while the message is enqueued.
60+
///
61+
/// These are easiest to satisfy by ensuring the message is Boxed, and owned by the queue
62+
/// system.
5363
pub unsafe fn send(&self, data: *mut c_void) {
5464
k_queue_append(self.item.get(), data)
5565
}
@@ -64,6 +74,11 @@ impl Queue {
6474
/// [`Forever`]: crate::time::Forever
6575
/// [`NoWait`]: crate::time::NoWait
6676
/// [`Duration`]: crate::time::Duration
77+
///
78+
/// # Safety
79+
///
80+
/// Once an item is received from a queue, ownership is returned to the caller, and Zephyr no
81+
/// longer depends on it not being freed, or the first `usize` field being for its use.
6782
pub unsafe fn recv<T>(&self, timeout: T) -> *mut c_void
6883
where
6984
T: Into<Timeout>,

zephyr/src/sys/thread.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ impl Thread {
227227

228228
/// Simple thread spawn. This is unsafe because of the raw values being used. This can be
229229
/// useful in systems without an allocator defined.
230+
///
231+
/// # Safety
232+
///
233+
/// Safe use follows similar requirements to using this safely from within C code. Passing Rust
234+
/// values through this interface is difficult to get right, and it is generally recommended to
235+
/// use [`spawn`].
230236
pub unsafe fn simple_spawn(
231237
mut self,
232238
child: k_thread_entry_t,

0 commit comments

Comments
 (0)