From patchwork Mon Jul 18 17:49:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 72234 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp214193qga; Mon, 18 Jul 2016 10:03:17 -0700 (PDT) X-Received: by 10.37.36.16 with SMTP id k16mr12711315ybk.37.1468861390305; Mon, 18 Jul 2016 10:03:10 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id d8si8828216qta.81.2016.07.18.10.03.09; Mon, 18 Jul 2016 10:03:10 -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 BD73261DFD; Mon, 18 Jul 2016 17:03:09 +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 AD1B76179E; Mon, 18 Jul 2016 16:52: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 F0533687F3; Mon, 18 Jul 2016 16:52:04 +0000 (UTC) Received: from mail-lf0-f47.google.com (mail-lf0-f47.google.com [209.85.215.47]) by lists.linaro.org (Postfix) with ESMTPS id B22DF687EF for ; Mon, 18 Jul 2016 16:50:54 +0000 (UTC) Received: by mail-lf0-f47.google.com with SMTP id f93so136189829lfi.2 for ; Mon, 18 Jul 2016 09:50:54 -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=2GSF5xSIyMwRtj0kVjyD3uTAw11zw5q+97E5B2Fiht0=; b=iW0IBxmLxsoCgdA7M8dy3pF0ECcVPxmFgDENNOhWVRt26rcxlnVVG66NzYqd4c1Obc ypXq4HsGxJd/KhVN15UZn6fWP5h0EE0hfr488FTfIJz2tdD5P4RPbsrNEjHXPE1b67V9 xpIorZpMfZe+oUhRm31DGLu1PyOP2GnCE0bsYM2kznVa6R1Ak7aARpaCacZ72NY/Uo/B uXJy5lKnwdIAKNl0m+TrqpoNEJ8goHoIhQD594rt72BnqoT8pZD1MIO5+iD2jJM3J+qm l5xInx8/AaXP8i7gf7tLdQhh4W8p4vZwrd8rc8EkldfkvMcEnxxki/4s67yLMGOktJJM tX3g== X-Gm-Message-State: ALyK8tKkK6k3YxSrm3fLRJudRIk3EBGc39qE5K+d9Qubi2e6UEtwr3B9oyjG+4bQEe1SxrE8J+s= X-Received: by 10.25.43.144 with SMTP id r138mr16183940lfr.112.1468860653417; Mon, 18 Jul 2016 09:50:53 -0700 (PDT) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-90-46.cust.bredband2.com. [83.233.90.46]) by smtp.gmail.com with ESMTPSA id y200sm2226229lfd.12.2016.07.18.09.50.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 18 Jul 2016 09:50:52 -0700 (PDT) From: Christophe Milard To: mike.holmes@linaro.org, yi.he@linaro.org, balakrishna.garapati@linaro.org, lng-odp@lists.linaro.org Date: Mon, 18 Jul 2016 19:49:35 +0200 Message-Id: <1468864178-43721-16-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1468864178-43721-1-git-send-email-christophe.milard@linaro.org> References: <1468864178-43721-1-git-send-email-christophe.milard@linaro.org> X-Topics: patch Subject: [lng-odp] [API-NEXT PATCH 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" duplicated from the API side (hash: 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