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

taosdata / TDengine / #4609

28 Jul 2025 03:45AM UTC coverage: 60.413% (-0.7%) from 61.072%
#4609

push

travis-ci

web-flow
merge: from 3.3.6 to main branch #32314 

Merge/3.3.6tomain

138583 of 291345 branches covered (47.57%)

Branch coverage included in aggregate %.

79 of 97 new or added lines in 12 files covered. (81.44%)

9880 existing lines in 148 files now uncovered.

208900 of 283831 relevant lines covered (73.6%)

18073489.5 hits per line

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

69.0
/source/util/src/tqueue.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 "taoserror.h"
18
#include "tlog.h"
19
#include "tqueue.h"
20
#include "tutil.h"
21

22
int64_t tsQueueMemoryAllowed = 0;
23
int64_t tsQueueMemoryUsed = 0;
24

25
int64_t tsApplyMemoryAllowed = 0;
26
int64_t tsApplyMemoryUsed = 0;
27
struct STaosQueue {
28
  STaosQnode   *head;
29
  STaosQnode   *tail;
30
  STaosQueue   *next;     // for queue set
31
  STaosQset    *qset;     // for queue set
32
  void         *ahandle;  // for queue set
33
  FItem         itemFp;
34
  FItems        itemsFp;
35
  int32_t       numOfItems;
36
  int64_t       memOfItems;
37
  int64_t       threadId;
38
  int64_t       memLimit;
39
  int64_t       itemLimit;
40
  TdThreadMutex mutex;
41
};
42

43
struct STaosQset {
44
  STaosQueue   *head;
45
  STaosQueue   *current;
46
  TdThreadMutex mutex;
47
  tsem_t        sem;
48
  int32_t       numOfQueues;
49
  int32_t       numOfItems;
50
};
51

52
struct STaosQall {
53
  STaosQnode *current;
54
  STaosQnode *start;
55
  int32_t     numOfItems;
56
  int64_t     memOfItems;
57
  int32_t     unAccessedNumOfItems;
58
  int64_t     unAccessMemOfItems;
59
};
60

61
void taosSetQueueMemoryCapacity(STaosQueue *queue, int64_t cap) { queue->memLimit = cap; }
×
62
void taosSetQueueCapacity(STaosQueue *queue, int64_t size) { queue->itemLimit = size; }
×
63

64
int32_t taosOpenQueue(STaosQueue **queue) {
8,251,720✔
65
  *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
8,251,720!
66
  if (*queue == NULL) {
8,258,193!
67
    return terrno;
×
68
  }
69

70
  int32_t code = taosThreadMutexInit(&(*queue)->mutex, NULL);
8,258,193✔
71
  if (code) {
8,259,267!
72
    taosMemoryFreeClear(*queue);
×
73
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
74
  }
75

76
  uDebug("queue:%p is opened", queue);
8,259,267✔
77
  return 0;
8,253,043✔
78
}
79

80
void taosSetQueueFp(STaosQueue *queue, FItem itemFp, FItems itemsFp) {
191,770✔
81
  if (queue == NULL) return;
191,770!
82
  queue->itemFp = itemFp;
191,770✔
83
  queue->itemsFp = itemsFp;
191,770✔
84
}
85

86
void taosCloseQueue(STaosQueue *queue) {
8,257,672✔
87
  if (queue == NULL) return;
8,257,672✔
88
  STaosQnode *pTemp;
89
  STaosQset  *qset;
90

91
  (void)taosThreadMutexLock(&queue->mutex);
8,257,671✔
92
  STaosQnode *pNode = queue->head;
8,259,472✔
93
  queue->head = NULL;
8,259,472✔
94
  qset = queue->qset;
8,259,472✔
95
  (void)taosThreadMutexUnlock(&queue->mutex);
8,259,472✔
96

97
  if (queue->qset) {
8,259,681✔
98
    taosRemoveFromQset(qset, queue);
88,593✔
99
  }
100

101
  while (pNode) {
8,256,875✔
102
    pTemp = pNode;
117✔
103
    pNode = pNode->next;
117✔
104
    taosMemoryFree(pTemp);
117!
105
  }
106

107
  (void)taosThreadMutexDestroy(&queue->mutex);
8,256,758✔
108
  taosMemoryFree(queue);
8,254,693!
109

110
  uDebug("queue:%p is closed", queue);
8,258,146✔
111
}
112

113
bool taosQueueEmpty(STaosQueue *queue) {
30,746,478✔
114
  if (queue == NULL) return true;
30,746,478!
115

116
  bool empty = false;
30,746,478✔
117
  (void)taosThreadMutexLock(&queue->mutex);
30,746,478✔
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 /*&& queue->memOfItems == 0*/) {
30,784,859!
119
    empty = true;
10,938,258✔
120
  }
121
  (void)taosThreadMutexUnlock(&queue->mutex);
30,784,859✔
122

123
  return empty;
30,793,942✔
124
}
125

126
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
58,940,479✔
127
  if (queue == NULL) return;
58,940,479!
128

129
  (void)taosThreadMutexLock(&queue->mutex);
58,940,479✔
130
  queue->numOfItems -= items;
58,960,055✔
131
  (void)taosThreadMutexUnlock(&queue->mutex);
58,960,055✔
132
}
133

134
int32_t taosQueueItemSize(STaosQueue *queue) {
39,594,222✔
135
  if (queue == NULL) return 0;
39,594,222!
136

137
  (void)taosThreadMutexLock(&queue->mutex);
39,594,222✔
138
  int32_t numOfItems = queue->numOfItems;
39,605,303✔
139
  (void)taosThreadMutexUnlock(&queue->mutex);
39,605,303✔
140

141
  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
39,608,354✔
142
  return numOfItems;
39,610,458✔
143
}
144

145
int64_t taosQueueMemorySize(STaosQueue *queue) {
×
146
  (void)taosThreadMutexLock(&queue->mutex);
×
147
  int64_t memOfItems = queue->memOfItems;
×
148
  (void)taosThreadMutexUnlock(&queue->mutex);
×
149
  return memOfItems;
×
150
}
151

152
int32_t taosAllocateQitem(int32_t size, EQItype itype, int64_t dataSize, void **item) {
86,898,169✔
153
  int64_t alloced = -1;
86,898,169✔
154

155
  if (itype == RPC_QITEM) {
86,898,169✔
156
    alloced = atomic_add_fetch_64(&tsQueueMemoryUsed, size + dataSize);
52,715,596✔
157
    if (alloced > tsQueueMemoryAllowed) {
52,776,550!
158
      uError("failed to alloc qitem, size:%" PRId64 " alloc:%" PRId64 " allowed:%" PRId64, size + dataSize, alloced,
×
159
             tsQueueMemoryAllowed);
160
      (void)atomic_sub_fetch_64(&tsQueueMemoryUsed, size + dataSize);
×
161
      return (terrno = TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE);
×
162
    }
163
  } else if (itype == APPLY_QITEM) {
34,182,573✔
164
    alloced = atomic_add_fetch_64(&tsApplyMemoryUsed, size + dataSize);
4,088,850✔
165
    if (alloced > tsApplyMemoryAllowed) {
4,088,845!
166
      uDebug("failed to alloc qitem, size:%" PRId64 " alloc:%" PRId64 " allowed:%" PRId64, size + dataSize, alloced,
×
167
             tsApplyMemoryAllowed);
168
      (void)atomic_sub_fetch_64(&tsApplyMemoryUsed, size + dataSize);
×
169
      return (terrno = TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE);
×
170
    }
171
  }
172

173
  *item = NULL;
86,959,118✔
174
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
86,959,118✔
175
  if (pNode == NULL) {
86,925,756!
176
    if (itype == RPC_QITEM) {
×
177
      (void)atomic_sub_fetch_64(&tsQueueMemoryUsed, size + dataSize);
×
178
    } else if (itype == APPLY_QITEM) {
×
179
      (void)atomic_sub_fetch_64(&tsApplyMemoryUsed, size + dataSize);
×
180
    }
181
    return terrno;
×
182
  }
183

184
  pNode->dataSize = dataSize;
86,925,756✔
185
  pNode->size = size;
86,925,756✔
186
  pNode->itype = itype;
86,925,756✔
187
  pNode->timestamp = taosGetTimestampUs();
86,947,964✔
188
  uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
86,947,964✔
189
  *item = pNode->item;
86,973,750✔
190
  return 0;
86,973,750✔
191
}
192

193
void taosFreeQitem(void *pItem) {
86,959,941✔
194
  if (pItem == NULL) return;
86,959,941!
195

196
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
86,959,941✔
197
  int64_t     alloced = -1;
86,959,941✔
198
  if (pNode->itype == RPC_QITEM) {
86,959,941✔
199
    alloced = atomic_sub_fetch_64(&tsQueueMemoryUsed, pNode->size + pNode->dataSize);
52,771,916✔
200
  } else if (pNode->itype == APPLY_QITEM) {
34,188,025✔
201
    alloced = atomic_sub_fetch_64(&tsApplyMemoryUsed, pNode->size + pNode->dataSize);
4,088,845✔
202
  }
203
  uTrace("item:%p, node:%p is freed, alloc:%" PRId64, pItem, pNode, alloced);
86,970,589✔
204

205
  taosMemoryFree(pNode);
86,970,589✔
206
}
207

208
int32_t taosWriteQitem(STaosQueue *queue, void *pItem) {
86,883,340✔
209
  int32_t     code = 0;
86,883,340✔
210
  STaosQnode *pNode = (STaosQnode *)(((char *)pItem) - sizeof(STaosQnode));
86,883,340✔
211
  pNode->timestamp = taosGetTimestampUs();
86,937,789✔
212
  pNode->next = NULL;
86,937,789✔
213

214
  (void)taosThreadMutexLock(&queue->mutex);
86,937,789✔
215
  if (queue->memLimit > 0 && (queue->memOfItems + pNode->size + pNode->dataSize) > queue->memLimit) {
86,987,889!
216
    code = TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY;
×
217
    uError("item:%p, failed to put into queue:%p, queue mem limit:%" PRId64 ", reason:%s" PRId64, pItem, queue,
×
218
           queue->memLimit, tstrerror(code));
219

220
    (void)taosThreadMutexUnlock(&queue->mutex);
×
221
    return code;
×
222
  } else if (queue->itemLimit > 0 && queue->numOfItems + 1 > queue->itemLimit) {
86,987,889!
223
    code = TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY;
×
224
    uError("item:%p, failed to put into queue:%p, queue size limit:%" PRId64 ", reason:%s" PRId64, pItem, queue,
×
225
           queue->itemLimit, tstrerror(code));
226
    (void)taosThreadMutexUnlock(&queue->mutex);
×
227
    return code;
×
228
  }
229

230
  if (queue->tail) {
86,987,889✔
231
    queue->tail->next = pNode;
23,542,877✔
232
    queue->tail = pNode;
23,542,877✔
233
  } else {
234
    queue->head = pNode;
63,445,012✔
235
    queue->tail = pNode;
63,445,012✔
236
  }
237
  queue->numOfItems++;
86,987,889✔
238
  queue->memOfItems += (pNode->size + pNode->dataSize);
86,987,889✔
239
  if (queue->qset) {
86,987,889✔
240
    (void)atomic_add_fetch_32(&queue->qset->numOfItems, 1);
67,062,726✔
241
  }
242

243
  uTrace("item:%p, is put into queue:%p, items:%d mem:%" PRId64, pItem, queue, queue->numOfItems, queue->memOfItems);
87,025,319✔
244

245
  (void)taosThreadMutexUnlock(&queue->mutex);
87,025,319✔
246

247
  if (queue->qset) {
86,993,598✔
248
    if (tsem_post(&queue->qset->sem) != 0) {
67,096,136!
249
      uError("failed to post semaphore for queue set:%p", queue->qset);
×
250
    } else {
251
      uDebug("sem_post Qset %p, sem:%p", queue->qset, &queue->qset->sem);
67,085,575✔
252
    }
253
  } else {
254
    uDebug("empty qset");
19,897,462✔
255
  }
256
  return code;
86,975,156✔
257
}
258

259
void taosReadQitem(STaosQueue *queue, void **ppItem) {
20,105,880✔
260
  STaosQnode *pNode = NULL;
20,105,880✔
261

262
  (void)taosThreadMutexLock(&queue->mutex);
20,105,880✔
263

264
  if (queue->head) {
20,106,214✔
265
    pNode = queue->head;
19,855,251✔
266
    *ppItem = pNode->item;
19,855,251✔
267
    queue->head = pNode->next;
19,855,251✔
268
    if (queue->head == NULL) {
19,855,251✔
269
      queue->tail = NULL;
6,313,930✔
270
    }
271
    queue->numOfItems--;
19,855,251✔
272
    queue->memOfItems -= (pNode->size + pNode->dataSize);
19,855,251✔
273
    if (queue->qset) {
19,855,251!
UNCOV
274
      (void)atomic_sub_fetch_32(&queue->qset->numOfItems, 1);
×
275
    }
276
    uTrace("item:%p, is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems,
19,855,251✔
277
           queue->memOfItems);
278
  }
279

280
  (void)taosThreadMutexUnlock(&queue->mutex);
20,106,214✔
281
}
20,129,760✔
282

283
int32_t taosAllocateQall(STaosQall **qall) {
90,836✔
284
  *qall = taosMemoryCalloc(1, sizeof(STaosQall));
90,836!
285
  if (*qall == NULL) {
90,837!
UNCOV
286
    return terrno;
×
287
  }
288
  return 0;
90,837✔
289
}
290

291
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
90,837!
292

293
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
×
UNCOV
294
  int32_t numOfItems = 0;
×
295
  bool    empty;
296

297
  (void)taosThreadMutexLock(&queue->mutex);
×
298

299
  empty = queue->head == NULL;
×
300
  if (!empty) {
×
301
    memset(qall, 0, sizeof(STaosQall));
×
UNCOV
302
    qall->current = queue->head;
×
303
    qall->start = queue->head;
×
304
    qall->numOfItems = queue->numOfItems;
×
UNCOV
305
    qall->memOfItems = queue->memOfItems;
×
306

UNCOV
307
    qall->unAccessedNumOfItems = queue->numOfItems;
×
308
    qall->unAccessMemOfItems = queue->memOfItems;
×
309

310
    numOfItems = qall->numOfItems;
×
311

312
    queue->head = NULL;
×
UNCOV
313
    queue->tail = NULL;
×
314
    queue->numOfItems = 0;
×
315
    queue->memOfItems = 0;
×
UNCOV
316
    uTrace("read %d items from queue:%p, items:%d mem:%" PRId64, numOfItems, queue, queue->numOfItems,
×
317
           queue->memOfItems);
UNCOV
318
    if (queue->qset) {
×
319
      (void)atomic_sub_fetch_32(&queue->qset->numOfItems, qall->numOfItems);
×
320
    }
321
  }
322

323
  (void)taosThreadMutexUnlock(&queue->mutex);
×
324

325
  // if source queue is empty, we set destination qall to empty too.
UNCOV
326
  if (empty) {
×
327
    qall->current = NULL;
×
UNCOV
328
    qall->start = NULL;
×
UNCOV
329
    qall->numOfItems = 0;
×
330
  }
UNCOV
331
  return numOfItems;
×
332
}
333

334
int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
43,380,997✔
335
  STaosQnode *pNode;
336
  int32_t     num = 0;
43,380,997✔
337

338
  pNode = qall->current;
43,380,997✔
339
  if (pNode) qall->current = pNode->next;
43,380,997!
340

341
  if (pNode) {
43,380,997!
342
    *ppItem = pNode->item;
43,391,915✔
343
    num = 1;
43,391,915✔
344

345
    qall->unAccessedNumOfItems -= 1;
43,391,915✔
346
    qall->unAccessMemOfItems -= pNode->dataSize;
43,391,915✔
347

348
    uTrace("item:%p, is fetched", *ppItem);
43,391,915✔
349
  } else {
UNCOV
350
    *ppItem = NULL;
×
351
  }
352

353
  return num;
43,392,943✔
354
}
355

356
int32_t taosOpenQset(STaosQset **qset) {
173,397✔
357
  *qset = taosMemoryCalloc(sizeof(STaosQset), 1);
173,397!
358
  if (*qset == NULL) {
173,399!
UNCOV
359
    return terrno;
×
360
  }
361

362
  (void)taosThreadMutexInit(&(*qset)->mutex, NULL);
173,399✔
363
  if (tsem_init(&(*qset)->sem, 0, 0) != 0) {
173,399!
UNCOV
364
    taosMemoryFree(*qset);
×
UNCOV
365
    return terrno;
×
366
  }
367

368
  uDebug("qset:%p, is opened", qset);
173,398✔
369
  return 0;
173,396✔
370
}
371

372
void taosCloseQset(STaosQset *qset) {
173,400✔
373
  if (qset == NULL) return;
173,400✔
374

375
  // remove all the queues from qset
376
  (void)taosThreadMutexLock(&qset->mutex);
173,399✔
377
  while (qset->head) {
276,581✔
378
    STaosQueue *queue = qset->head;
103,180✔
379
    qset->head = qset->head->next;
103,180✔
380

381
    queue->qset = NULL;
103,180✔
382
    queue->next = NULL;
103,180✔
383
  }
384
  (void)taosThreadMutexUnlock(&qset->mutex);
173,401✔
385

386
  (void)taosThreadMutexDestroy(&qset->mutex);
173,401✔
387
  if (tsem_destroy(&qset->sem) != 0) {
173,400!
UNCOV
388
    uError("failed to destroy semaphore for qset:%p", qset);
×
389
  }
390
  taosMemoryFree(qset);
173,400!
391
  uDebug("qset:%p, is closed", qset);
173,401✔
392
}
393

394
// tsem_post 'qset->sem', so that reader threads waiting for it
395
// resumes execution and return, should only be used to signal the
396
// thread to exit.
397
void taosQsetThreadResume(STaosQset *qset) {
1,118,478✔
398
  uDebug("qset:%p, it will exit", qset);
1,118,478✔
399
  if (tsem_post(&qset->sem) != 0) {
1,118,478!
UNCOV
400
    uError("failed to post semaphore for qset:%p", qset);
×
401
  }
402
}
1,118,473✔
403

404
int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle) {
191,769✔
405
  if (queue->qset) return TSDB_CODE_INVALID_PARA;
191,769!
406

407
  (void)taosThreadMutexLock(&qset->mutex);
191,769✔
408

409
  queue->next = qset->head;
191,772✔
410
  queue->ahandle = ahandle;
191,772✔
411
  qset->head = queue;
191,772✔
412
  qset->numOfQueues++;
191,772✔
413

414
  (void)taosThreadMutexLock(&queue->mutex);
191,772✔
415
  (void)atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
191,771✔
416
  queue->qset = qset;
191,773✔
417
  (void)taosThreadMutexUnlock(&queue->mutex);
191,773✔
418

419
  (void)taosThreadMutexUnlock(&qset->mutex);
191,772✔
420

421
  uTrace("queue:%p, is added into qset:%p", queue, qset);
191,771✔
422
  return 0;
191,771✔
423
}
424

425
void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) {
88,591✔
426
  STaosQueue *tqueue = NULL;
88,591✔
427

428
  (void)taosThreadMutexLock(&qset->mutex);
88,591✔
429

430
  if (qset->head) {
88,592!
431
    if (qset->head == queue) {
88,592✔
432
      qset->head = qset->head->next;
81,200✔
433
      tqueue = queue;
81,200✔
434
    } else {
435
      STaosQueue *prev = qset->head;
7,392✔
436
      tqueue = qset->head->next;
7,392✔
437
      while (tqueue) {
25,589!
438
        if (tqueue == queue) {
25,589✔
439
          prev->next = tqueue->next;
7,392✔
440
          break;
7,392✔
441
        } else {
442
          prev = tqueue;
18,197✔
443
          tqueue = tqueue->next;
18,197✔
444
        }
445
      }
446
    }
447

448
    if (tqueue) {
88,592!
449
      if (qset->current == queue) qset->current = tqueue->next;
88,592✔
450
      qset->numOfQueues--;
88,592✔
451

452
      (void)taosThreadMutexLock(&queue->mutex);
88,592✔
453
      (void)atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems);
88,593✔
454
      queue->qset = NULL;
88,592✔
455
      queue->next = NULL;
88,592✔
456
      (void)taosThreadMutexUnlock(&queue->mutex);
88,592✔
457
    }
458
  }
459

460
  (void)taosThreadMutexUnlock(&qset->mutex);
88,593✔
461

462
  uDebug("queue:%p, is removed from qset:%p", queue, qset);
88,592✔
463
}
88,592✔
464

465
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, SQueueInfo *qinfo) {
24,716,390✔
466
  STaosQnode *pNode = NULL;
24,716,390✔
467
  int32_t     code = 0;
24,716,390✔
468

469
  uDebug("start to waitfromQset %p, sem:%p, idx:%d", qset, &qset->sem, qinfo->workerId);
24,716,390✔
470
  if (tsem_wait(&qset->sem) != 0) {
24,716,410!
UNCOV
471
    uError("failed to wait semaphore for qset:%p", qset);
×
472
  }
473
  uDebug("end waitfromQset %p, sem:%p, idx:%d", qset, &qset->sem, qinfo->workerId);
24,687,765✔
474

475
  (void)taosThreadMutexLock(&qset->mutex);
24,689,195✔
476

477
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
156,085,124✔
478
    if (qset->current == NULL) qset->current = qset->head;
155,056,884✔
479
    STaosQueue *queue = qset->current;
155,056,884✔
480
    if (queue) qset->current = queue->next;
155,056,884!
481
    if (queue == NULL) break;
155,056,884!
482
    if (queue->head == NULL) continue;
155,056,884✔
483

484
    (void)taosThreadMutexLock(&queue->mutex);
23,693,612✔
485

486
    if (queue->head) {
23,696,450!
487
      pNode = queue->head;
23,696,470✔
488
      *ppItem = pNode->item;
23,696,470✔
489
      qinfo->ahandle = queue->ahandle;
23,696,470✔
490
      qinfo->fp = queue->itemFp;
23,696,470✔
491
      qinfo->queue = queue;
23,696,470✔
492
      qinfo->timestamp = pNode->timestamp;
23,696,470✔
493

494
      queue->head = pNode->next;
23,696,470✔
495
      if (queue->head == NULL) queue->tail = NULL;
23,696,470✔
496
      // queue->numOfItems--;
497
      queue->memOfItems -= (pNode->size + pNode->dataSize);
23,696,470✔
498
      (void)atomic_sub_fetch_32(&qset->numOfItems, 1);
23,696,470✔
499
      code = 1;
23,696,487✔
500
      uTrace("item:%p, is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems - 1,
23,696,487✔
501
             queue->memOfItems);
502
    }
503

504
    (void)taosThreadMutexUnlock(&queue->mutex);
23,696,467✔
505
    if (pNode) break;
23,696,410!
506
  }
507

508
  (void)taosThreadMutexUnlock(&qset->mutex);
24,724,710✔
509

510
  return code;
24,719,100✔
511
}
512

513
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, SQueueInfo *qinfo) {
35,471,763✔
514
  STaosQueue *queue;
515
  int32_t     code = 0;
35,471,763✔
516

517
  if (tsem_wait(&qset->sem) != 0) {
35,471,763!
UNCOV
518
    uError("failed to wait semaphore for qset:%p", qset);
×
519
  }
520
  (void)taosThreadMutexLock(&qset->mutex);
35,462,901✔
521

522
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
79,920,692✔
523
    if (qset->current == NULL) qset->current = qset->head;
79,811,387✔
524
    queue = qset->current;
79,811,387✔
525
    if (queue) qset->current = queue->next;
79,811,387!
526
    if (queue == NULL) break;
79,811,387!
527
    if (queue->head == NULL) continue;
79,811,387✔
528

529
    (void)taosThreadMutexLock(&queue->mutex);
35,365,336✔
530

531
    if (queue->head) {
35,387,282!
532
      qall->current = queue->head;
35,387,659✔
533
      qall->start = queue->head;
35,387,659✔
534
      qall->numOfItems = queue->numOfItems;
35,387,659✔
535
      qall->memOfItems = queue->memOfItems;
35,387,659✔
536
      qall->unAccessedNumOfItems = queue->numOfItems;
35,387,659✔
537
      qall->unAccessMemOfItems = queue->memOfItems;
35,387,659✔
538

539
      code = qall->numOfItems;
35,387,659✔
540
      qinfo->ahandle = queue->ahandle;
35,387,659✔
541
      qinfo->fp = queue->itemsFp;
35,387,659✔
542
      qinfo->queue = queue;
35,387,659✔
543
      qinfo->timestamp = queue->head->timestamp;
35,387,659✔
544

545
      queue->head = NULL;
35,387,659✔
546
      queue->tail = NULL;
35,387,659✔
547
      // queue->numOfItems = 0;
548
      queue->memOfItems = 0;
35,387,659✔
549
      uTrace("read %d items from queue:%p, items:0 mem:%" PRId64, code, queue, queue->memOfItems);
35,387,659✔
550

551
      (void)atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
35,387,659✔
552
      for (int32_t j = 1; j < qall->numOfItems; ++j) {
43,408,102✔
553
        if (tsem_wait(&qset->sem) != 0) {
8,022,280!
UNCOV
554
          uError("failed to wait semaphore for qset:%p", qset);
×
555
        }
556
      }
557
    }
558

559
    (void)taosThreadMutexUnlock(&queue->mutex);
35,385,445✔
560

561
    if (code != 0) break;
35,386,637!
562
  }
563

564
  (void)taosThreadMutexUnlock(&qset->mutex);
35,496,612✔
565
  return code;
35,475,305✔
566
}
567

568
int32_t taosQallItemSize(STaosQall *qall) { return qall->numOfItems; }
×
569
int64_t taosQallMemSize(STaosQall *qall) { return qall->memOfItems; }
×
570

UNCOV
571
int64_t taosQallUnAccessedItemSize(STaosQall *qall) { return qall->unAccessedNumOfItems; }
×
UNCOV
572
int64_t taosQallUnAccessedMemSize(STaosQall *qall) { return qall->unAccessMemOfItems; }
×
573

UNCOV
574
void    taosResetQitems(STaosQall *qall) { qall->current = qall->start; }
×
UNCOV
575
int32_t taosGetQueueNumber(STaosQset *qset) { return qset->numOfQueues; }
×
576

577
void taosQueueSetThreadId(STaosQueue *pQueue, int64_t threadId) { pQueue->threadId = threadId; }
96,816✔
578

579
int64_t taosQueueGetThreadId(STaosQueue *pQueue) { return pQueue->threadId; }
172,597✔
580

581
#if 0
582

583
void taosResetQsetThread(STaosQset *qset, void *pItem) {
584
  if (pItem == NULL) return;
585
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
586

587
  (void)taosThreadMutexLock(&qset->mutex);
588
  for (int32_t i = 0; i < pNode->queue->numOfItems; ++i) {
589
    tsem_post(&qset->sem);
590
  }
591
  (void)taosThreadMutexUnlock(&qset->mutex);
592
}
593

594
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc