From patchwork Wed Jul 20 20:39:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 72511 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp78653qga; Wed, 20 Jul 2016 13:50:37 -0700 (PDT) X-Received: by 10.129.137.4 with SMTP id z4mr29755792ywf.213.1469047837449; Wed, 20 Jul 2016 13:50:37 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id l55si2793635qta.11.2016.07.20.13.50.37; Wed, 20 Jul 2016 13:50:37 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 18E4B68206; Wed, 20 Jul 2016 20:50:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 5D56468912; Wed, 20 Jul 2016 20:41:20 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id C2B7E6890C; Wed, 20 Jul 2016 20:40:51 +0000 (UTC) Received: from mail-lf0-f48.google.com (mail-lf0-f48.google.com [209.85.215.48]) by lists.linaro.org (Postfix) with ESMTPS id 922B968913 for ; Wed, 20 Jul 2016 20:39:24 +0000 (UTC) Received: by mail-lf0-f48.google.com with SMTP id b199so46481643lfe.0 for ; Wed, 20 Jul 2016 13:39:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=sFDlPTlEVVtf8AGNZhqoPCcj0+kUwNujfZd8FLmarZs=; b=J1oO6uL5k0NI5EwtPhPJM2zJvRfqRE21m0GWp5u4/uc1+hkpj8LnvCD9tonjUMzS4j 8vgv6IX8RP4NrZmg2QgtNecFA/kg9Pvawh95RKCvueyAtNoQzLSzWDgzKaI+ecKCM0Gc JBIyikGkt0my62AKbBIgB2yJKAeLLFZj7s58g32B69SmIenpv/wtavvNsKXzzarQk741 bLZvAxeAd2ktYrxPQ37d3gEiA5X99oxb59EfCDd7aEP6tVz+05vkixe4CLjN6yeAlTFh 5NvXPOhYFb2IvMClcMCwEA1A4IChgep41QGnp/onD2fwM0Zc/SPRClPdmDVGN98lM3iz zB+A== X-Gm-Message-State: ALyK8tI1hgsq0s+c6CzaTNeasr6KIobn607PJezQr3firB+oQr6JzZydtIrpFN46SgyFqt+oj3s= X-Received: by 10.46.71.147 with SMTP id u141mr19354174lja.15.1469047163228; Wed, 20 Jul 2016 13:39:23 -0700 (PDT) Received: from localhost.localdomain (c-83-233-90-46.cust.bredband2.com. [83.233.90.46]) by smtp.gmail.com with ESMTPSA id 29sm895112lfu.43.2016.07.20.13.39.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Jul 2016 13:39:22 -0700 (PDT) From: Christophe Milard To: mike.holmes@linaro.org, lng-odp@lists.linaro.org, balakrishna.garapati@linaro.org Date: Wed, 20 Jul 2016 22:39:01 +0200 Message-Id: <1469047144-7960-16-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1469047144-7960-1-git-send-email-christophe.milard@linaro.org> References: <1469047144-7960-1-git-send-email-christophe.milard@linaro.org> X-Topics: patch Subject: [lng-odp] [API-NEXT PATCHv2 15/18] linux-generic: drv: adding atomic X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Based on API interface files (sha:77a27212b563299959fb2a609b86bb9117a8f918). Signed-off-by: Christophe Milard --- include/odp_drv.h | 1 + platform/linux-generic/Makefile.am | 3 + platform/linux-generic/drv_atomic.c | 26 ++ platform/linux-generic/include/odp/drv/atomic.h | 430 +++++++++++++++++++++ .../include/odp/drv/plat/atomic_types.h | 88 +++++ 5 files changed, 548 insertions(+) create mode 100644 platform/linux-generic/drv_atomic.c create mode 100644 platform/linux-generic/include/odp/drv/atomic.h create mode 100644 platform/linux-generic/include/odp/drv/plat/atomic_types.h -- 2.7.4 diff --git a/include/odp_drv.h b/include/odp_drv.h index 6d2f7ff..35e070c 100644 --- a/include/odp_drv.h +++ b/include/odp_drv.h @@ -19,6 +19,7 @@ extern C { #endif #include +#include #include #include #include diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am index 20b4b29..109b42e 100644 --- a/platform/linux-generic/Makefile.am +++ b/platform/linux-generic/Makefile.am @@ -94,6 +94,7 @@ odpapiplatinclude_HEADERS = \ odpdrvincludedir = $(includedir)/odp/drv odpdrvinclude_HEADERS = \ $(srcdir)/include/odp/drv/align.h \ + $(srcdir)/include/odp/drv/atomic.h \ $(srcdir)/include/odp/drv/byteorder.h \ $(srcdir)/include/odp/drv/compiler.h \ $(srcdir)/include/odp/drv/std_types.h \ @@ -101,6 +102,7 @@ odpdrvinclude_HEADERS = \ odpdrvplatincludedir = $(includedir)/odp/drv/plat odpdrvplatinclude_HEADERS = \ + $(srcdir)/include/odp/drv/plat/atomic_types.h \ $(srcdir)/include/odp/drv/plat/byteorder_types.h noinst_HEADERS = \ @@ -198,6 +200,7 @@ __LIB__libodp_linux_la_SOURCES = \ odp_traffic_mngr.c \ odp_version.c \ odp_weak.c \ + drv_atomic.c \ arch/@ARCH_DIR@/odp_cpu_arch.c \ arch/@ARCH_DIR@/odp_sysinfo_parse.c diff --git a/platform/linux-generic/drv_atomic.c b/platform/linux-generic/drv_atomic.c new file mode 100644 index 0000000..72c85e8 --- /dev/null +++ b/platform/linux-generic/drv_atomic.c @@ -0,0 +1,26 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +int odpdrv_atomic_lock_free_u64(odpdrv_atomic_op_t *atomic_op) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + /* All operations have locks */ + if (atomic_op) + atomic_op->all_bits = 0; + + return 0; +#else + /* All operations are lock-free */ + if (atomic_op) { + atomic_op->all_bits = ~((uint32_t)0); + atomic_op->op.init = 0; + } + + return 2; +#endif +} diff --git a/platform/linux-generic/include/odp/drv/atomic.h b/platform/linux-generic/include/odp/drv/atomic.h new file mode 100644 index 0000000..7d922da --- /dev/null +++ b/platform/linux-generic/include/odp/drv/atomic.h @@ -0,0 +1,430 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/** + * @file + * + * ODPDRV atomic operations + */ + +#ifndef ODPDRV_PLAT_ATOMIC_H_ +#define ODPDRV_PLAT_ATOMIC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +/** @ingroup odpdrv_atomic + * @{ + */ + +static inline void odpdrv_atomic_init_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + __atomic_store_n(&atom->v, val, __ATOMIC_RELAXED); +} + +static inline uint32_t odpdrv_atomic_load_u32(odpdrv_atomic_u32_t *atom) +{ + return __atomic_load_n(&atom->v, __ATOMIC_RELAXED); +} + +static inline void odpdrv_atomic_store_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + __atomic_store_n(&atom->v, val, __ATOMIC_RELAXED); +} + +static inline uint32_t odpdrv_atomic_fetch_add_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + return __atomic_fetch_add(&atom->v, val, __ATOMIC_RELAXED); +} + +static inline void odpdrv_atomic_add_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + (void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELAXED); +} + +static inline uint32_t odpdrv_atomic_fetch_sub_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + return __atomic_fetch_sub(&atom->v, val, __ATOMIC_RELAXED); +} + +static inline void odpdrv_atomic_sub_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + (void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELAXED); +} + +static inline uint32_t odpdrv_atomic_fetch_inc_u32(odpdrv_atomic_u32_t *atom) +{ + return __atomic_fetch_add(&atom->v, 1, __ATOMIC_RELAXED); +} + +static inline void odpdrv_atomic_inc_u32(odpdrv_atomic_u32_t *atom) +{ + (void)__atomic_fetch_add(&atom->v, 1, __ATOMIC_RELAXED); +} + +static inline uint32_t odpdrv_atomic_fetch_dec_u32(odpdrv_atomic_u32_t *atom) +{ + return __atomic_fetch_sub(&atom->v, 1, __ATOMIC_RELAXED); +} + +static inline void odpdrv_atomic_dec_u32(odpdrv_atomic_u32_t *atom) +{ + (void)__atomic_fetch_sub(&atom->v, 1, __ATOMIC_RELAXED); +} + +static inline int odpdrv_atomic_cas_u32(odpdrv_atomic_u32_t *atom, + uint32_t *old_val, uint32_t new_val) +{ + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_RELAXED, + __ATOMIC_RELAXED); +} + +static inline uint32_t odpdrv_atomic_xchg_u32(odpdrv_atomic_u32_t *atom, + uint32_t new_val) +{ + return __atomic_exchange_n(&atom->v, new_val, __ATOMIC_RELAXED); +} + +static inline void odpdrv_atomic_max_u32(odpdrv_atomic_u32_t *atom, + uint32_t new_max) +{ + uint32_t old_val; + + old_val = odpdrv_atomic_load_u32(atom); + + while (new_max > old_val) { + if (odpdrv_atomic_cas_u32(atom, &old_val, new_max)) + break; + } +} + +static inline void odpdrv_atomic_min_u32(odpdrv_atomic_u32_t *atom, + uint32_t new_min) +{ + uint32_t old_val; + + old_val = odpdrv_atomic_load_u32(atom); + + while (new_min < old_val) { + if (odpdrv_atomic_cas_u32(atom, &old_val, new_min)) + break; + } +} + +static inline void odpdrv_atomic_init_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ + atom->v = val; +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + __atomic_clear(&atom->lock, __ATOMIC_RELAXED); +#endif +} + +static inline uint64_t odpdrv_atomic_load_u64(odpdrv_atomic_u64_t *atom) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + return ATOMIC_OP(atom, (void)0); +#else + return __atomic_load_n(&atom->v, __ATOMIC_RELAXED); +#endif +} + +static inline void odpdrv_atomic_store_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v = val); +#else + __atomic_store_n(&atom->v, val, __ATOMIC_RELAXED); +#endif +} + +static inline uint64_t odpdrv_atomic_fetch_add_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + return ATOMIC_OP(atom, atom->v += val); +#else + return __atomic_fetch_add(&atom->v, val, __ATOMIC_RELAXED); +#endif +} + +static inline void odpdrv_atomic_add_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v += val); +#else + (void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELAXED); +#endif +} + +static inline uint64_t odpdrv_atomic_fetch_sub_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + return ATOMIC_OP(atom, atom->v -= val); +#else + return __atomic_fetch_sub(&atom->v, val, __ATOMIC_RELAXED); +#endif +} + +static inline void odpdrv_atomic_sub_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v -= val); +#else + (void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELAXED); +#endif +} + +static inline uint64_t odpdrv_atomic_fetch_inc_u64(odpdrv_atomic_u64_t *atom) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + return ATOMIC_OP(atom, atom->v++); +#else + return __atomic_fetch_add(&atom->v, 1, __ATOMIC_RELAXED); +#endif +} + +static inline void odpdrv_atomic_inc_u64(odpdrv_atomic_u64_t *atom) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v++); +#else + (void)__atomic_fetch_add(&atom->v, 1, __ATOMIC_RELAXED); +#endif +} + +static inline uint64_t odpdrv_atomic_fetch_dec_u64(odpdrv_atomic_u64_t *atom) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + return ATOMIC_OP(atom, atom->v--); +#else + return __atomic_fetch_sub(&atom->v, 1, __ATOMIC_RELAXED); +#endif +} + +static inline void odpdrv_atomic_dec_u64(odpdrv_atomic_u64_t *atom) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v--); +#else + (void)__atomic_fetch_sub(&atom->v, 1, __ATOMIC_RELAXED); +#endif +} + +static inline int odpdrv_atomic_cas_u64(odpdrv_atomic_u64_t *atom, + uint64_t *old_val, uint64_t new_val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + int ret; + *old_val = ATOMIC_OP(atom, ATOMIC_CAS_OP(&ret, *old_val, new_val)); + return ret; +#else + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_RELAXED, + __ATOMIC_RELAXED); +#endif +} + +static inline uint64_t odpdrv_atomic_xchg_u64(odpdrv_atomic_u64_t *atom, + uint64_t new_val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + return ATOMIC_OP(atom, atom->v = new_val); +#else + return __atomic_exchange_n(&atom->v, new_val, __ATOMIC_RELAXED); +#endif +} + +static inline void odpdrv_atomic_max_u64(odpdrv_atomic_u64_t *atom, + uint64_t new_max) +{ + uint64_t old_val; + + old_val = odpdrv_atomic_load_u64(atom); + + while (new_max > old_val) { + if (odpdrv_atomic_cas_u64(atom, &old_val, new_max)) + break; + } +} + +static inline void odpdrv_atomic_min_u64(odpdrv_atomic_u64_t *atom, + uint64_t new_min) +{ + uint64_t old_val; + + old_val = odpdrv_atomic_load_u64(atom); + + while (new_min < old_val) { + if (odpdrv_atomic_cas_u64(atom, &old_val, new_min)) + break; + } +} + +static inline uint32_t odpdrv_atomic_load_acq_u32(odpdrv_atomic_u32_t *atom) +{ + return __atomic_load_n(&atom->v, __ATOMIC_ACQUIRE); +} + +static inline void odpdrv_atomic_store_rel_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + __atomic_store_n(&atom->v, val, __ATOMIC_RELEASE); +} + +static inline void odpdrv_atomic_add_rel_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + (void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELEASE); +} + +static inline void odpdrv_atomic_sub_rel_u32(odpdrv_atomic_u32_t *atom, + uint32_t val) +{ + (void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELEASE); +} + +static inline int odpdrv_atomic_cas_acq_u32(odpdrv_atomic_u32_t *atom, + uint32_t *old_val, uint32_t new_val) +{ + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_ACQUIRE, + __ATOMIC_RELAXED); +} + +static inline int odpdrv_atomic_cas_rel_u32(odpdrv_atomic_u32_t *atom, + uint32_t *old_val, uint32_t new_val) +{ + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_RELEASE, + __ATOMIC_RELAXED); +} + +static inline int odpdrv_atomic_cas_acq_rel_u32(odpdrv_atomic_u32_t *atom, + uint32_t *old_val, + uint32_t new_val) +{ + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_ACQ_REL, + __ATOMIC_RELAXED); +} + +static inline uint64_t odpdrv_atomic_load_acq_u64(odpdrv_atomic_u64_t *atom) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + return ATOMIC_OP(atom, (void)0); +#else + return __atomic_load_n(&atom->v, __ATOMIC_ACQUIRE); +#endif +} + +static inline void odpdrv_atomic_store_rel_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v = val); +#else + __atomic_store_n(&atom->v, val, __ATOMIC_RELEASE); +#endif +} + +static inline void odpdrv_atomic_add_rel_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v += val); +#else + (void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELEASE); +#endif +} + +static inline void odpdrv_atomic_sub_rel_u64(odpdrv_atomic_u64_t *atom, + uint64_t val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + (void)ATOMIC_OP(atom, atom->v -= val); +#else + (void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELEASE); +#endif +} + +static inline int odpdrv_atomic_cas_acq_u64(odpdrv_atomic_u64_t *atom, + uint64_t *old_val, uint64_t new_val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + int ret; + *old_val = ATOMIC_OP(atom, ATOMIC_CAS_OP(&ret, *old_val, new_val)); + return ret; +#else + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_ACQUIRE, + __ATOMIC_RELAXED); +#endif +} + +static inline int odpdrv_atomic_cas_rel_u64(odpdrv_atomic_u64_t *atom, + uint64_t *old_val, uint64_t new_val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + int ret; + *old_val = ATOMIC_OP(atom, ATOMIC_CAS_OP(&ret, *old_val, new_val)); + return ret; +#else + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_RELEASE, + __ATOMIC_RELAXED); +#endif +} + +static inline int odpdrv_atomic_cas_acq_rel_u64(odpdrv_atomic_u64_t *atom, + uint64_t *old_val, + uint64_t new_val) +{ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + int ret; + *old_val = ATOMIC_OP(atom, ATOMIC_CAS_OP(&ret, *old_val, new_val)); + return ret; +#else + return __atomic_compare_exchange_n(&atom->v, old_val, new_val, + 0 /* strong */, + __ATOMIC_ACQ_REL, + __ATOMIC_RELAXED); +#endif +} + +/** + * @} + */ + +#include + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/platform/linux-generic/include/odp/drv/plat/atomic_types.h b/platform/linux-generic/include/odp/drv/plat/atomic_types.h new file mode 100644 index 0000000..6a7ff0d --- /dev/null +++ b/platform/linux-generic/include/odp/drv/plat/atomic_types.h @@ -0,0 +1,88 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/** + * @file + * + * ODPDRV atomic operations + */ + +#ifndef ODPDRV_ATOMIC_TYPES_H_ +#define ODPDRV_ATOMIC_TYPES_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +/** + * @internal + * Atomic 64-bit unsigned integer + */ +struct odpdrv_atomic_u64_s { + uint64_t v; /**< Actual storage for the atomic variable */ +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + /* Some architectures do not support lock-free operations on 64-bit + * data types. We use a spin lock to ensure atomicity. */ + char lock; /**< Spin lock (if needed) used to ensure atomic access */ +#endif +} ODPDRV_ALIGNED(sizeof(uint64_t)); /* Enforce alignement! */; + +/** + * @internal + * Atomic 32-bit unsigned integer + */ +struct odpdrv_atomic_u32_s { + uint32_t v; /**< Actual storage for the atomic variable */ +} ODPDRV_ALIGNED(sizeof(uint32_t)); /* Enforce alignement! */; + +#if __GCC_ATOMIC_LLONG_LOCK_FREE < 2 + +/** + * @internal + * CAS operation expression for the ATOMIC_OP macro + */ +#define ATOMIC_CAS_OP(ret_ptr, old_val, new_val) \ +({ \ + if (atom->v == (old_val)) { \ + atom->v = (new_val); \ + *(ret_ptr) = 1; \ + } else { \ + *(ret_ptr) = 0; \ + } \ +}) + +/** + * @internal + * Helper macro for lock-based atomic operations on 64-bit integers + * @param[in,out] atom Pointer to the 64-bit atomic variable + * @param expr Expression used update the variable. + * @return The old value of the variable. + */ +#define ATOMIC_OP(atom, expr) \ +({ \ + uint64_t _old_val; \ + /* Loop while lock is already taken, stop when lock becomes clear */ \ + while (__atomic_test_and_set(&(atom)->lock, __ATOMIC_ACQUIRE)) \ + (void)0; \ + _old_val = (atom)->v; \ + (expr); /* Perform whatever update is desired */ \ + __atomic_clear(&(atom)->lock, __ATOMIC_RELEASE); \ + _old_val; /* Return old value */ \ +}) +#endif + +typedef struct odpdrv_atomic_u64_s odpdrv_atomic_u64_t; + +typedef struct odpdrv_atomic_u32_s odpdrv_atomic_u32_t; + +#ifdef __cplusplus +} +#endif + +#endif