Threads K_ESSENTIAL

Hello, we have a thread that essentially is running a loop and we want that thread to be running for all the lifecycle of the app. If for whatever reason that thread died we want to restart the whole app. Reading the documentation there is this option for k_thread_create called K_ESSENTIAL which according to the documentation:

This option tags the thread as an essential thread. This instructs the kernel to treat the termination or aborting of the thread as a fatal system error.

Our assumption is that if the thread ends the whole system will reboot which it is what it does when a fatal error is detected otherwise (we are working on a nrf9160 based board), but what we see happen is that the thread just ends but the app keeps running, please see attached example to reproduce the behavior.

Is this the expected outcome? Is there a way to signal to the system "if this thread dies, restart the app"?

Thank you

Xavier

#include <zephyr.h>
#include <sys/printk.h>


#define LOOP2_STACK_SIZE 1024
#define LOOP2_PRIORITY 1

struct k_thread loop2_thread;
K_THREAD_STACK_DEFINE(loop2_area, LOOP2_STACK_SIZE);


void loop2(void *one, void *two, void *three) {
	int i = 0;
	while (1)
	{
		printk("LOOP 2\n");
		i++;
		if (i > 3)
		{
			printk("LOOP 2 EXITING\n");
			return;
		}	
		k_sleep(K_SECONDS(1));
	}
	
}


void main(void)
{
	printk("Hello World! %s\n", CONFIG_BOARD);

    k_tid_t loop2_thread_id = k_thread_create(&loop2_thread, loop2_area,
                                                   K_THREAD_STACK_SIZEOF(loop2_area),
                                                   loop2,
                                                   NULL, NULL, NULL,
                                                   LOOP2_PRIORITY, K_ESSENTIAL, K_NO_WAIT);

	while (1) {
		printk("LOOP 1\n");
		k_sleep(K_SECONDS(2));
	}
	
}

Parents Reply Children
Related