Hello, Diego Nieto Cid, le mar. 17 déc. 2024 23:02:01 -0300, a ecrit: > El mar, 17 dic 2024 a las 22:21, Zhaoming Luo (<zhming...@163.com>) escribió: > > > > The precision of this implmentation is 10ms. Not sure how to do with the > > possible data race. > > > > Signed-off-by: Zhaoming Luo <zhming...@163.com> > > --- > > include/mach/mach_host.defs | 7 +++++++ > > kern/mach_clock.c | 20 ++++++++++++++++++++ > > 2 files changed, 27 insertions(+) > > > > diff --git a/include/mach/mach_host.defs b/include/mach/mach_host.defs > > index 8fd9d6b3..67f72cda 100644 > > --- a/include/mach/mach_host.defs > > +++ b/include/mach/mach_host.defs > > @@ -386,3 +386,10 @@ routine host_adjust_time64( > > routine host_get_kernel_version( > > host : host_t; > > out kernel_version : new_kernel_version_t); > > + > > +/* > > + * Get the elapsed time on this host. > > + */ > > +routine host_get_elapsed_time( > > + host : host_t; > > + out elapsed_time : time_value_t); > > If I'm not mistaken, this is most commonly called `uptime` (like in > `host_get_uptime` or similar) > > > + > > + read_elapsed_ticks = elapsed_ticks; > > + elapsed_usec = read_elapsed_ticks * tick; > > + elapsed_time->seconds = elapsed_usec / MICROSECONDS_IN_ONE_SECOND; > > + elapsed_time->microseconds = elapsed_usec % > > MICROSECONDS_IN_ONE_SECOND; > > `read_elapsed_ticks` is only used to calculate elapsed_usec and thus > may be inlined, removing the otherwise unused variable > > As for the data races, I believe you can use atomic builtins[1], like below: > > elapsed_usec = __sync_fetch_and_add(&elapsed_tikcs, 0) * tick > > Or you could also use the lock used to increment `elapsed_ticks`: > > s = simple_lock_irq(&timer_lock); > elapsed_usec = elapsed_tikcs * tick; > simple_unlock_irq(s, &timer_lock); > > I'm not sure which works best.
The former will be way better: it'll just introduce memory barriers to make sure to get a coherent value, and read it only once (otherwise the compiler would be allowed to read it several times, for filling elapsed_time->seconds and elapsed_time->microseconds). Samuel