barriers: Introduce barrier operations

Introduce a new API for barrier operations starting with a general
skeleton and the implementation for barrier_data_memory_fence_full().

Select a built-in or an arch-based implementation according to new
Kconfig symbols CONFIG_BARRIER_OPERATIONS_BUILTIN and
CONFIG_BARRIER_OPERATIONS_ARCH.

The built-in implementation falls back on the compiler built-in
function using __ATOMIC_SEQ_CST as it is done for the atomic APIs
already.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
This commit is contained in:
Carlo Caione 2023-05-01 14:33:31 +02:00 committed by Anas Nashif
parent 1c095239b7
commit 74a942e673
3 changed files with 91 additions and 0 deletions

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Carlo Caione <ccaione@baylibre.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_SYS_BARRIER_H_
#define ZEPHYR_INCLUDE_SYS_BARRIER_H_
#include <zephyr/toolchain.h>
#if defined(CONFIG_BARRIER_OPERATIONS_ARCH)
/* Empty */
#elif defined(CONFIG_BARRIER_OPERATIONS_BUILTIN)
#include <zephyr/sys/barrier_builtin.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* @addtogroup barrier_apis Barrier Services APIs
* @ingroup kernel_apis
* @{
*/
/**
* @brief Full/sequentially-consistent data memory barrier.
*
* This routine acts as a synchronization fence between threads and prevents
* re-ordering of data accesses instructions across the barrier instruction.
*/
static ALWAYS_INLINE void barrier_dmem_fence_full(void)
{
#if defined(CONFIG_BARRIER_OPERATIONS_ARCH) || defined(CONFIG_BARRIER_OPERATIONS_BUILTIN)
z_barrier_dmem_fence_full();
#endif
}
/** @} */
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* ZEPHYR_INCLUDE_SYS_ATOMIC_H_ */

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 Carlo Caione <ccaione@baylibre.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_SYS_BARRIER_BUILTIN_H_
#define ZEPHYR_INCLUDE_SYS_BARRIER_BUILTIN_H_
#ifndef ZEPHYR_INCLUDE_SYS_BARRIER_H_
#error Please include <zephyr/sys/barrier.h>
#endif
#include <zephyr/toolchain.h>
#ifdef __cplusplus
extern "C" {
#endif
static ALWAYS_INLINE void z_barrier_dmem_fence_full(void)
{
__atomic_thread_fence(__ATOMIC_SEQ_CST);
}
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_SYS_BARRIER_BUILTIN_H_ */

View file

@ -452,6 +452,21 @@ config SYSTEM_WORKQUEUE_NO_YIELD
endmenu
menu "Barrier Operations"
config BARRIER_OPERATIONS_BUILTIN
bool
help
Use the compiler builtin functions for barrier operations. This is
the preferred method. However, support for all arches in GCC is
incomplete.
config BARRIER_OPERATIONS_ARCH
bool
help
Use when there isn't support for compiler built-ins, but you have
written optimized assembly code under arch/ which implements these.
endmenu
menu "Atomic Operations"
config ATOMIC_OPERATIONS_BUILTIN
bool