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

Threads are not preempted? #142

Open
m42uko opened this issue Dec 15, 2024 · 0 comments
Open

Threads are not preempted? #142

m42uko opened this issue Dec 15, 2024 · 0 comments

Comments

@m42uko
Copy link
Contributor

m42uko commented Dec 15, 2024

This is another one where I'm not sure whether this is a problem with SERV, or whether I'm not understanding something fundamental about Zephyr, but my threads don't get preempted, only cooperative multithreading is possible.

I modified the hello_world example to have the following main.c:

#include <stdio.h>
#include <zephyr/kernel.h>

#define PRIORITY 7
#define STACKSIZE (256)

int main(void)
{
	while (1) {
		printf("Hello World! %s\n", CONFIG_BOARD_TARGET);
		k_msleep(1);
	}
	return 0;
}

int test_thread(void)
{
	while (1) {
		printf("Test thread\n");
		k_msleep(1);
	}
	return 0;
}

K_THREAD_DEFINE(test_thread_id, STACKSIZE, test_thread, NULL, NULL, NULL,
		PRIORITY, 0, 0);

When I run this, everything looks fine:

(.venv) user@3ce4f3685fd5:~/projects/fpga/serv_mainline/0TESTING (feature/zephyr-4.0.0)$ fusesoc run --target=verilator_tb servant --uart_baudrate=57600 --firmware=zephyr/samples/hello_world/hello.hex --memsize=65535
INFO: Preparing ::serv:1.3.0
INFO: Preparing ::servile:1.3.0
INFO: Preparing ::servant:1.3.0
make: Nothing to be done for 'all'.
Loading RAM from /home/user/projects/fpga/serv_mainline/0TESTING/zephyr/samples/hello_world/hello.hex
*** Booting Zephyr OS build v4.0.0 ***
Hello World! service
Test thread
Hello World! service
Test thread
Hello World! service
Test thread
Hello World! service
Test thread
Hello World! service
Test thread
Hello World! service
Test thread

But when I go ahead now and comment out the k_msleep() in one of the processes, the other one is never scheduled again.

With msleep commented out in main:

(.venv) user@3ce4f3685fd5:~/projects/fpga/serv_mainline/0TESTING (feature/zephyr-4.0.0)$ fusesoc run --target=verilator_tb servant --uart_baudrate=57600 --firmware=zephyr/samples/hello_world/hello.hex --memsize=65535
INFO: Preparing ::serv:1.3.0
INFO: Preparing ::servile:1.3.0
INFO: Preparing ::servant:1.3.0
make: Nothing to be done for 'all'.
Loading RAM from /home/user/projects/fpga/serv_mainline/0TESTING/zephyr/samples/hello_world/hello.hex
*** Booting Zephyr OS build v4.0.0 ***
Hello World! service
Hello World! service
Hello World! service
Hello World! service
Hello World! service
Hello World! service
Hello World! service
Hello World! service

With msleep comment out in task:

(.venv) user@3ce4f3685fd5:~/projects/fpga/serv_mainline/0TESTING (feature/zephyr-4.0.0)$ fusesoc run --target=verilator_tb servant --uart_baudrate=57600 --firmware=zephyr/samples/hello_world/hello.hex --memsize=65535
INFO: Preparing ::serv:1.3.0
INFO: Preparing ::servile:1.3.0
INFO: Preparing ::servant:1.3.0
make: Nothing to be done for 'all'.
Loading RAM from /home/user/projects/fpga/serv_mainline/0TESTING/zephyr/samples/hello_world/hello.hex
*** Booting Zephyr OS build v4.0.0 ***
Hello World! service
Test thread
Test thread
Test thread
Test thread
Test thread
Test thread
Test thread
Test thread
Test thread

I checked that PREEMPT_ENABLED=1 is selected in the config. Everything looks good there as far as I can tell. Also, removing the printf along with the msleep (just a silent loop) shows that the other thread is never scheduled (just to make sure that the many prints don't hide the less often scheduled print.)

Maybe you have some minutes to look into this as well. Thanks!

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