Stop and start again chibios_rt::BaseStaticThread

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

Dron_Gus
Posts: 8
Joined: Wed Apr 22, 2020 1:03 am
Has thanked: 1 time
Been thanked: 3 times

Stop and start again chibios_rt::BaseStaticThread

Postby Dron_Gus » Mon Mar 11, 2024 5:32 pm

Hi
I'm working with ChibiOS 20.3
I have static thread based on chibios_rt::BaseStaticThread.
Code in this thread checks for chThdShouldTerminateX() on each iteration.
If chThdShouldTerminateX() == true it calls chThdExit((msg_t)0x0).
Now I want to stop this thread, apply new config and start it again.
I have chibios_rt::ThreadReference ref for this thread.
So I'm doing:

Code: Select all

         /* Getting a reference.*/
         ref.addRef();

         /* Asking for thread termination.*/
         ref.requestTerminate();

           /* Waiting for termination, releasing the reference.*/
         ref.wait();

After this I see this thread in FINAL state:

Code: Select all

  14   Thread 536891672 "MAX3185X" (Name: MAX3185X, State: FINAL)                          0x080072b6 in chSchGoSleepS (newstate=<optimized out>) at ChibiOS/os/rt/src/chschd.c:235

Looks ok so far.
But when I try to start this thread again chThdCreateStatic fails on following assertion:

Code: Select all

#if (CH_CFG_USE_REGISTRY == TRUE) &&                                        \
    ((CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE))
  chDbgAssert(chRegFindThreadByWorkingArea(wsp) == NULL,
              "working area in use");
#endif

This is because thread is still in list. Comment to chRegFindThreadByWorkingArea function says:
Confirms that a working area is being used by some active thread.

Is thread with CH_STATE_FINAL state also considered active?
Am I missing some step after killing thread to remove it from registry?

If I just comment this assert in chThdCreateStatic then thread seems starts fine, but registry is corrupted, at least GDB shows only one task:

Code: Select all

(gdb) info thread
  Id   Target Id                                                              Frame
* 16   Thread 1 "Current Execution" (Name: Current Execution, No RTOS thread) Vector108 () at ChibiOS/os/hal/ports/STM32/LLD/TIMv1/hal_pwm_lld.c:310


Am I missing something trivial?

Dron_Gus
Posts: 8
Joined: Wed Apr 22, 2020 1:03 am
Has thanked: 1 time
Been thanked: 3 times

Re: Stop and start again chibios_rt::BaseStaticThread

Postby Dron_Gus » Mon Mar 11, 2024 5:41 pm

Ok, it seems there is no need to take ref (addRef()) before calling requestTerminate() and wait()

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

Re: Stop and start again chibios_rt::BaseStaticThread

Postby Giovanni » Mon Mar 11, 2024 8:42 pm

Hi,

You don't need addref if you already own a reference. By incrementing the reference counter you prevent the thread to be disposed.

Giovanni

Dron_Gus
Posts: 8
Joined: Wed Apr 22, 2020 1:03 am
Has thanked: 1 time
Been thanked: 3 times

Re: Stop and start again chibios_rt::BaseStaticThread

Postby Dron_Gus » Tue Mar 12, 2024 5:26 pm

Giovanni wrote:You don't need addref if you already own a reference. By incrementing the reference counter you prevent the thread to be disposed.

Thank you for clarifying. I had wrong assumption that ref is decremented when thread exits.
Now without addRef everything works as expected.


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 11 guests