timing: do not repeatedly do init()/start()/stop()

We should not be initializing/starting/stoping timing functions
multiple times. So this changes how the timing functions are
structured to allow only one initialization, only start when
stopped, and only stop when started.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2020-10-14 11:15:40 -07:00 committed by Anas Nashif
commit 9be37553ee
13 changed files with 353 additions and 88 deletions

View file

@ -0,0 +1,6 @@
# Copyright (c) 2020 Intel Corporation.
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources(timing.c)

60
subsys/timing/timing.c Normal file
View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>
#include <kernel.h>
#include <sys/atomic.h>
#include <timing/timing.h>
static bool has_inited;
static atomic_val_t started_ref;
void timing_init(void)
{
if (has_inited) {
return;
}
#if defined(CONFIG_BOARD_HAS_TIMING_FUNCTIONS)
board_timing_init();
#elif defined(CONFIG_SOC_HAS_TIMING_FUNCTIONS)
soc_timing_init();
#else
arch_timing_init();
#endif
has_inited = true;
}
void timing_start(void)
{
if (atomic_inc(&started_ref) != 0) {
return;
}
#if defined(CONFIG_BOARD_HAS_TIMING_FUNCTIONS)
board_timing_start();
#elif defined(CONFIG_SOC_HAS_TIMING_FUNCTIONS)
soc_timing_start();
#else
arch_timing_start();
#endif
}
void timing_stop(void)
{
if (atomic_dec(&started_ref) > 1) {
return;
}
#if defined(CONFIG_BOARD_HAS_TIMING_FUNCTIONS)
board_timing_stop();
#elif defined(CONFIG_SOC_HAS_TIMING_FUNCTIONS)
soc_timing_stop();
#else
arch_timing_stop();
#endif
}