Skip to content

Commit 0a78a5e

Browse files
committed
zephyr-macros: Incorporate fn name into thread name
To help with debugging, try to give created Zephyr threads a readable name. Currently, this is based off of the name of the function used in the declaration of the thread. Signed-off-by: David Brown <[email protected]>
1 parent a32a8e7 commit 0a78a5e

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

zephyr-macros/src/task.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Expansion of `#[zephyr::task(...)]`.
22
3-
use std::fmt::Display;
3+
use std::{ffi::CString, fmt::Display};
44

55
use darling::FromMeta;
66
use darling::export::NestedMeta;
7-
use proc_macro2::{Span, TokenStream};
7+
use proc_macro2::{Literal, Span, TokenStream};
88
use quote::{ToTokens, format_ident, quote};
99
use syn::{
1010
Expr, ExprLit, ItemFn, Lit, LitInt, ReturnType, Type,
@@ -149,6 +149,8 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
149149
});
150150
}
151151

152+
let thread_name = Literal::c_string(&CString::new(thread_ident.to_string()).unwrap());
153+
152154
let mut thread_outer_body = quote! {
153155
const _ZEPHYR_INTERNAL_STACK_SIZE: usize = zephyr::thread::stack_len(#stack_size);
154156
const _ZEPHYR_INTERNAL_POOL_SIZE: usize = #pool_size;
@@ -187,6 +189,7 @@ pub fn run(args: TokenStream, item: TokenStream) -> TokenStream {
187189
_ZephyrInternalArgs { #(#inner_args)* },
188190
Some(startup),
189191
0,
192+
#thread_name,
190193
)
191194
};
192195

zephyr/src/thread.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
1111
use core::{
1212
cell::UnsafeCell,
13-
ffi::{c_int, c_void},
13+
ffi::{c_int, c_void, CStr},
1414
mem,
1515
ptr::null_mut,
1616
sync::atomic::Ordering,
1717
};
1818

1919
use portable_atomic::AtomicU8;
2020
use zephyr_sys::{
21-
k_thread, k_thread_create, k_thread_entry_t, k_thread_join, k_thread_priority_set, k_wakeup,
22-
z_thread_stack_element, ZR_STACK_ALIGN, ZR_STACK_RESERVED,
21+
k_thread, k_thread_create, k_thread_entry_t, k_thread_join, k_thread_name_set,
22+
k_thread_priority_set, k_wakeup, z_thread_stack_element, ZR_STACK_ALIGN, ZR_STACK_RESERVED,
2323
};
2424

2525
use crate::{
@@ -133,6 +133,7 @@ impl<T: Send> ThreadData<T> {
133133
args: T,
134134
entry: k_thread_entry_t,
135135
priority: c_int,
136+
name: &'static CStr,
136137
) -> ReadyThread {
137138
let id = Self::find_thread(pool);
138139

@@ -157,6 +158,11 @@ impl<T: Send> ThreadData<T> {
157158
)
158159
};
159160

161+
// Set the name.
162+
unsafe {
163+
k_thread_name_set(tid, name.as_ptr());
164+
}
165+
160166
ReadyThread { id: tid }
161167
}
162168

0 commit comments

Comments
 (0)