• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5034

24 Apr 2026 11:25AM UTC coverage: 73.058%. Remained the same
#5034

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1336 of 1975 new or added lines in 48 files covered. (67.65%)

14149 existing lines in 164 files now uncovered.

275896 of 377640 relevant lines covered (73.06%)

132944440.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/source/util/src/tref.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "tref.h"
18
#include "taoserror.h"
19
#include "tlog.h"
20
#include "tutil.h"
21

22
#define TSDB_REF_OBJECTS        2000
23
#define TSDB_REF_STATE_EMPTY    0
24
#define TSDB_REF_STATE_ACTIVE   1
25
#define TSDB_REF_STATE_DELETED  2
26
#define TSDB_REF_ITER_THRESHOLD 100
27

28
typedef struct SRefNode {
29
  struct SRefNode *prev;     // previous node
30
  struct SRefNode *next;     // next node
31
  void            *p;        // pointer to resource protected,
32
  int64_t          rid;      // reference ID
33
  int32_t          count;    // number of references
34
  int32_t          removed;  // 1: removed
35
} SRefNode;
36

37
typedef struct {
38
  SRefNode **nodeList;  // array of SRefNode linked list
39
  int32_t    state;     // 0: empty, 1: active;  2: deleted
40
  int32_t    rsetId;    // refSet ID, global unique
41
  int64_t    rid;       // increase by one for each new reference
42
  int32_t    max;       // mod
43
  int32_t    count;     // total number of SRefNodes in this set
44
  int64_t   *lockedBy;
45
  void (*fp)(void *);
46
} SRefSet;
47

48
static SRefSet       tsRefSetList[TSDB_REF_OBJECTS];
49
static TdThreadOnce  tsRefModuleInit = PTHREAD_ONCE_INIT;
50
static TdThreadMutex tsRefMutex;
51
static int32_t       tsRefSetNum = 0;
52
static int32_t       tsNextId = 0;
53

54
static void    taosInitRefModule(void);
55
static void    taosLockList(int64_t *lockedBy);
56
static void    taosUnlockList(int64_t *lockedBy);
57
static void    taosIncRsetCount(SRefSet *pSet);
58
static void    taosDecRsetCount(SRefSet *pSet);
59
static int32_t taosDecRefCount(int32_t rsetId, int64_t rid, int32_t remove, int32_t *isReleased);
60

61
int32_t taosOpenRef(int32_t max, RefFp fp) {
37,588,105✔
62
  SRefNode **nodeList;
63
  SRefSet   *pSet;
64
  int64_t   *lockedBy;
65
  int32_t    i, rsetId;
66

67
  (void)taosThreadOnce(&tsRefModuleInit, taosInitRefModule);
37,588,105✔
68

69
  nodeList = taosMemoryCalloc(sizeof(SRefNode *), (size_t)max);
37,588,105✔
70
  if (nodeList == NULL) {
37,588,105✔
71
    return terrno;
×
72
  }
73

74
  lockedBy = taosMemoryCalloc(sizeof(int64_t), (size_t)max);
37,588,105✔
75
  if (lockedBy == NULL) {
37,588,105✔
76
    taosMemoryFree(nodeList);
×
77
    return terrno;
×
78
  }
79

80
  (void)taosThreadMutexLock(&tsRefMutex);
37,588,105✔
81

82
  for (i = 0; i < TSDB_REF_OBJECTS; ++i) {
37,588,105✔
83
    tsNextId = (tsNextId + 1) % TSDB_REF_OBJECTS;
37,588,105✔
84
    if (tsNextId == 0) tsNextId = 1;  // dont use 0 as rsetId
37,588,105✔
85
    if (tsRefSetList[tsNextId].state == TSDB_REF_STATE_EMPTY) break;
37,588,105✔
86
  }
87

88
  if (i < TSDB_REF_OBJECTS) {
37,588,105✔
89
    rsetId = tsNextId;
37,588,105✔
90
    pSet = tsRefSetList + rsetId;
37,588,105✔
91
    pSet->max = max;
37,588,105✔
92
    pSet->nodeList = nodeList;
37,588,105✔
93
    pSet->lockedBy = lockedBy;
37,588,105✔
94
    pSet->fp = fp;
37,588,105✔
95
    pSet->rid = 1;
37,588,105✔
96
    pSet->rsetId = rsetId;
37,588,105✔
97
    pSet->state = TSDB_REF_STATE_ACTIVE;
37,588,105✔
98
    taosIncRsetCount(pSet);
37,588,105✔
99

100
    tsRefSetNum++;
37,588,105✔
101
    uTrace("rsetId:%d, is opened, max:%d, fp:%p refSetNum:%d", rsetId, max, fp, tsRefSetNum);
37,588,105✔
102
  } else {
103
    rsetId = TSDB_CODE_REF_FULL;
×
104
    taosMemoryFree(nodeList);
×
105
    taosMemoryFree(lockedBy);
×
106
    uTrace("run out of Ref ID, maximum:%d refSetNum:%d", TSDB_REF_OBJECTS, tsRefSetNum);
×
107
  }
108

109
  (void)taosThreadMutexUnlock(&tsRefMutex);
37,588,105✔
110

111
  return rsetId;
37,588,105✔
112
}
113

114
void taosCloseRef(int32_t rsetId) {
34,044,178✔
115
  SRefSet *pSet;
116
  int32_t  deleted = 0;
34,044,178✔
117

118
  if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) {
34,044,178✔
119
    uTrace("rsetId:%d, is invalid, out of range", rsetId);
86✔
120
    return;
86✔
121
  }
122

123
  pSet = tsRefSetList + rsetId;
34,044,092✔
124

125
  (void)taosThreadMutexLock(&tsRefMutex);
34,044,092✔
126

127
  if (pSet->state == TSDB_REF_STATE_ACTIVE) {
34,044,092✔
128
    pSet->state = TSDB_REF_STATE_DELETED;
31,746,452✔
129
    deleted = 1;
31,746,452✔
130
    uTrace("rsetId:%d, is closed, count:%d", rsetId, pSet->count);
31,746,452✔
131
  } else {
132
    uTrace("rsetId:%d, is already closed, count:%d", rsetId, pSet->count);
2,297,640✔
133
  }
134

135
  (void)taosThreadMutexUnlock(&tsRefMutex);
34,044,092✔
136

137
  if (deleted) taosDecRsetCount(pSet);
34,044,092✔
138
}
139

140
int32_t taosGetRefSetCount(int32_t rsetId, int32_t *pCount) {
926,598,518✔
141
  SRefSet *pSet;
142

143
  if (pCount == NULL) {
926,598,518✔
NEW
144
    return terrno = TSDB_CODE_INVALID_PARA;
×
145
  }
146

147
  if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) {
926,598,518✔
NEW
148
    uTrace("rsetId:%d, failed to get state/count, out of range", rsetId);
×
NEW
149
    return terrno = TSDB_CODE_REF_INVALID_ID;
×
150
  }
151

152
  (void)taosThreadOnce(&tsRefModuleInit, taosInitRefModule);
926,598,518✔
153

154
  pSet = tsRefSetList + rsetId;
926,598,518✔
155

156
  (void)taosThreadMutexLock(&tsRefMutex);
926,598,518✔
157
  *pCount = atomic_load_32(&pSet->count);
926,598,518✔
158
  (void)taosThreadMutexUnlock(&tsRefMutex);
926,598,518✔
159

160
  return TSDB_CODE_SUCCESS;
926,598,518✔
161
}
162

163
int64_t taosAddRef(int32_t rsetId, void *p) {
2,147,483,647✔
164
  int32_t   hash;
165
  SRefNode *pNode;
166
  SRefSet  *pSet;
167
  int64_t   rid = 0;
2,147,483,647✔
168

169
  if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) {
2,147,483,647✔
170
    uTrace("p:%p, failed to add, rsetId not valid, rsetId:%d", p, rsetId);
6✔
171
    return terrno = TSDB_CODE_REF_INVALID_ID;
6✔
172
  }
173

174
  pSet = tsRefSetList + rsetId;
2,147,483,647✔
175
  taosIncRsetCount(pSet);
2,147,483,647✔
176
  if (pSet->state != TSDB_REF_STATE_ACTIVE) {
2,147,483,647✔
UNCOV
177
    taosDecRsetCount(pSet);
×
UNCOV
178
    uTrace("p:%p, failed to add, not active, rsetId:%d", p, rsetId);
×
UNCOV
179
    return terrno = TSDB_CODE_REF_ID_REMOVED;
×
180
  }
181

182
  pNode = taosMemoryCalloc(sizeof(SRefNode), 1);
2,147,483,647✔
183
  if (pNode == NULL) {
2,147,483,647✔
UNCOV
184
    taosDecRsetCount(pSet);
×
UNCOV
185
    uError("p:%p, failed to add, out of memory, rsetId:%d", p, rsetId);
×
UNCOV
186
    return terrno;
×
187
  }
188

189
  rid = atomic_add_fetch_64(&pSet->rid, 1);
2,147,483,647✔
190
  hash = rid % pSet->max;
2,147,483,647✔
191
  taosLockList(pSet->lockedBy + hash);
2,147,483,647✔
192

193
  pNode->p = p;
2,147,483,647✔
194
  pNode->rid = rid;
2,147,483,647✔
195
  pNode->count = 1;
2,147,483,647✔
196

197
  pNode->prev = NULL;
2,147,483,647✔
198
  pNode->next = pSet->nodeList[hash];
2,147,483,647✔
199
  if (pSet->nodeList[hash]) pSet->nodeList[hash]->prev = pNode;
2,147,483,647✔
200
  pSet->nodeList[hash] = pNode;
2,147,483,647✔
201

202
  uTrace("p:%p, rid:0x%" PRIx64 " is added, count:%d, remain count:%d rsetId:%d", p, rid, pSet->count, pNode->count,
2,147,483,647✔
203
         rsetId);
204

205
  taosUnlockList(pSet->lockedBy + hash);
2,147,483,647✔
206

207
  return rid;
2,147,483,647✔
208
}
209

210
int32_t taosRemoveRef(int32_t rsetId, int64_t rid) { return taosDecRefCount(rsetId, rid, 1, NULL); }
2,147,483,647✔
211

212
// if rid is 0, return the first p in hash list, otherwise, return the next after current rid
213
void *taosAcquireRef(int32_t rsetId, int64_t rid) {
2,147,483,647✔
214
  int32_t   hash;
215
  SRefNode *pNode;
216
  SRefSet  *pSet;
217
  int32_t   iter = 0;
2,147,483,647✔
218
  void     *p = NULL;
2,147,483,647✔
219

220
  if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) {
2,147,483,647✔
221
    // uTrace("rsetId:%d, rid:0x%" PRIx64 " failed to acquire, rsetId not valid", rsetId, rid);
222
    terrno = TSDB_CODE_REF_INVALID_ID;
278✔
223
    return NULL;
72,801✔
224
  }
225

226
  if (rid <= 0) {
2,147,483,647✔
227
    uTrace("rid:0x%" PRIx64 ", failed to acquire, rid not valid, rsetId:%d", rid, rsetId);
266,317,441✔
228
    terrno = TSDB_CODE_REF_NOT_EXIST;
266,317,441✔
229
    return NULL;
266,317,441✔
230
  }
231

232
  pSet = tsRefSetList + rsetId;
2,147,483,647✔
233
  taosIncRsetCount(pSet);
2,147,483,647✔
234
  if (pSet->state != TSDB_REF_STATE_ACTIVE) {
2,147,483,647✔
UNCOV
235
    uTrace("rid:0x%" PRIx64 ", failed to acquire, not active, rsetId:%d", rid, rsetId);
×
UNCOV
236
    taosDecRsetCount(pSet);
×
UNCOV
237
    terrno = TSDB_CODE_REF_ID_REMOVED;
×
UNCOV
238
    return NULL;
×
239
  }
240

241
  hash = rid % pSet->max;
2,147,483,647✔
242
  taosLockList(pSet->lockedBy + hash);
2,147,483,647✔
243

244
  pNode = pSet->nodeList[hash];
2,147,483,647✔
245

246
  while (pNode) {
2,147,483,647✔
247
    if (pNode->rid == rid) {
2,147,483,647✔
248
      break;
2,147,483,647✔
249
    }
250
    iter++;
14,672,958✔
251
    pNode = pNode->next;
14,672,958✔
252
  }
253
 
254
 if (iter >= TSDB_REF_ITER_THRESHOLD) {
2,147,483,647✔
UNCOV
255
    uWarn("rsetId:%d rid:%" PRId64 " iter:%d", rsetId, rid, iter);
×
256
  }
257

258
  if (pNode) {
2,147,483,647✔
259
    if (pNode->removed == 0) {
2,147,483,647✔
260
      pNode->count++;
2,147,483,647✔
261
      p = pNode->p;
2,147,483,647✔
262
      uTrace("p:%p, rid:0x%" PRIx64 " is acquired, remain count:%d, rsetId:%d", pNode->p, rid, pNode->count, rsetId);
2,147,483,647✔
263
    } else {
264
      terrno = TSDB_CODE_REF_NOT_EXIST;
9,615,028✔
265
      uTrace("p:%p, rid:0x%" PRIx64 " is already removed, failed to acquire, rsetId:%d", pNode->p, rid, rsetId);
9,784,920✔
266
    }
267
  } else {
268
    terrno = TSDB_CODE_REF_NOT_EXIST;
1,342,387,801✔
269
    uTrace("rid:0x%" PRIx64 ", is not there, failed to acquire, rsetId:%d", rid, rsetId);
1,342,381,098✔
270
  }
271

272
  taosUnlockList(pSet->lockedBy + hash);
2,147,483,647✔
273

274
  taosDecRsetCount(pSet);
2,147,483,647✔
275

276
  return p;
2,147,483,647✔
277
}
278

279
int32_t taosReleaseRef(int32_t rsetId, int64_t rid) { return taosDecRefCount(rsetId, rid, 0, NULL); }
2,147,483,647✔
280
int32_t taosReleaseRefEx(int32_t rsetId, int64_t rid, int32_t *isReleased) {
877,713,880✔
281
  return taosDecRefCount(rsetId, rid, 0, isReleased);
877,713,880✔
282
}
283

284
// if rid is 0, return the first p in hash list, otherwise, return the next after current rid
285
void *taosIterateRef(int32_t rsetId, int64_t rid) {
261,049,428✔
286
  SRefNode *pNode = NULL;
261,049,428✔
287
  SRefSet  *pSet;
288

289
  if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) {
261,049,428✔
UNCOV
290
    uTrace("rid:0x%" PRIx64 ", failed to iterate, rsetId not valid, rsetId:%d", rid, rsetId);
×
UNCOV
291
    terrno = TSDB_CODE_REF_INVALID_ID;
×
UNCOV
292
    return NULL;
×
293
  }
294

295
  if (rid < 0) {
261,049,428✔
UNCOV
296
    uTrace("rid:0x%" PRIx64 ", failed to iterate, rid not valid, rsetId:%d", rid, rsetId);
×
UNCOV
297
    terrno = TSDB_CODE_REF_NOT_EXIST;
×
UNCOV
298
    return NULL;
×
299
  }
300

301
  void *newP = NULL;
261,049,428✔
302
  pSet = tsRefSetList + rsetId;
261,049,428✔
303
  taosIncRsetCount(pSet);
261,049,428✔
304
  if (pSet->state != TSDB_REF_STATE_ACTIVE) {
261,049,428✔
UNCOV
305
    uTrace("rid:0x%" PRIx64 ", failed to iterate, rset not active, rsetId:%d", rid, rsetId);
×
UNCOV
306
    terrno = TSDB_CODE_REF_ID_REMOVED;
×
UNCOV
307
    taosDecRsetCount(pSet);
×
UNCOV
308
    return NULL;
×
309
  }
310

311
  do {
312
    newP = NULL;
261,049,428✔
313
    int32_t hash = 0;
261,049,428✔
314
    int32_t iter = 0;
261,049,428✔
315
    if (rid > 0) {
261,049,428✔
316
      hash = rid % pSet->max;
201,574,101✔
317
      taosLockList(pSet->lockedBy + hash);
201,574,101✔
318

319
      pNode = pSet->nodeList[hash];
201,574,101✔
320
      while (pNode) {
205,430,317✔
321
        if (pNode->rid == rid) break;
205,430,317✔
322
        pNode = pNode->next;
3,856,216✔
323
        iter++;
3,856,216✔
324
      }
325

326
      if (iter >= TSDB_REF_ITER_THRESHOLD) {
201,574,101✔
UNCOV
327
        uWarn("rid:0x%" PRIx64 ", iter:%d, rsetId:%d", rid, iter, rsetId);
×
328
      }
329

330
      if (pNode == NULL) {
201,574,101✔
331
        uError("rid:0x%" PRIx64 ", not there and quit, rsetId:%d", rid, rsetId);
×
UNCOV
332
        terrno = TSDB_CODE_REF_NOT_EXIST;
×
UNCOV
333
        taosUnlockList(pSet->lockedBy + hash);
×
UNCOV
334
        taosDecRsetCount(pSet);
×
UNCOV
335
        return NULL;
×
336
      }
337

338
      // rid is there
339
      pNode = pNode->next;
201,574,101✔
340
      // check first place
341
      while (pNode) {
201,574,101✔
342
        if (!pNode->removed) break;
3,638,127✔
UNCOV
343
        pNode = pNode->next;
×
344
      }
345
      if (pNode == NULL) {
201,574,101✔
346
        taosUnlockList(pSet->lockedBy + hash);
197,935,974✔
347
        hash++;
197,935,974✔
348
      }
349
    }
350

351
    if (pNode == NULL) {
261,049,428✔
352
      for (; hash < pSet->max; ++hash) {
2,147,483,647✔
353
        taosLockList(pSet->lockedBy + hash);
2,147,483,647✔
354
        pNode = pSet->nodeList[hash];
2,147,483,647✔
355
        if (pNode) {
2,147,483,647✔
356
          // check first place
357
          while (pNode) {
197,935,974✔
358
            if (!pNode->removed) break;
197,935,974✔
UNCOV
359
            pNode = pNode->next;
×
360
          }
361
          if (pNode) break;
197,935,974✔
362
        }
363
        taosUnlockList(pSet->lockedBy + hash);
2,147,483,647✔
364
      }
365
    }
366

367
    if (pNode) {
261,049,428✔
368
      pNode->count++;  // acquire it
201,574,101✔
369
      newP = pNode->p;
201,574,101✔
370
      taosUnlockList(pSet->lockedBy + hash);
201,574,101✔
371
      uTrace("p:%p, rid:0x%" PRIx64 " is returned, rsetId:%d", newP, rid, rsetId);
201,574,101✔
372
    } else {
373
      uTrace("rsetId:%d, the list is over", rsetId);
59,475,327✔
374
    }
375

376
    if (rid > 0) taosReleaseRef(rsetId, rid);  // release the current one
261,049,428✔
377
    if (pNode) rid = pNode->rid;
261,049,428✔
378
  } while (newP && pNode->removed);
261,049,428✔
379

380
  taosDecRsetCount(pSet);
261,049,428✔
381

382
  return newP;
261,049,428✔
383
}
384

UNCOV
385
int32_t taosListRef() {
×
386
  SRefSet  *pSet;
387
  SRefNode *pNode;
UNCOV
388
  int32_t   num = 0;
×
389

UNCOV
390
  (void)taosThreadMutexLock(&tsRefMutex);
×
391

UNCOV
392
  for (int32_t i = 0; i < TSDB_REF_OBJECTS; ++i) {
×
UNCOV
393
    pSet = tsRefSetList + i;
×
394

UNCOV
395
    if (pSet->state == TSDB_REF_STATE_EMPTY) continue;
×
396

UNCOV
397
    uInfo("rsetId:%d, state:%d count:%d", i, pSet->state, pSet->count);
×
398

UNCOV
399
    for (int32_t j = 0; j < pSet->max; ++j) {
×
UNCOV
400
      pNode = pSet->nodeList[j];
×
401

UNCOV
402
      while (pNode) {
×
UNCOV
403
        uInfo("p:%p, rid:0x%" PRIx64 " count:%d, rsetId:%d", pNode->p, pNode->rid, pNode->count, i);
×
UNCOV
404
        pNode = pNode->next;
×
UNCOV
405
        num++;
×
406
      }
407
    }
408
  }
409

UNCOV
410
  (void)taosThreadMutexUnlock(&tsRefMutex);
×
411

UNCOV
412
  return num;
×
413
}
414

415
static int32_t taosDecRefCount(int32_t rsetId, int64_t rid, int32_t remove, int32_t *isReleased) {
2,147,483,647✔
416
  int32_t   hash;
417
  SRefSet  *pSet;
418
  SRefNode *pNode;
419
  int32_t   iter = 0;
2,147,483,647✔
420
  int32_t   released = 0;
2,147,483,647✔
421
  int32_t   code = 0;
2,147,483,647✔
422

423
  if (rsetId < 0 || rsetId >= TSDB_REF_OBJECTS) {
2,147,483,647✔
424
    uTrace("rid:0x%" PRIx64 ", failed to remove, rsetId not valid, rsetId:%d", rid, rsetId);
316✔
425
    return terrno = TSDB_CODE_REF_INVALID_ID;
316✔
426
  }
427

428
  if (rid <= 0) {
2,147,483,647✔
429
    uTrace("rid:0x%" PRIx64 ", failed to remove, rid not valid, rsetId:%d", rid, rsetId);
610,233,746✔
430
    return terrno = TSDB_CODE_REF_NOT_EXIST;
610,233,746✔
431
  }
432

433
  pSet = tsRefSetList + rsetId;
2,147,483,647✔
434
  if (pSet->state == TSDB_REF_STATE_EMPTY) {
2,147,483,647✔
435
    uTrace("rid:0x%" PRIx64 ", failed to remove, cleaned, rsetId:%d", rid, rsetId);
×
UNCOV
436
    return terrno = TSDB_CODE_REF_ID_REMOVED;
×
437
  }
438

439
  hash = rid % pSet->max;
2,147,483,647✔
440
  taosLockList(pSet->lockedBy + hash);
2,147,483,647✔
441

442
  pNode = pSet->nodeList[hash];
2,147,483,647✔
443
  while (pNode) {
2,147,483,647✔
444
    if (pNode->rid == rid) break;
2,147,483,647✔
445

446
    pNode = pNode->next;
30,735,425✔
447
    iter++;
30,735,425✔
448
  }
449

450
  if (iter >= TSDB_REF_ITER_THRESHOLD) {
2,147,483,647✔
UNCOV
451
    uWarn("rid:0x%" PRIx64 ", iter:%d, rsetId:%d", rid, iter, rsetId);
×
452
  }
453

454
  if (pNode) {
2,147,483,647✔
455
    pNode->count--;
2,147,483,647✔
456
    if (remove) pNode->removed = 1;
2,147,483,647✔
457

458
    if (pNode->count <= 0) {
2,147,483,647✔
459
      if (pNode->prev) {
2,147,483,647✔
460
        pNode->prev->next = pNode->next;
5,011,622✔
461
      } else {
462
        pSet->nodeList[hash] = pNode->next;
2,147,483,647✔
463
      }
464

465
      if (pNode->next) {
2,147,483,647✔
466
        pNode->next->prev = pNode->prev;
1,489,362✔
467
      }
468
      released = 1;
2,147,483,647✔
469
    } else {
470
      uTrace("p:%p, rid:0x%" PRIx64 " is released, remain count:%d, rsetId:%d", pNode->p, rid, pNode->count, rsetId);
2,147,483,647✔
471
    }
472
  } else {
473
    uTrace("rid:0x%" PRIx64 ", is not there, failed to release/remove, rsetId:%d", rid, rsetId);
1,292,650✔
474
    terrno = TSDB_CODE_REF_NOT_EXIST;
1,292,650✔
475
    code = terrno;
1,292,650✔
476
  }
477

478
  taosUnlockList(pSet->lockedBy + hash);
2,147,483,647✔
479

480
  if (released) {
2,147,483,647✔
481
    uTrace("p:%p, rid:0x%" PRIx64 " is removed, count:%d, free mem:%p, rsetId:%d", pNode->p, rid, pSet->count, pNode,
2,147,483,647✔
482
           rsetId);
483
    (*pSet->fp)(pNode->p);
2,147,483,647✔
484
    taosMemoryFree(pNode);
2,147,483,647✔
485

486
    taosDecRsetCount(pSet);
2,147,483,647✔
487
  }
488

489
  if (isReleased) {
2,147,483,647✔
490
    *isReleased = released;
877,757,925✔
491
  }
492

493
  return code;
2,147,483,647✔
494
}
495

496
static void taosLockList(int64_t *lockedBy) {
2,147,483,647✔
497
  int64_t tid = taosGetSelfPthreadId();
2,147,483,647✔
498
  int32_t i = 0;
2,147,483,647✔
499
  while (atomic_val_compare_exchange_64(lockedBy, 0, tid) != 0) {
2,147,483,647✔
500
    if (++i % 100 == 0) {
2,147,483,647✔
501
      (void)sched_yield();
298,471,950✔
502
    }
503

504
  }
505
}
2,147,483,647✔
506

507
static void taosUnlockList(int64_t *lockedBy) {
2,147,483,647✔
508
  int64_t tid = taosGetSelfPthreadId();
2,147,483,647✔
509
  (void)atomic_val_compare_exchange_64(lockedBy, tid, 0);
2,147,483,647✔
510
}
2,147,483,647✔
511

512
static void taosInitRefModule(void) { (void)taosThreadMutexInit(&tsRefMutex, NULL); }
2,900,310✔
513

514
static void taosIncRsetCount(SRefSet *pSet) {
2,147,483,647✔
515
  (void)atomic_add_fetch_32(&pSet->count, 1);
2,147,483,647✔
516
  // uTrace("rsetId:%d, inc count:%d", pSet->rsetId, count);
517
}
2,147,483,647✔
518

519
static void taosDecRsetCount(SRefSet *pSet) {
2,147,483,647✔
520
  int32_t count = atomic_sub_fetch_32(&pSet->count, 1);
2,147,483,647✔
521
  // uTrace("rsetId:%d, dec count:%d", pSet->rsetId, count);
522

523
  if (count > 0) return;
2,147,483,647✔
524

525
  (void)taosThreadMutexLock(&tsRefMutex);
31,420,864✔
526

527
  if (pSet->state != TSDB_REF_STATE_EMPTY) {
31,516,298✔
528
    pSet->state = TSDB_REF_STATE_EMPTY;
31,516,298✔
529
    pSet->max = 0;
31,516,298✔
530
    pSet->fp = NULL;
31,516,298✔
531

532
    taosMemoryFreeClear(pSet->nodeList);
31,516,298✔
533
    taosMemoryFreeClear(pSet->lockedBy);
31,516,298✔
534

535
    tsRefSetNum--;
31,516,298✔
536
    uTrace("rsetId:%d, is cleaned, refSetNum:%d count:%d", pSet->rsetId, tsRefSetNum, pSet->count);
31,516,298✔
537
  }
538

539
  (void)taosThreadMutexUnlock(&tsRefMutex);
31,516,298✔
540
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc