Thread function starting in locked state

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

Thread function starting in locked state

Post by jschall »

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: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Thread function starting in locked state

Post by Giovanni »

Hi,

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

Giovanni
jschall
Posts: 33
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times
Been thanked: 1 time

Re: Thread function starting in locked state

Post by jschall »

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: 33
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times
Been thanked: 1 time

Re: Thread function starting in locked state

Post by jschall »

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: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Thread function starting in locked state

Post by Giovanni »

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: 33
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times
Been thanked: 1 time

Re: Thread function starting in locked state

Post by jschall »

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: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Thread function starting in locked state

Post by Giovanni »

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

Giovanni
jschall
Posts: 33
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times
Been thanked: 1 time

Re: Thread function starting in locked state

Post by jschall »

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: 33
Joined: Wed Sep 06, 2017 4:29 am
Has thanked: 2 times
Been thanked: 1 time

Re: Thread function starting in locked state

Post by jschall »

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: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Thread function starting in locked state

Post by Giovanni »

Hi,

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

Giovanni
Post Reply