Thread function starting in locked state

Discussions and support about ChibiOS/RT, the free embedded RTOS.
jschall
Posts: 18
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times

Thread function starting in locked state

Postby jschall » Fri Dec 01, 2017 9:33 pm

On STM32, I'm creating a thread like so:

Code: Select all

void worker_thread_init(/*...*/) {
    void* working_area = chCoreAllocAligned(THD_WORKING_AREA_SIZE(stack_size), PORT_WORKING_AREA_ALIGN);
    if (!working_area) {
        return;
    }
   
    /*...*/
   
    chDbgCheck(ch.dbg.lock_cnt == 0);
    worker_thread->thread = chThdCreateStatic(working_area, THD_WORKING_AREA_SIZE(stack_size), prio, worker_thread_func, worker_thread);
   
    chThdSleep(TIME_INFINITE);
}


And I've put this debug check at the top of the thread function:

Code: Select all

static THD_FUNCTION(worker_thread_func, arg) {
    chDbgCheck(ch.dbg.lock_cnt == 0);
   
    /*...*/
}


The debug check in worker_thread_func fails. The one in worker_thread_init does not.

I've banged my head against this for several hours now. Any idea what's going on?

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

Re: Thread function starting in locked state

Postby Giovanni » Fri Dec 01, 2017 9:57 pm

Hi,

Which ChibiOS version? are you using the provided makefiles in your project?

Giovanni

jschall
Posts: 18
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times

Re: Thread function starting in locked state

Postby jschall » Fri Dec 01, 2017 10:07 pm

The chibios version is 17.6.3.

The Makefile is modified from the provided Makefile, but rules.mk remains almost exactly the same.

jschall
Posts: 18
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times

Re: Thread function starting in locked state

Postby jschall » Fri Dec 01, 2017 10:14 pm

I switched to using the rules.mk provided by ChibiOS, which had a small change. The issue still occurs.

Code: Select all

-ASFLAGS   = $(MCFLAGS) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
-ASXFLAGS  = $(MCFLAGS) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)
+ASFLAGS   = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
+ASXFLAGS  = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)

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

Re: Thread function starting in locked state

Postby Giovanni » Fri Dec 01, 2017 10:19 pm

I suspect that your .S files are not recompiled when you change chconfh, that could explain the behavior because the thread trampoline code would not decrement that counter.

Code: Select all

               .thumb_func
                .globl  _port_thread_start
_port_thread_start:
#if CH_DBG_SYSTEM_STATE_CHECK
                bl      _dbg_check_unlock
#endif
#if CH_DBG_STATISTICS
                bl      _stats_stop_measure_crit_thd
#endif
#if CORTEX_SIMPLIFIED_PRIORITY
                cpsie   i
#else
                movs    r3, #0              /* CORTEX_BASEPRI_DISABLED */
                msr     BASEPRI, r3
#endif
                mov     r0, r5
                blx     r4
#if defined(_CHIBIOS_RT_CONF_)
                movs    r0, #0              /* MSG_OK */
                bl      chThdExit
#endif
#if defined(_CHIBIOS_NIL_CONF_)
                mov     r3, #0
                bl      chSysHalt
#endif


Note the call to _dbg_check_unlock(), it is within a condition, it is where the counter is decremented. This is why I think to a build issue.

Giovanni

jschall
Posts: 18
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times

Re: Thread function starting in locked state

Postby jschall » Fri Dec 01, 2017 10:22 pm

I've been doing completely clean builds - in which the build directory was deleted. I'll clean again and do a search for files that don't belong.

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

Re: Thread function starting in locked state

Postby Giovanni » Fri Dec 01, 2017 10:23 pm

Look into the generated listing if that "bl" is actually compiled.

Giovanni

jschall
Posts: 18
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times

Re: Thread function starting in locked state

Postby jschall » Fri Dec 01, 2017 10:27 pm

I think I know what it is. I was using UDEFS += -D... to enable the checks. I'll figure it out and fix it.

jschall
Posts: 18
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times

Re: Thread function starting in locked state

Postby jschall » Fri Dec 01, 2017 10:52 pm

Thanks! This was a really tough one to debug.

How about a check like this?

Code: Select all

ifneq ($(sort $(filter -DCH_DBG%,$(DEFS))),$(sort $(filter -DCH_DBG%,$(ADEFS))))
  $(error -DCH_DBG flags differ between DEFS and ADEFS)
endif

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

Re: Thread function starting in locked state

Postby Giovanni » Sat Dec 02, 2017 7:58 am

Hi,

You could add that to the makefile, I don't think that rules.mk should be "aware" of upper layers.

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 2 guests