Multi-threading not working

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Arjontje220
Posts: 6
Joined: Tue Feb 25, 2020 8:28 am

Multi-threading not working

Postby Arjontje220 » Tue Feb 25, 2020 8:55 am

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/os

Code: 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

Arjontje220
Posts: 6
Joined: Tue Feb 25, 2020 8:28 am

Re: Multi-threading not working

Postby Arjontje220 » Tue Feb 25, 2020 11:00 am

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

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Multi-threading not working

Postby Giovanni » Tue Feb 25, 2020 1:55 pm

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

Arjontje220
Posts: 6
Joined: Tue Feb 25, 2020 8:28 am

Re: Multi-threading not working

Postby Arjontje220 » Tue Feb 25, 2020 4:02 pm

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

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Multi-threading not working

Postby Giovanni » Tue Feb 25, 2020 5:34 pm

Are both created threads below NORMALPRIO?

Giovanni

Arjontje220
Posts: 6
Joined: Tue Feb 25, 2020 8:28 am

Re: Multi-threading not working

Postby Arjontje220 » Wed Feb 26, 2020 8:37 am

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

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Multi-threading not working

Postby Giovanni » Wed Feb 26, 2020 1:55 pm

In your main:

Code: Select all

        while(1);


This is preventing lower priority threads to run, put a chThdSleepMilliseconds() inside.

Giovanni

Arjontje220
Posts: 6
Joined: Tue Feb 25, 2020 8:28 am

Re: Multi-threading not working

Postby Arjontje220 » Wed Feb 26, 2020 2:51 pm

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

Arjontje220
Posts: 6
Joined: Tue Feb 25, 2020 8:28 am

Re: Multi-threading not working

Postby Arjontje220 » Wed Feb 26, 2020 3:10 pm

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

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Multi-threading not working

Postby Giovanni » Wed Feb 26, 2020 4:39 pm

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);
  }
}


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 8 guests