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:
/* * 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:
/** * @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
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.
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.
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.