2.6.x chTimeNow() safe without system lock?

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

2.6.x chTimeNow() safe without system lock?

Post by lilvinz »

Hi Giovanni,

chTimeNow() is a marco returning vtlist.vt_systime.

https://github.com/ChibiOS/ChibiOS/blob ... hvt.h#L216

In its doxygen header it is marked as @api which means it can be used from outside of a system lock zone.

vtlist.vt_systime is incremented here:

https://github.com/ChibiOS/ChibiOS/blob ... hvt.h#L140

But there are architectures where the variable access is not an atomic operation like e.g. AVR.
systime_t of AVR port is uint16_t but AVR registers width is 8 bits and thus more than one instruction is required to read vtlist.vt_systime from memory.
As i understand it, this means chTimeNow() could return a value which is in the middle of being incremented.

https://github.com/ChibiOS/ChibiOS/blob ... ypes.h#L53

Shouldn't there be chTimeNowI() and shouldn't chTimeNow() access vtlist.vt_systime from within a system lock
- at least if access to vtlist.vt_systime is not guaranteed to be atomic?

Thanks for clarification and best regards

Vinz
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: 2.6.x chTimeNow() safe without system lock?

Post by Giovanni »

Hi,

It has been modified in 3.0 because of this, on 8bits architectures it is safer to call it from a critical zone.

Code: Select all

/**
 * @brief   Current system time.
 * @details Returns the number of system ticks since the @p chSysInit()
 *          invocation.
 * @note    The counter can reach its maximum and then restart from zero.
 * @note    This function can be called from any context but its atomicity
 *          is not guaranteed on architectures whose word size is less than
 *          @p systime_t size.
 *
 * @return              The system time in ticks.
 *
 * @xclass
 */
static inline systime_t chVTGetSystemTimeX(void) {

#if CH_CFG_ST_TIMEDELTA == 0
  return ch.vtlist.vt_systime;
#else /* CH_CFG_ST_TIMEDELTA > 0 */
  return port_timer_get_time();
#endif /* CH_CFG_ST_TIMEDELTA > 0 */
}

/**
 * @brief   Current system time.
 * @details Returns the number of system ticks since the @p chSysInit()
 *          invocation.
 * @note    The counter can reach its maximum and then restart from zero.
 *
 * @return              The system time in ticks.
 *
 * @api
 */
static inline systime_t chVTGetSystemTime(void) {
  systime_t systime;

  chSysLock();
  systime = chVTGetSystemTimeX();
  chSysUnlock();

  return systime;
}


You can assume it to be an "x-class" function and be forward-compatible.

Giovanni
User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

Re: 2.6.x chTimeNow() safe without system lock?

Post by lilvinz »

Hi Giovanni,

thanks for the clarification!

Cheers

Vinz
Post Reply