Implement atomic_time_stat_t for lockless timekeeping
commitb2ec5cfd5e00eac7963f5a84fb71f002ad79f1f0
authorJiří Zárevúcky <zarevucky.jiri@gmail.com>
Sat, 15 Apr 2023 11:54:58 +0000 (15 13:54 +0200)
committerJiří Zárevúcky <zarevucky.jiri@gmail.com>
Sat, 15 Apr 2023 16:47:54 +0000 (15 18:47 +0200)
tree13e3691ea2646a7ff6059aa178432c40d820300e
parentdd218ea076a20828a183015319cffbd31d1769de
Implement atomic_time_stat_t for lockless timekeeping

We keep monotonically increasing temporal statistics in several places.
They are frequently written from the thread that owns them, and rarely
read from other threads in certain syscalls. This new code serves the
purpose of avoiding the need for synchronization on the writer side.
On 64b system, we can simply assume that 64b writes are indivisible,
and relaxed atomic read/writes simply serve to formally prevent C
undefined behavior from data races (they translate to regular memory
reads/writes in assembly).

On 32b systems, we use the same algorithm that's been used for userspace
clock access, using three fields and some memory barriers to maintain
consistency of reads when the upper half changes. Only readers always
synchronize though. For writers, barriers are avoided in the common case
when the upper half remains unchanged.
kernel/generic/include/atomic.h
kernel/generic/include/cpu.h
kernel/generic/src/cpu/cpu.c
kernel/generic/src/interrupt/interrupt.c
kernel/generic/src/sysinfo/stats.c
kernel/generic/src/time/clock.c