kernel.h: Header hygine, move clock/timer handling

The kernel.h file had a bunch of internal APIs for timeout/clock
handling mixed in.  Move these to sys_clock.h, which it always
included (in a weird location, so move THAT to kernel_includes.h with
everything else).

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
This commit is contained in:
Andy Ross 2018-09-19 09:35:43 -07:00 committed by Anas Nashif
commit 0d1228af36
3 changed files with 115 additions and 116 deletions

View file

@ -16,6 +16,9 @@
#ifndef ZEPHYR_INCLUDE_SYS_CLOCK_H_
#define ZEPHYR_INCLUDE_SYS_CLOCK_H_
#include <misc/util.h>
#include <misc/dlist.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -23,11 +26,6 @@ extern "C" {
#include <toolchain.h>
#include <zephyr/types.h>
#if defined(CONFIG_SYS_CLOCK_EXISTS) && \
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0)
#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
#endif
#ifdef CONFIG_TICKLESS_KERNEL
#define sys_clock_ticks_per_sec \
(1000000 / (CONFIG_TICKLESS_KERNEL_TIME_UNIT_IN_MICRO_SECS))
@ -43,6 +41,102 @@ extern int sys_clock_hw_cycles_per_sec;
#define sys_clock_hw_cycles_per_sec CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
#endif
#if defined(CONFIG_SYS_CLOCK_EXISTS) && \
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 0)
#error "SYS_CLOCK_HW_CYCLES_PER_SEC must be non-zero!"
#endif
/* number of nsec per usec */
#define NSEC_PER_USEC 1000
/* number of microseconds per millisecond */
#define USEC_PER_MSEC 1000
/* number of milliseconds per second */
#define MSEC_PER_SEC 1000
/* number of microseconds per second */
#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC))
/* number of nanoseconds per second */
#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
/* kernel clocks */
#ifdef CONFIG_SYS_CLOCK_EXISTS
/*
* If timer frequency is known at compile time, a simple (32-bit)
* tick <-> ms conversion could be used for some combinations of
* hardware timer frequency and tick rate. Otherwise precise
* (64-bit) calculations are used.
*/
#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
#define _NEED_PRECISE_TICK_MS_CONVERSION
#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
#define _NON_OPTIMIZED_TICKS_PER_SEC
#endif
#endif
#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
defined(_NON_OPTIMIZED_TICKS_PER_SEC)
#define _NEED_PRECISE_TICK_MS_CONVERSION
#endif
#endif
static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
{
#ifdef CONFIG_SYS_CLOCK_EXISTS
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
/* use 64-bit math to keep precision */
return (s32_t)ceiling_fraction(
(s64_t)ms * sys_clock_hw_cycles_per_sec,
((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) /
sys_clock_ticks_per_sec);
#else
/* simple division keeps precision */
s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
return (s32_t)ceiling_fraction(ms, ms_per_tick);
#endif
#else
__ASSERT(ms == 0, "ms not zero");
return 0;
#endif
}
static inline s64_t __ticks_to_ms(s64_t ticks)
{
#ifdef CONFIG_SYS_CLOCK_EXISTS
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
/* use 64-bit math to keep precision */
return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec;
#else
/* simple multiplication keeps precision */
u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
return (u64_t)ticks * ms_per_tick;
#endif
#else
__ASSERT(ticks == 0, "ticks not zero");
return 0;
#endif
}
/* added tick needed to account for tick in progress */
#ifdef CONFIG_TICKLESS_KERNEL
#define _TICK_ALIGN 0
#else
#define _TICK_ALIGN 1
#endif
/*
* sys_clock_us_per_tick global variable represents a number
* of microseconds in one OS timer tick
@ -59,22 +153,6 @@ __deprecated extern int sys_clock_us_per_tick;
*/
extern int sys_clock_hw_cycles_per_tick;
/* number of nsec per usec */
#define NSEC_PER_USEC 1000
/* number of microseconds per millisecond */
#define USEC_PER_MSEC 1000
/* number of milliseconds per second */
#define MSEC_PER_SEC 1000
/* number of microseconds per second */
#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC))
/* number of nanoseconds per second */
#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC))
/* SYS_CLOCK_HW_CYCLES_TO_NS64 converts CPU clock cycles to nanoseconds */
#define SYS_CLOCK_HW_CYCLES_TO_NS64(X) \
(((u64_t)(X) * NSEC_PER_SEC) / sys_clock_hw_cycles_per_sec)
@ -110,6 +188,21 @@ extern int sys_clock_hw_cycles_per_tick;
extern volatile u64_t _sys_clock_tick_count;
/* timeouts */
struct _timeout;
typedef void (*_timeout_func_t)(struct _timeout *t);
struct _timeout {
sys_dnode_t node;
struct k_thread *thread;
sys_dlist_t *wait_q;
s32_t delta_ticks_from_prev;
_timeout_func_t func;
};
extern s32_t _timeout_remaining_get(struct _timeout *timeout);
/*
* Number of ticks for x seconds. NOTE: With MSEC() or USEC(),
* since it does an integer division, x must be greater or equal to