Skip to content
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

Fixes to Thread #24

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions zephyr/src/sys/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ extern crate alloc;

#[cfg(CONFIG_RUST_ALLOC)]
use alloc::boxed::Box;
use core::{cell::UnsafeCell, ffi::{c_int, c_void}, mem};
use core::{cell::UnsafeCell, ffi::{c_int, c_void, CStr}, mem};

use zephyr_sys::{
k_tid_t,
k_thread,
k_thread_entry_t,
k_thread_create,
k_thread_name_set,
z_thread_stack_element,
ZR_STACK_ALIGN,
ZR_STACK_RESERVED,
Expand Down Expand Up @@ -179,6 +181,8 @@ pub struct Thread {
priority: c_int,
/// Options given to thread creation.
options: u32,
/// The name to be given to the thread, if desired.
name: Option<&'static CStr>,
}

/// A statically defined thread.
Expand All @@ -195,6 +199,7 @@ impl Wrapped for StaticKernelObject<k_thread> {
stack,
priority: 0,
options: 0,
name: None,
}
}
}
Expand All @@ -210,15 +215,23 @@ impl Thread {
self.options = options;
}

/// Set a name for this thread.
///
/// Attempts to set the name of this thread, if Zephyr if configured to do so. Has no effect
/// otherwise.
pub fn set_name(&mut self, name: &'static CStr) {
self.name = Some(name);
}

/// Simple thread spawn. This is unsafe because of the raw values being used. This can be
/// useful in systems without an allocator defined.
pub unsafe fn simple_spawn(self,
pub unsafe fn simple_spawn(mut self,
child: k_thread_entry_t,
p1: *mut c_void,
p2: *mut c_void,
p3: *mut c_void)
{
k_thread_create(
let tid = k_thread_create(
self.raw,
self.stack.base,
self.stack.size,
Expand All @@ -229,21 +242,23 @@ impl Thread {
self.priority,
self.options,
K_NO_WAIT);

self.set_thread_name(tid);
}

#[cfg(CONFIG_RUST_ALLOC)]
/// Spawn a thread, with a closure.
///
/// This requires allocation to be able to safely pass the closure to the other thread.
pub fn spawn<F: FnOnce() + Send + 'static>(&self, child: F) {
pub fn spawn<F: FnOnce() + Send + 'static>(mut self, child: F) {
use core::ptr::null_mut;

let child: closure::Closure = Box::new(child);
let child = Box::into_raw(Box::new(closure::ThreadData {
closure: child,
}));
unsafe {
k_thread_create(
let tid = k_thread_create(
self.raw,
self.stack.base,
self.stack.size,
Expand All @@ -254,6 +269,16 @@ impl Thread {
self.priority,
self.options,
K_NO_WAIT);

self.set_thread_name(tid);
}
}

fn set_thread_name(&mut self, tid: k_tid_t) {
if let Some(name) = self.name {
unsafe {
k_thread_name_set(tid, name.as_ptr());
}
}
}
}
Expand Down Expand Up @@ -423,7 +448,7 @@ impl StaticThread {
#[cfg(CONFIG_RUST_ALLOC)]
/// Spawn a thread, running a closure. The closure will be boxed to give to the new thread.
/// The new thread runs immediately.
pub fn spawn<F: FnOnce() + Send + 'static>(&self, stack: StackToken, child: F) -> Thread {
pub fn spawn<F: FnOnce() + Send + 'static>(self, stack: StackToken, child: F) -> Thread {
let child: closure::Closure = Box::new(child);
let child = Box::into_raw(Box::new(closure::ThreadData {
closure: ManuallyDrop::new(child),
Expand Down