Page 1 of 1
Multi-threading not working
Posted: Tue Feb 25, 2020 8:55 am
by Arjontje220
Hi,
I recently discovered ChibiOS/RT and so far I like it, but there is one problem. If I want to run the code down below, it forever stays in the while loop of thread 1 and never executes the code of thread 2. Am I doing something wrong? I am using a ARM Cortex-M33
I am using this code:
https://gitlab.soft.vub.ac.be/yvdriess/ ... chibiOS/osCode: Select all
#include "ch.h"
int a = 0;
#define THREADS_STACK_SIZE 128
#define WA_SIZE THD_WA_SIZE(THREADS_STACK_SIZE)
static WORKING_AREA(waThread1, THREADS_STACK_SIZE);
Thread* t1;
static msg_t Thread1(void *p) {
while(1){ <<<<<<<<<<<<<<<<<<<<it stays forever in this while-loop
a++;
if(a > 100) a = 0;
}
chThdExit(0);
return 0;
}
static WORKING_AREA(waThread2, THREADS_STACK_SIZE);
Thread* t2;
static msg_t Thread2(void *p) {
while(1){ <<<<<<<<<<<<<<<<<<<<it never reaches this while-loop
a--;
if(a < -100) a = 0;
}
chThdExit(0);
return 0;
}
int main(){
chSysInit();
t1 = chThdCreateStatic(waThread1, sizeof(waThread1),
NORMALPRIO + 2, Thread1, NULL);
t2 = chThdCreateStatic(waThread2, sizeof(waThread2),
NORMALPRIO + 2, Thread2, NULL);
while(1);
return 0;
}
Thanks in advance,
Arjon
Re: Multi-threading not working
Posted: Tue Feb 25, 2020 11:00 am
by Arjontje220
So, if I add the function "chSchDoReschedule()" in each thread it works.
This is not ideal, is there a function that does this automatically? For instance, FreeRTOS has "vTaskStartScheduler()" to do this, is there a similar function available for ChibiOS?
Thanks,
Arjon
Re: Multi-threading not working
Posted: Tue Feb 25, 2020 1:55 pm
by Giovanni
Depending on how you assign priorities and the chosen options this is normal.
The default settings are:
- Tick-less mode enabled.
- Preemptive round-robin disabled.
RR preemption is not compatible with tick-less mode. If you need RR-preemption then change in chaconf.h:
Code: Select all
#define CH_CFG_ST_FREQUENCY 10000 -> 1000 (10000 is too high without tick-less mode)
#define CH_CFG_ST_TIMEDELTA 2 -> 0 (disables tick-less mode)
#define CH_CFG_TIME_QUANTUM 0 ->> 10 (rotation after 10 ticks)
You could keep the collaborative-RR but the correct function to call from within the loop is chThdYield().
Note that placing RR thread at higher priority than any other thread is a bad idea, in your code main() would no more be executed regardless the settings because it has priority NORMALPRIO, it would never launch the 2nd thread after starting the 1st one. Place RR threads below NORMALPRIO.
Remember, the rule is (from all common RTOSes), the task in execution is the highest priority one among those eligible for execution (ready ones).
Giovanni
Re: Multi-threading not working
Posted: Tue Feb 25, 2020 4:02 pm
by Arjontje220
Hi Giovanni,
Thank you for your response.
These are my settings (for RR-preemption):
Code: Select all
#define CH_CFG_ST_FREQUENCY 1000
#define CH_CFG_ST_TIMEDELTA 0
#define CH_CFG_TIME_QUANTUM 20
I understand what you are saying about priorities, but for some reason, it doesn't matter what priority I give the threads (so greater/lower than NORMALPRIO) or if I create thread 2 inside of thread 1, it doesn't work. It only executes threat 1. Can you somehow explain this or am I still missing something?
Thanks in advance,
Arjon
Re: Multi-threading not working
Posted: Tue Feb 25, 2020 5:34 pm
by Giovanni
Are both created threads below NORMALPRIO?
Giovanni
Re: Multi-threading not working
Posted: Wed Feb 26, 2020 8:37 am
by Arjontje220
Hi Giovanni,
Thank you for your quick response.
So to sum up, there are two different cases:
Case 1: both threads are lower than NORMALPRIO -> both threads are NOT being executed, instead it is stuck when creating the second thread (see code). When I debug the code and step over that line, it doesn't give me an error or anything but it wont continue either.
Code: Select all
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *p) {
while (1) {
//code
}
chThdExit(0);
return 0;
}
static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *p) {
while (1) {
//code
}
chThdExit(0);
return 0;
}
int main( void ) {
chSysInit();
chThdCreateStatic(waThread1, sizeof(waThread1),
LOWPRIO, Thread1, NULL);
chThdCreateStatic(waThread2, sizeof(waThread2), <<<<<<< It is stuck in this line
LOWPRIO, Thread2, NULL);
while(1);
return 0;
}
Case 2: both threads are higher than NORMALPRIO -> only thread 1 gets executed.
Code: Select all
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *p) {
while (1) {
//code
}
chThdExit(0);
return 0;
}
static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *p) {
while (1) {
//code <<<<<<<< never gets executed
}
chThdExit(0);
return 0;
}
int main( void ) {
chSysInit();
chThdCreateStatic(waThread1, sizeof(waThread1),
NORMALPRIO+1, Thread1, NULL);
chThdCreateStatic(waThread2, sizeof(waThread2),
NORMALPRIO+1, Thread2, NULL);
while(1);
return 0;
}
Case 2 makes sense because thread 1 has a higher priority than the main thread, so thread 2 will never be created. So that is when I thought if I create threat 2 inside of thread 1, thread 2 will be created and executed, but this also failed. Thread 2 is created, but not being executed.
Code: Select all
static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *p) {
while (1) {
//code <<<<<<<< never gets executed
}
chThdExit(0);
return 0;
}
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *p) {
chThdCreateStatic(waThread2, sizeof(waThread2),
NORMALPRIO+1, Thread2, NULL);
while (1) {
//code
}
chThdExit(0);
return 0;
}
int main( void ) {
chSysInit();
chThdCreateStatic(waThread1, sizeof(waThread1),
NORMALPRIO+1, Thread1, NULL);
while(1);
}
Any thoughts why? Or do I need to add a timer interrupt that calls the function _port_switch_from_isr()?
Thanks in advance,
Arjon
Re: Multi-threading not working
Posted: Wed Feb 26, 2020 1:55 pm
by Giovanni
In your main:
This is preventing lower priority threads to run, put a chThdSleepMilliseconds() inside.
Giovanni
Re: Multi-threading not working
Posted: Wed Feb 26, 2020 2:51 pm
by Arjontje220
Hi,
Thread 1 is created and being executed, threat 2 is created but not being executed...
Code: Select all
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *p) {
while (1) {
//code
}
chThdExit(0);
return 0;
}
static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *p) {
while (1) {
//code <<<<<<<<<<<<<<not being executed
}
chThdExit(0);
return 0;
}
int main( void ) {
chSysInit();
chThdCreateStatic(waThread1, sizeof(waThread1),
LOWPRIO, Thread1, NULL);
chThdCreateStatic(waThread2, sizeof(waThread2),
LOWPRIO, Thread2, NULL);
chThdSleepSeconds(100);
return 0;
}
Any other suggestions?
Thanks,
Arjon
Re: Multi-threading not working
Posted: Wed Feb 26, 2020 3:10 pm
by Arjontje220
So the function chSysTimerHandlerI() never gets called, this means that the running threads quantum never gets expired so context switch never happens. Why does this function never gets called, because it should be, right?
Arjon
Re: Multi-threading not working
Posted: Wed Feb 26, 2020 4:39 pm
by Giovanni
Why are you returning from the main() now?
Please, just do like all demos do:
Code: Select all
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
/*
* Create threads.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO-1, Thread1, NULL);
chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO-1, Thread2, NULL);
/*
* Normal main() thread activity.
*/
while (true) {
chThdSleepMilliseconds(500);
}
}