-
Notifications
You must be signed in to change notification settings - Fork 27
Start of Kobj support: Semaphores and Threads #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fa00bea
823b48a
45192f3
ed875c2
e1196a0
594bbc4
3e362a7
67e456b
d8fbb57
aed43fd
6598cd1
a9ea9a1
59907d7
6b3e831
e71bd6a
096d34b
32b8fbf
907c044
3d2477d
3fd043a
c88307f
1b92ea5
1971feb
97b217f
2ccb628
e8eebb2
a34507f
91d6f33
177a932
1cd9299
266a7a7
fdd97ae
d01153a
7375e44
8bbade3
98a15fe
861cb83
057e169
d11c5d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright (c) 2024 Linaro LTD | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! # Zephyr low-level synchronization primities. | ||
//! | ||
//! The `zephyr-sys` crate contains direct calls into the Zephyr C API. This interface, however, | ||
//! cannot be used from safe Rust. This crate attempts to be as direct an interface to some of | ||
//! these synchronization mechanisms, but without the need for unsafe. The other module | ||
//! `crate::sync` provides higher level interfaces that help manage synchronization in coordination | ||
//! with Rust's borrowing and sharing rules, and will generally provide much more usable | ||
//! interfaces. | ||
//! | ||
//! # Kernel objects | ||
//! | ||
//! Zephyr's primitives work with the concept of a kernel object. These are the data structures | ||
//! that are used by the Zephyr kernel to coordinate the operation of the primitives. In addition, | ||
//! they are where the protection barrier provided by `CONFIG_USERSPACE` is implemented. In order | ||
//! to use these primitives from a userspace thread two things must happen: | ||
//! | ||
//! - The kernel objects must be specially declared. All kernel objects in Zephyr will be built, | ||
//! at compile time, into a perfect hash table that is used to validate them. The special | ||
//! declaration will take care of this. | ||
//! - The objects must be granted permission to be used by the userspace thread. This can be | ||
//! managed either by specifically granting permission, or by using inheritance when creating the | ||
//! thread. | ||
//! | ||
//! At this time, only the first mechanism is implemented, and all kernel objects should be | ||
//! declared using the `crate::kobj_define!` macro. These then must be initialized, and then the | ||
//! special method `.get()` called, to retrieve the Rust-style value that is used to manage them. | ||
//! Later, there will be a pool mechanism to allow these kernel objects to be allocated and freed | ||
//! from a pool, although the objects will still be statically allocated. | ||
|
||
use crate::{ | ||
error::{Result, to_result_void}, | ||
object::{StaticKernelObject, Wrapped}, | ||
raw::{ | ||
k_sem, | ||
k_sem_init, | ||
k_sem_take, | ||
k_sem_give, | ||
k_sem_reset, | ||
k_sem_count_get, | ||
K_SEM_MAX_LIMIT, | ||
}, | ||
time::Timeout, | ||
}; | ||
|
||
/// A zephyr `k_sem` usable from safe Rust code. | ||
#[derive(Clone)] | ||
pub struct Semaphore { | ||
/// The raw Zephyr `k_sem`. | ||
item: *mut k_sem, | ||
} | ||
|
||
/// By nature, Semaphores are both Sync and Send. Safety is handled by the underlying Zephyr | ||
/// implementation (which is why Clone is also implemented). | ||
unsafe impl Sync for Semaphore {} | ||
unsafe impl Send for Semaphore {} | ||
|
||
impl Semaphore { | ||
/// Take a semaphore. | ||
/// | ||
/// Can be called from ISR if called with [`NoWait`]. | ||
pub fn take<T>(&mut self, timeout: T) -> Result<()> | ||
where T: Into<Timeout>, | ||
{ | ||
let timeout: Timeout = timeout.into(); | ||
let ret = unsafe { | ||
k_sem_take(self.item, timeout.0) | ||
}; | ||
to_result_void(ret) | ||
} | ||
|
||
/// Give a semaphore. | ||
/// | ||
/// This routine gives to the semaphore, unless the semaphore is already at its maximum | ||
/// permitted count. | ||
pub fn give(&mut self) { | ||
unsafe { | ||
k_sem_give(self.item) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to remind myself that omitting the semicolon with the last expression of a function returns that value 🦀 Does the documentation engine publish return type information for these functions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would have to be declared in the code, it is never inferred. In this case, give returns nothing, and the zephyr function is void. |
||
} | ||
} | ||
|
||
/// Resets a semaphor's count to zero. | ||
/// | ||
/// This resets the count to zero. Any outstanding [`take`] calls will be aborted with | ||
/// `Error(EAGAIN)`. | ||
pub fn reset(&mut self) { | ||
unsafe { | ||
k_sem_reset(self.item) | ||
} | ||
} | ||
|
||
/// Get a semaphore's count. | ||
/// | ||
/// Returns the current count. | ||
pub fn count_get(&mut self) -> usize { | ||
unsafe { | ||
k_sem_count_get(self.item) as usize | ||
} | ||
} | ||
} | ||
|
||
/// A static Zephyr `k_sem`. | ||
/// | ||
/// This is intended to be used from within the `kobj_define!` macro. It declares a static ksem | ||
/// that will be properly registered with the Zephyr kernel object system. Call [`take`] to get the | ||
/// [`Semaphore`] that is represents. | ||
pub type StaticSemaphore = StaticKernelObject<k_sem>; | ||
|
||
impl Wrapped for StaticKernelObject<k_sem> { | ||
type T = Semaphore; | ||
|
||
// TODO: Thoughts about how to give parameters to the initialzation. | ||
fn get_wrapped(&self) -> Semaphore { | ||
let ptr = self.value.get(); | ||
unsafe { | ||
k_sem_init(ptr, 0, K_SEM_MAX_LIMIT); | ||
} | ||
Semaphore { | ||
item: ptr, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to work fine as other code carefully avoids constructing this for non-static semahpores. Does zephyr also have dynamic ones? I found these internal pointers made it harder to reason about safety eventually.
I wonder: Have you considered something like:
?
(see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/types.rs#n261 / rust-lang/rust#43467 for context)
Then of course most code would work with a
&Semaphore
, but that seems like a more direct mapping of C -> Rust to me?If all you ever deal with are static objects, then I guess this is fine too. But I think the direct wrapping is going to be a pattern elsewhere eventually anyway? It seems to me like that may also allow to unify this together with the "static object" that currently needs to be defined separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
Semaphore
is not a static semaphore, it is a singleton reference to some kind of semaphore. The StaticSemaphore is one way of declaring the semaphores (and currently the only way).I intend to add dynamic variants of many of these, but as the Zephyr dynamic object support is rather poor at the moment, these will likely just have a pool of the objects that they take from. The
Semaphore
type will be the same, but there will lookly be aSemaphore::new
that returns one from the pool. When it is dropped, it will be returned to the pool.