From 7ae45a0a837d27d7f51e57f942356f820625095d Mon Sep 17 00:00:00 2001 From: dmchen Date: Fri, 2 Feb 2024 02:21:06 +0000 Subject: [PATCH] atomic refactor --- .../libs/monitorfw/inc/taos_metric_sample_t.h | 6 ++-- .../libs/monitorfw/src/taos_metric_sample.c | 10 +++---- source/os/src/osAtomic.c | 28 +++++++++++-------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/source/libs/monitorfw/inc/taos_metric_sample_t.h b/source/libs/monitorfw/inc/taos_metric_sample_t.h index a955a1fa4d..62d58c5b9f 100644 --- a/source/libs/monitorfw/inc/taos_metric_sample_t.h +++ b/source/libs/monitorfw/inc/taos_metric_sample_t.h @@ -20,17 +20,17 @@ #include "taos_metric_t.h" #if !defined(WINDOWS) -#define DOUBLE_ATOMIC +#define C11_ATOMIC #endif -#ifdef DOUBLE_ATOMIC +#ifdef C11_ATOMIC #include #endif struct taos_metric_sample { taos_metric_type_t type; /**< type is the metric type for the sample */ char *l_value; /**< l_value is the full metric name and label set represeted as a string */ -#ifdef DOUBLE_ATOMIC +#ifdef C11_ATOMIC _Atomic double r_value; /**< r_value is the value of the metric sample */ #else double r_value; /**< r_value is the value of the metric sample */ diff --git a/source/libs/monitorfw/src/taos_metric_sample.c b/source/libs/monitorfw/src/taos_metric_sample.c index 445568ed5b..c3f948434d 100644 --- a/source/libs/monitorfw/src/taos_metric_sample.c +++ b/source/libs/monitorfw/src/taos_metric_sample.c @@ -25,7 +25,7 @@ #include "taos_metric_sample_i.h" #include "taos_metric_sample_t.h" -#ifdef DOUBLE_ATOMIC +#ifdef C11_ATOMIC #include #else #define ALLOW_FORBID_FUNC @@ -71,7 +71,7 @@ int taos_metric_sample_add(taos_metric_sample_t *self, double r_value) { return 1; } -#ifdef DOUBLE_ATOMIC +#ifdef C11_ATOMIC /*_Atomic*/ double old = atomic_load(&self->r_value); for (;;) { @@ -94,7 +94,7 @@ int taos_metric_sample_sub(taos_metric_sample_t *self, double r_value) { return 1; } -#ifdef DOUBLE_ATOMIC +#ifdef C11_ATOMIC /*_Atomic*/ double old = atomic_load(&self->r_value); for (;;) { _Atomic double new = ATOMIC_VAR_INIT(old - r_value); @@ -115,7 +115,7 @@ int taos_metric_sample_set(taos_metric_sample_t *self, double r_value) { return 1; } -#ifdef DOUBLE_ATOMIC +#ifdef C11_ATOMIC atomic_store(&self->r_value, r_value); #else atomic_store_double(&self->r_value, r_value); @@ -130,7 +130,7 @@ int taos_metric_sample_exchange(taos_metric_sample_t *self, double r_value, doub return 1; } -#ifdef DOUBLE_ATOMIC +#ifdef C11_ATOMIC _Atomic double new = ATOMIC_VAR_INIT(r_value); for (;;) { /*_Atomic*/ double old = atomic_load(&self->r_value); diff --git a/source/os/src/osAtomic.c b/source/os/src/osAtomic.c index 3736a71e08..e56cf629bb 100644 --- a/source/os/src/osAtomic.c +++ b/source/os/src/osAtomic.c @@ -346,13 +346,16 @@ void atomic_store_64(int64_t volatile* ptr, int64_t val) { double atomic_store_double(double volatile *ptr, double val){ for (;;) { - int64_t iOld = *(int64_t *)ptr; // current old value + double_number old_num = {0}; + old_num.d = *ptr; // current old value - int64_t iNew = *(int64_t *)(&val); + double_number new_num = {0}; + new_num.d = val; - int64_t old_value = atomic_val_compare_exchange_64((volatile int64_t *)ptr, iOld, iNew); + double_number ret_num = {0}; + ret_num.i = atomic_val_compare_exchange_64((volatile int64_t *)ptr, old_num.i, new_num.i); - if (old_value == iOld) return old_value; + if (ret_num.i == old_num.i) return ret_num.d; } } @@ -412,14 +415,17 @@ int64_t atomic_exchange_64(int64_t volatile* ptr, int64_t val) { double atomic_exchange_double(double volatile *ptr, int64_t val){ for (;;) { - int64_t iOld = *(int64_t *)ptr; // current old value + double_number old_num = {0}; + old_num.i = *ptr; // current old value - int64_t iNew = *(int64_t *)(&val); + double_number new_num = {0}; + int64_t iNew = val; - int64_t iold_value = atomic_val_compare_exchange_64((volatile int64_t *)ptr, iOld, iNew); + double_number ret_num = {0}; + ret_num.i = atomic_val_compare_exchange_64((volatile int64_t *)ptr, old_num.i, new_num.i); - if (iold_value == iOld) { - return *(double *)(&iold_value); + if (ret_num.i == old_num.i) { + return ret_num.d; } } } @@ -585,7 +591,7 @@ int64_t atomic_fetch_add_64(int64_t volatile* ptr, int64_t val) { double atomic_fetch_add_double(double volatile *ptr, double val){ for (;;) { double_number old_num = {0}; - old_num.i = *(int64_t *)ptr; // current old value + old_num.d = *ptr; // current old value double_number new_num = {0}; new_num.d = old_num.d + val; @@ -706,7 +712,7 @@ int64_t atomic_fetch_sub_64(int64_t volatile* ptr, int64_t val) { double atomic_fetch_sub_double(double volatile *ptr, double val){ for (;;) { double_number old_num = {0}; - old_num.i = *(int64_t *)ptr; // current old value + old_num.d = *ptr; // current old value double_number new_num = {0}; new_num.d = old_num.d - val;