double atomic lib

This commit is contained in:
dmchen 2024-02-02 01:48:00 +00:00
parent 97a120ee09
commit d3b668bd5f
3 changed files with 69 additions and 54 deletions

View File

@ -48,11 +48,13 @@ void atomic_store_8(int8_t volatile *ptr, int8_t val);
void atomic_store_16(int16_t volatile *ptr, int16_t val);
void atomic_store_32(int32_t volatile *ptr, int32_t val);
void atomic_store_64(int64_t volatile *ptr, int64_t val);
double atomic_store_double(double volatile *ptr, double val);
void atomic_store_ptr(void *ptr, void *val);
int8_t atomic_exchange_8(int8_t volatile *ptr, int8_t val);
int16_t atomic_exchange_16(int16_t volatile *ptr, int16_t val);
int32_t atomic_exchange_32(int32_t volatile *ptr, int32_t val);
int64_t atomic_exchange_64(int64_t volatile *ptr, int64_t val);
double atomic_exchange_double(double volatile *ptr, int64_t val);
void *atomic_exchange_ptr(void *ptr, void *val);
int8_t atomic_val_compare_exchange_8(int8_t volatile *ptr, int8_t oldval, int8_t newval);
int16_t atomic_val_compare_exchange_16(int16_t volatile *ptr, int16_t oldval, int16_t newval);
@ -68,6 +70,7 @@ int8_t atomic_fetch_add_8(int8_t volatile *ptr, int8_t val);
int16_t atomic_fetch_add_16(int16_t volatile *ptr, int16_t val);
int32_t atomic_fetch_add_32(int32_t volatile *ptr, int32_t val);
int64_t atomic_fetch_add_64(int64_t volatile *ptr, int64_t val);
double atomic_fetch_add_double(double volatile *ptr, double val);
void *atomic_fetch_add_ptr(void *ptr, void *val);
int8_t atomic_sub_fetch_8(int8_t volatile *ptr, int8_t val);
int16_t atomic_sub_fetch_16(int16_t volatile *ptr, int16_t val);
@ -78,6 +81,7 @@ int8_t atomic_fetch_sub_8(int8_t volatile *ptr, int8_t val);
int16_t atomic_fetch_sub_16(int16_t volatile *ptr, int16_t val);
int32_t atomic_fetch_sub_32(int32_t volatile *ptr, int32_t val);
int64_t atomic_fetch_sub_64(int64_t volatile *ptr, int64_t val);
double atomic_fetch_sub_double(double volatile *ptr, double val);
void *atomic_fetch_sub_ptr(void *ptr, void *val);
int8_t atomic_and_fetch_8(int8_t volatile *ptr, int8_t val);
int16_t atomic_and_fetch_16(int16_t volatile *ptr, int16_t val);

View File

@ -31,10 +31,6 @@
#define ALLOW_FORBID_FUNC
#include "tdef.h"
#include "osAtomic.h"
typedef union {
volatile int64_t i;
double d;
} double_number;
#endif
taos_metric_sample_t *taos_metric_sample_new(taos_metric_type_t type, const char *l_value, double r_value) {
@ -85,19 +81,7 @@ int taos_metric_sample_add(taos_metric_sample_t *self, double r_value) {
}
}
#else
for (;;) {
double_number old_num;
old_num.i = *(int64_t *)(&self->r_value); // current old value
double_number new_num;
new_num.d = old_num.d + r_value;
int64_t old_value = atomic_val_compare_exchange_64((volatile int64_t *)(&self->r_value), new_num.i, old_num.i);
if (old_value == old_num.i) return 0;
}
//atomic_fetch_add_64(&self->r_value, r_value);
atomic_fetch_add_double(&self->r_value, r_value);
#endif
return 0;
@ -119,19 +103,7 @@ int taos_metric_sample_sub(taos_metric_sample_t *self, double r_value) {
}
}
#else
for (;;) {
double_number old_num;
old_num.i = *(int64_t *)(&self->r_value); // current old value
double_number new_num;
new_num.d = old_num.d - r_value;
int64_t old_value = atomic_val_compare_exchange_64((volatile int64_t *)(&self->r_value), new_num.i, old_num.i);
if (old_value == old_num.i) return 0;
}
//atomic_fetch_sub_64(&self->r_value, r_value);
atomic_fetch_sub_double(&self->r_value, r_value);
#endif
return 0;
@ -146,16 +118,7 @@ int taos_metric_sample_set(taos_metric_sample_t *self, double r_value) {
#ifdef DOUBLE_ATOMIC
atomic_store(&self->r_value, r_value);
#else
for (;;) {
int64_t iOld = *(int64_t *)(&self->r_value); // current old value
int64_t iNew = *(int64_t *)(&r_value);
int64_t old_value = atomic_val_compare_exchange_64((volatile int64_t *)(&self->r_value), iNew, iOld);
if (old_value == iOld) return 0;
}
//atomic_store_64(&self->r_value, r_value);
atomic_store_double(&self->r_value, r_value);
#endif
return 0;
@ -177,20 +140,7 @@ int taos_metric_sample_exchange(taos_metric_sample_t *self, double r_value, doub
}
}
#else
for (;;) {
int64_t iOld = *(int64_t *)(&self->r_value); // current old value
int64_t iNew = *(int64_t *)(&r_value);
int64_t iold_value = atomic_val_compare_exchange_64((volatile int64_t *)(&self->r_value), iNew, iOld);
if (iold_value == iOld) {
*old_value = *(double *)(&iold_value);
return 0;
}
}
//*old_value = atomic_exchange_64(&self->r_value, r_value);
*old_value = atomic_exchange_double(&self->r_value, r_value);
#endif
return 0;

View File

@ -16,6 +16,11 @@
#define ALLOW_FORBID_FUNC
#include "os.h"
typedef union {
volatile int64_t i;
double d;
} double_number;
#ifdef WINDOWS
// add
@ -339,6 +344,18 @@ void atomic_store_64(int64_t volatile* ptr, int64_t val) {
#endif
}
double atomic_store_double(double volatile *ptr, double val){
for (;;) {
int64_t iOld = *(int64_t *)ptr; // current old value
int64_t iNew = *(int64_t *)(&val);
int64_t old_value = atomic_val_compare_exchange_64((volatile int64_t *)ptr, iOld, iNew);
if (old_value == iOld) return old_value;
}
}
void atomic_store_ptr(void* ptr, void* val) {
#ifdef WINDOWS
((*(void* volatile*)(ptr)) = (void*)(val));
@ -393,6 +410,20 @@ int64_t atomic_exchange_64(int64_t volatile* ptr, int64_t val) {
#endif
}
double atomic_exchange_double(double volatile *ptr, int64_t val){
for (;;) {
int64_t iOld = *(int64_t *)ptr; // current old value
int64_t iNew = *(int64_t *)(&val);
int64_t iold_value = atomic_val_compare_exchange_64((volatile int64_t *)ptr, iOld, iNew);
if (iold_value == iOld) {
return *(double *)(&iold_value);
}
}
}
void* atomic_exchange_ptr(void* ptr, void* val) {
#ifdef WINDOWS
#ifdef _WIN64
@ -551,6 +582,21 @@ int64_t atomic_fetch_add_64(int64_t volatile* ptr, int64_t val) {
#endif
}
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
double_number new_num = {0};
new_num.d = old_num.d + val;
double_number ret_num = {0};
ret_num.i = atomic_val_compare_exchange_64((volatile int64_t *)ptr, old_num.i, new_num.i);
if (ret_num.i == old_num.i) return ret_num.d;
}
}
void* atomic_fetch_add_ptr(void* ptr, void* val) {
#ifdef WINDOWS
return _InterlockedExchangePointer((void* volatile*)(ptr), (void*)(val));
@ -657,6 +703,21 @@ int64_t atomic_fetch_sub_64(int64_t volatile* ptr, int64_t val) {
#endif
}
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
double_number new_num = {0};
new_num.d = old_num.d - val;
double_number ret_num = {0};
ret_num.i = atomic_val_compare_exchange_64((volatile int64_t *)ptr, old_num.i, new_num.i);
if (ret_num.i == old_num.i) return ret_num.d;
}
}
void* atomic_fetch_sub_ptr(void* ptr, void* val) {
#ifdef WINDOWS
return interlocked_fetch_sub_ptr(ptr, val);