zephyr/lib/posix
Alexander Mihajlovic f19787bb84 posix: Fix calculation of clock base in clock_settime
Previous version calculated rt_clock_base incorrectly by subtracting
clock_gettime from the specified time. Effectively the following
formula was used.

    rt_clock_base := new_time - clock_gettime()

This is clearly incorrect when we consider what should happen if we
call clock_settime with the result of clock_gettime. It ought to be
approximately a no-op, but instead we end up zeroing the clock.

    rt_clock_base := clock_gettime() - clock_gettime() = 0

This patch fixes clock_settime by instead using k_uptime_get to
calculate rt_clock_base, like so:

    rt_clock_base := new_time - k_uptime_get()

Trying the earlier thought experiment we get:

    rt_clock_base := clock_gettime() - k_uptime_get()

Using the definition of clock_gettime this expands to:

    rt_clock_base := (rt_clock_base + k_uptime_get()) - k_uptime_get()

The two k_uptime_get() terms cancel out, leaving:

    rt_clock_base := rt_clock_base

I.e. the no-op that we expect when calling clock_settime with
the result of clock_gettime.

Note: The bug is only observable when rt_clock_base is non-zero.
So when clock_settime is called for the first time, it will appear
to work correctly since rt_clock_base is initialized to 0.

Signed-off-by: Alexander Mihajlovic <alexander.mihajlovic@endian.se>
2019-05-21 08:24:59 -04:00
..
CMakeLists.txt license: cleanup: add SPDX Apache-2.0 license identifier 2019-04-07 08:45:22 -04:00
Kconfig lib/posix: correct the meaning of CONFIG_MAX_PTHREAD_COUNT 2019-05-11 08:24:36 -04:00
clock.c posix: Fix calculation of clock base in clock_settime 2019-05-21 08:24:59 -04:00
fs.c lib: posix: fs: Fix access invalid memory 2019-03-23 09:52:51 -05:00
mqueue.c all: Add 'U' suffix when using unsigned variables 2019-03-28 17:15:58 -05:00
pthread.c lib/posix: correct the meaning of CONFIG_MAX_PTHREAD_COUNT 2019-05-11 08:24:36 -04:00
pthread_barrier.c all: Update reserved function names 2019-03-11 13:48:42 -04:00
pthread_common.c lib: posix: pthread_common: Don't depend on pthreads 2018-10-02 10:51:52 -07:00
pthread_cond.c all: Add 'U' suffix when using unsigned variables 2019-03-28 17:15:58 -05:00
pthread_key.c posix: add pthread_key and pthread_once APIs 2018-07-19 08:46:33 -04:00
pthread_mutex.c all: Add 'U' suffix when using unsigned variables 2019-03-28 17:15:58 -05:00
pthread_rwlock.c all: Add 'U' suffix when using unsigned variables 2019-03-28 17:15:58 -05:00
pthread_sched.c
semaphore.c lib: posix: Return errno code 2018-10-31 12:15:22 -04:00
sleep.c
timer.c all: Add 'U' suffix when using unsigned variables 2019-03-28 17:15:58 -05:00