Skip to content

Commit 6cea302

Browse files
committed
zephyr: Allow setting of thread name
Add the ability to set the thread name. This function will allocate space for the name, leak it, and then set it within Zephyr. Signed-off-by: David Brown <[email protected]>
1 parent c63dee0 commit 6cea302

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

zephyr/src/sys/thread.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ extern crate alloc;
4040

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

4545
use zephyr_sys::{
46+
k_tid_t,
4647
k_thread,
4748
k_thread_entry_t,
4849
k_thread_create,
50+
k_thread_name_set,
4951
z_thread_stack_element,
5052
ZR_STACK_ALIGN,
5153
ZR_STACK_RESERVED,
@@ -179,6 +181,8 @@ pub struct Thread {
179181
priority: c_int,
180182
/// Options given to thread creation.
181183
options: u32,
184+
/// The name to be given to the thread, if desired.
185+
name: Option<&'static CStr>,
182186
}
183187

184188
/// A statically defined thread.
@@ -195,6 +199,7 @@ impl Wrapped for StaticKernelObject<k_thread> {
195199
stack,
196200
priority: 0,
197201
options: 0,
202+
name: None,
198203
}
199204
}
200205
}
@@ -210,15 +215,23 @@ impl Thread {
210215
self.options = options;
211216
}
212217

218+
/// Set a name for this thread.
219+
///
220+
/// Attempts to set the name of this thread, if Zephyr if configured to do so. Has no effect
221+
/// otherwise.
222+
pub fn set_name(&mut self, name: &'static CStr) {
223+
self.name = Some(name);
224+
}
225+
213226
/// Simple thread spawn. This is unsafe because of the raw values being used. This can be
214227
/// useful in systems without an allocator defined.
215-
pub unsafe fn simple_spawn(self,
228+
pub unsafe fn simple_spawn(mut self,
216229
child: k_thread_entry_t,
217230
p1: *mut c_void,
218231
p2: *mut c_void,
219232
p3: *mut c_void)
220233
{
221-
k_thread_create(
234+
let tid = k_thread_create(
222235
self.raw,
223236
self.stack.base,
224237
self.stack.size,
@@ -229,21 +242,23 @@ impl Thread {
229242
self.priority,
230243
self.options,
231244
K_NO_WAIT);
245+
246+
self.set_thread_name(tid);
232247
}
233248

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

241256
let child: closure::Closure = Box::new(child);
242257
let child = Box::into_raw(Box::new(closure::ThreadData {
243258
closure: child,
244259
}));
245260
unsafe {
246-
k_thread_create(
261+
let tid = k_thread_create(
247262
self.raw,
248263
self.stack.base,
249264
self.stack.size,
@@ -254,6 +269,16 @@ impl Thread {
254269
self.priority,
255270
self.options,
256271
K_NO_WAIT);
272+
273+
self.set_thread_name(tid);
274+
}
275+
}
276+
277+
fn set_thread_name(&mut self, tid: k_tid_t) {
278+
if let Some(name) = self.name {
279+
unsafe {
280+
k_thread_name_set(tid, name.as_ptr());
281+
}
257282
}
258283
}
259284
}

0 commit comments

Comments
 (0)