atomic refactor

This commit is contained in:
dmchen 2024-02-02 02:21:06 +00:00
parent d3b668bd5f
commit 7ae45a0a83
3 changed files with 25 additions and 19 deletions

View File

@ -20,17 +20,17 @@
#include "taos_metric_t.h" #include "taos_metric_t.h"
#if !defined(WINDOWS) #if !defined(WINDOWS)
#define DOUBLE_ATOMIC #define C11_ATOMIC
#endif #endif
#ifdef DOUBLE_ATOMIC #ifdef C11_ATOMIC
#include <stdatomic.h> #include <stdatomic.h>
#endif #endif
struct taos_metric_sample { struct taos_metric_sample {
taos_metric_type_t type; /**< type is the metric type for the 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 */ 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 */ _Atomic double r_value; /**< r_value is the value of the metric sample */
#else #else
double r_value; /**< r_value is the value of the metric sample */ double r_value; /**< r_value is the value of the metric sample */

View File

@ -25,7 +25,7 @@
#include "taos_metric_sample_i.h" #include "taos_metric_sample_i.h"
#include "taos_metric_sample_t.h" #include "taos_metric_sample_t.h"
#ifdef DOUBLE_ATOMIC #ifdef C11_ATOMIC
#include <stdatomic.h> #include <stdatomic.h>
#else #else
#define ALLOW_FORBID_FUNC #define ALLOW_FORBID_FUNC
@ -71,7 +71,7 @@ int taos_metric_sample_add(taos_metric_sample_t *self, double r_value) {
return 1; return 1;
} }
#ifdef DOUBLE_ATOMIC #ifdef C11_ATOMIC
/*_Atomic*/ double old = atomic_load(&self->r_value); /*_Atomic*/ double old = atomic_load(&self->r_value);
for (;;) { for (;;) {
@ -94,7 +94,7 @@ int taos_metric_sample_sub(taos_metric_sample_t *self, double r_value) {
return 1; return 1;
} }
#ifdef DOUBLE_ATOMIC #ifdef C11_ATOMIC
/*_Atomic*/ double old = atomic_load(&self->r_value); /*_Atomic*/ double old = atomic_load(&self->r_value);
for (;;) { for (;;) {
_Atomic double new = ATOMIC_VAR_INIT(old - r_value); _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; return 1;
} }
#ifdef DOUBLE_ATOMIC #ifdef C11_ATOMIC
atomic_store(&self->r_value, r_value); atomic_store(&self->r_value, r_value);
#else #else
atomic_store_double(&self->r_value, r_value); 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; return 1;
} }
#ifdef DOUBLE_ATOMIC #ifdef C11_ATOMIC
_Atomic double new = ATOMIC_VAR_INIT(r_value); _Atomic double new = ATOMIC_VAR_INIT(r_value);
for (;;) { for (;;) {
/*_Atomic*/ double old = atomic_load(&self->r_value); /*_Atomic*/ double old = atomic_load(&self->r_value);

View File

@ -346,13 +346,16 @@ void atomic_store_64(int64_t volatile* ptr, int64_t val) {
double atomic_store_double(double volatile *ptr, double val){ double atomic_store_double(double volatile *ptr, double val){
for (;;) { 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){ double atomic_exchange_double(double volatile *ptr, int64_t val){
for (;;) { 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) { if (ret_num.i == old_num.i) {
return *(double *)(&iold_value); 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){ double atomic_fetch_add_double(double volatile *ptr, double val){
for (;;) { for (;;) {
double_number old_num = {0}; 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}; double_number new_num = {0};
new_num.d = old_num.d + val; 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){ double atomic_fetch_sub_double(double volatile *ptr, double val){
for (;;) { for (;;) {
double_number old_num = {0}; 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}; double_number new_num = {0};
new_num.d = old_num.d - val; new_num.d = old_num.d - val;