Skip to content

Commit 6df468f

Browse files
authored
atomic ref count (#403)
1 parent 1a3f394 commit 6df468f

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/model.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "backends.h"
1313
#include "stats.h"
1414
#include "backends/util.h"
15-
15+
#include <pthread.h>
1616
#include "rmutil/alloc.h"
1717
#include "util/arr_rm_alloc.h"
1818
#include "util/dict.h"
@@ -298,7 +298,7 @@ RAI_Model *RAI_ModelCreate(RAI_Backend backend, const char* devicestr, const cha
298298
}
299299

300300
void RAI_ModelFree(RAI_Model* model, RAI_Error* err) {
301-
if (--model->refCount > 0){
301+
if (__atomic_sub_fetch(&model->refCount, 1, __ATOMIC_RELAXED) > 0){
302302
return;
303303
}
304304

@@ -466,7 +466,7 @@ int RAI_ModelRun(RAI_ModelRunCtx** mctxs, long long n, RAI_Error* err) {
466466
}
467467

468468
RAI_Model* RAI_ModelGetShallowCopy(RAI_Model* model) {
469-
++model->refCount;
469+
__atomic_fetch_add(&model->refCount, 1, __ATOMIC_RELAXED);
470470
return model;
471471
}
472472

src/script.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "script_struct.h"
1414
#include "stats.h"
1515
#include "util/arr_rm_alloc.h"
16+
#include <pthread.h>
1617

1718
RedisModuleType* RedisAI_ScriptType = NULL;
1819

@@ -126,7 +127,7 @@ RAI_Script* RAI_ScriptCreate(const char* devicestr, const char* tag,
126127
}
127128

128129
void RAI_ScriptFree(RAI_Script* script, RAI_Error* err) {
129-
if (--script->refCount > 0) {
130+
if (__atomic_sub_fetch(&script->refCount, 1, __ATOMIC_RELAXED) > 0) {
130131
return;
131132
}
132133

@@ -218,7 +219,7 @@ int RAI_ScriptRun(RAI_ScriptRunCtx* sctx, RAI_Error* err) {
218219
}
219220

220221
RAI_Script* RAI_ScriptGetShallowCopy(RAI_Script* script) {
221-
++script->refCount;
222+
__atomic_fetch_add(&script->refCount, 1, __ATOMIC_RELAXED);
222223
return script;
223224
}
224225

src/tensor.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "util/dict.h"
1818
#include <assert.h>
1919
#include "redisai.h"
20+
#include <pthread.h>
2021

2122
RedisModuleType *RedisAI_TensorType = NULL;
2223

@@ -480,7 +481,7 @@ size_t RAI_TensorDataSizeFromDLDataType(DLDataType dtype) {
480481

481482
void RAI_TensorFree(RAI_Tensor *t) {
482483
if (t) {
483-
if (--t->refCount <= 0) {
484+
if (__atomic_sub_fetch(&t->refCount, 1, __ATOMIC_RELAXED) <= 0) {
484485
if (t->tensor.deleter) {
485486
t->tensor.deleter(&t->tensor);
486487
} else {
@@ -632,7 +633,7 @@ int RAI_TensorGetValueAsLongLong(RAI_Tensor* t, long long i, long long* val) {
632633
}
633634

634635
RAI_Tensor* RAI_TensorGetShallowCopy(RAI_Tensor* t){
635-
++t->refCount;
636+
__atomic_fetch_add(&t->refCount, 1, __ATOMIC_RELAXED);
636637
return t;
637638
}
638639

0 commit comments

Comments
 (0)