diff --git a/src/model.c b/src/model.c index 21bb5e58b..3a469ee78 100644 --- a/src/model.c +++ b/src/model.c @@ -12,7 +12,7 @@ #include "backends.h" #include "stats.h" #include "backends/util.h" - +#include #include "rmutil/alloc.h" #include "util/arr_rm_alloc.h" #include "util/dict.h" @@ -298,7 +298,7 @@ RAI_Model *RAI_ModelCreate(RAI_Backend backend, const char* devicestr, const cha } void RAI_ModelFree(RAI_Model* model, RAI_Error* err) { - if (--model->refCount > 0){ + if (__atomic_sub_fetch(&model->refCount, 1, __ATOMIC_RELAXED) > 0){ return; } @@ -466,7 +466,7 @@ int RAI_ModelRun(RAI_ModelRunCtx** mctxs, long long n, RAI_Error* err) { } RAI_Model* RAI_ModelGetShallowCopy(RAI_Model* model) { - ++model->refCount; + __atomic_fetch_add(&model->refCount, 1, __ATOMIC_RELAXED); return model; } diff --git a/src/script.c b/src/script.c index a907e1955..0c0fa4d7b 100644 --- a/src/script.c +++ b/src/script.c @@ -13,6 +13,7 @@ #include "script_struct.h" #include "stats.h" #include "util/arr_rm_alloc.h" +#include RedisModuleType* RedisAI_ScriptType = NULL; @@ -126,7 +127,7 @@ RAI_Script* RAI_ScriptCreate(const char* devicestr, const char* tag, } void RAI_ScriptFree(RAI_Script* script, RAI_Error* err) { - if (--script->refCount > 0) { + if (__atomic_sub_fetch(&script->refCount, 1, __ATOMIC_RELAXED) > 0) { return; } @@ -218,7 +219,7 @@ int RAI_ScriptRun(RAI_ScriptRunCtx* sctx, RAI_Error* err) { } RAI_Script* RAI_ScriptGetShallowCopy(RAI_Script* script) { - ++script->refCount; + __atomic_fetch_add(&script->refCount, 1, __ATOMIC_RELAXED); return script; } diff --git a/src/tensor.c b/src/tensor.c index 2d7485741..16ac618ee 100644 --- a/src/tensor.c +++ b/src/tensor.c @@ -17,6 +17,7 @@ #include "util/dict.h" #include #include "redisai.h" +#include RedisModuleType *RedisAI_TensorType = NULL; @@ -480,7 +481,7 @@ size_t RAI_TensorDataSizeFromDLDataType(DLDataType dtype) { void RAI_TensorFree(RAI_Tensor *t) { if (t) { - if (--t->refCount <= 0) { + if (__atomic_sub_fetch(&t->refCount, 1, __ATOMIC_RELAXED) <= 0) { if (t->tensor.deleter) { t->tensor.deleter(&t->tensor); } else { @@ -632,7 +633,7 @@ int RAI_TensorGetValueAsLongLong(RAI_Tensor* t, long long i, long long* val) { } RAI_Tensor* RAI_TensorGetShallowCopy(RAI_Tensor* t){ - ++t->refCount; + __atomic_fetch_add(&t->refCount, 1, __ATOMIC_RELAXED); return t; }