chSysLock() recursion

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

chSysLock() recursion

Post by skute »

Greetings,

I am new to ChibiOS, so please correct me if my understanding is wrong, but it seems that chSysLock() is not meant to be called recursively. This can lead to unexpected behavior; consider the following simple code example:

Code: Select all

int func2data = 0;
void func2(void)
{

    chSysLock();
    func2data += 1;
    chSysUnlock();
}


int func1(void)
{
    chSysLock();
    func2();

    /*
     * At this point we expect to be in a locked context, but we aren't because
     * chSysUnlock() from above has unlocked the system.
     */
    func3();
    ...
    chSysUnlock();

    return 0;
}


Is there an option to enable chSysLock() to be recursive? For example:

Code: Select all

int func2data = 0;
void func2(void)
{
    chCpuState_t savedState;

    chSysLock(&savedState);
    func2data += 1;
    chSysUnlock(&savedState);
}


int func1(void)
{
    chCpuState_t savedState;

    chSysLock(&savedState);
    func2();
    func3();
    ...
    chSysUnlock(&savedState);

    return 0;
}


=skute
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: chSysLock() recursion

Post by skute »

One potential solution that I've come up with is as follows:

chconf.h

Code: Select all

/**
 * @brief   Threads descriptor structure extension.
 * @details User fields added to the end of the @p Thread structure.
 */
#if !defined(THREAD_EXT_FIELDS) || defined(__DOXYGEN__)
#define THREAD_EXT_FIELDS                                                   \
  unsigned int lockStatus;                                                  \
  unsigned int lockCnt;                                                     
#endif

/**
 * @brief   Threads initialization hook.
 * @details User initialization code added to the @p chThdInit() API.
 *
 * @note    It is invoked from within @p chThdInit() and implicitly from all
 *          the threads creation APIs.
 */
#if !defined(THREAD_EXT_INIT_HOOK) || defined(__DOXYGEN__)
#define THREAD_EXT_INIT_HOOK(tp) {                                          \
  /* Add threads initialization code here.*/                                \
  tp->lockStatus = 0;                                                        \
  tp->lockCnt    = 0;                                                        \
}
#endif



chcore_v7m.h

Code: Select all

#if !defined(CORTEX_SIMPLIFIED_PRIORITY)
#define CORTEX_SIMPLIFIED_PRIORITY      TRUE
#endif



chcore_v7m.c

Code: Select all

unsigned int __attribute__((naked))
_lock(void)
{
    unsigned int primask;

    __asm("    mrs     r0, PRIMASK\n"
          "    cpsid   i\n"
          "    bx      lr\n"
          : "=r" (primask));

    // avoid compiler warning
    return primask;
}


void __attribute__((naked))
_unlock(unsigned int status)
{
    __asm("    msr     PRIMASK, r0\n"
          "    bx      lr\n");
}


void _port_lock(void) {
  Thread *tp = chThdSelf();
   
    if (0 == tp->lockCnt) {
        tp->lockStatus = _lock();
    }

    tp->lockCnt += 1;
}

void _port_unlock(void) {
    Thread *tp = chThdSelf();
   
    if (tp->lockCnt > 0) {
        tp->lockCnt -= 1;
        if (0 == tp->lockCnt) {
            _unlock(tp->lockStatus);
            tp->lockStatus = 0;
        }
    }
}



I believe this will do what I'm looking for and avoids having to define a variable everywhere you use chSysLock(). Thoughts?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chSysLock() recursion

Post by Giovanni »

Hi,

ChSysLock and chSysUnlock are meant to not be nest-able in order to make them as fast as possible. The whole OS code is designed around not needing nesting and, in general, you can reorganize any code to do the same.

In version 2.2.x there was an option to make them nest-able but it has since removed, you may use that version (still supported) or paste that implementation into the current version. See chsys.h, the implementation requires an extra field in each Thread structure, you can add it using the hook macro THREAD_EXT_FIELDS. It is very similar to what you suggested.

Giovanni
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: chSysLock() recursion

Post by mabl »

skute,

there was once an option for recursive syslocks which has been removed since some time now. There is a better solution to you problem:
Always think about from where your function is called. If your function is also called from a locked zone, suffix it with I: i.e.

Code: Select all


fooI() {
 // Do something
}

foo() {
chSysLock();
fooI();
chSysUnlock();
}



You might want to have a look at some ChibiOS functions, they do it exactly this way.

EDIT: Rule: Giovanni is ALWAYS faster.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chSysLock() recursion

Post by Giovanni »

Mabl and I often trigger a race condition in the forum code :-)

What he suggested is what we usually do in order to not depend on recursive locks.

Giovanni
skute
Posts: 64
Joined: Wed Aug 29, 2012 10:17 pm

Re: chSysLock() recursion

Post by skute »

Greetings,

Thanks for the reply's. The 2.2.x branch looks like a simple solution for the short term. I'll add an assert in the nesting case and to track down any offending code which and re-write it to be non-nesting.

Cheers!
Post Reply