atomic refactor
This commit is contained in:
parent
d3b668bd5f
commit
7ae45a0a83
|
@ -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 <stdatomic.h>
|
||||
#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 */
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
#include "taos_metric_sample_i.h"
|
||||
#include "taos_metric_sample_t.h"
|
||||
|
||||
#ifdef DOUBLE_ATOMIC
|
||||
#ifdef C11_ATOMIC
|
||||
#include <stdatomic.h>
|
||||
#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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue