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

Thread name is not set #143

Open
elwerene opened this issue Sep 27, 2022 · 3 comments
Open

Thread name is not set #143

elwerene opened this issue Sep 27, 2022 · 3 comments

Comments

@elwerene
Copy link

I tried this code:

thread::Builder::new().name("thread1".to_string()).spawn(move || {
    println!("Hello, world!");
});

I expected to see this happen: The thread/freertos task gets names thread1

Instead, this happened: The thread/freertos task gets names pthread

My guess of a reason

There's not set_name implemented for espidf:

https://github.com/esp-rs/rust/blob/esp-1.64.0.0/library/std/src/sys/unix/thread.rs#L205

So the pthread name is not set and it defaults to pthread:

https://github.com/espressif/esp-idf/blob/master/components/pthread/pthread.c#L237

@elwerene
Copy link
Author

elwerene commented Sep 28, 2022

I dug a bit more into it and I found that the thread is created in Thread::new and the name is afterwards set in Thread::set_name. But in the espidf libc, the name must be set prior to creating a new thread/task.

This works:

let thread_name = CString::new("thread1").unwrap();
let mut esp_thread_cfg = esp_idf_sys::esp_pthread_get_default_config();
esp_thread_cfg.thread_name = thread_name.as_ptr();
esp_idf_sys::esp_pthread_set_cfg(&esp_thread_cfg);

thread::Builder::new().name("thread1".to_string()).spawn(move || {
    println!("Hello, world!");
});

@elwerene
Copy link
Author

Maybe there's some function to set a task name in freertos after it was created?

@elwerene
Copy link
Author

I made this little helper function which I'm using in own of my projects until this issue is resolved:

pub fn spawn<F, T>(
    name: &'static str,
    stack_size: usize,
    f: F,
) -> std::io::Result<std::thread::JoinHandle<T>>
where
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static,
{
    let thread_name = std::ffi::CString::new(name).unwrap();
    let mut esp_thread_cfg = unsafe { esp_idf_sys::esp_pthread_get_default_config() };
    esp_thread_cfg.thread_name = thread_name.into_raw();
    unsafe { esp_idf_sys::esp_pthread_set_cfg(&esp_thread_cfg) };
    let thread_builder = std::thread::Builder::new().stack_size(stack_size);
    thread_builder.spawn(f)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant