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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

75.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) {
1,024✔
65
  *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
1,024✔
66
  if (*queue == NULL) {
1,024✔
67
    return terrno;
×
68
  }
69

70
  int32_t code = taosThreadMutexInit(&(*queue)->mutex, NULL);
1,024✔
71
  if (code) {
1,024✔
72
    taosMemoryFreeClear(*queue);
×
73
    return (terrno = TAOS_SYSTEM_ERROR(code));
×
74
  }
75

76
  uDebug("queue:%p is opened", queue);
1,024✔
77
  return 0;
1,024✔
78
}
79

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

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

91
  (void)taosThreadMutexLock(&queue->mutex);
940✔
92
  STaosQnode *pNode = queue->head;
940✔
93
  queue->head = NULL;
940✔
94
  qset = queue->qset;
940✔
95
  (void)taosThreadMutexUnlock(&queue->mutex);
940✔
96

97
  if (queue->qset) {
940✔
98
    taosRemoveFromQset(qset, queue);
312✔
99
  }
100

101
  while (pNode) {
944✔
102
    pTemp = pNode;
4✔
103
    pNode = pNode->next;
4✔
104
    taosMemoryFree(pTemp);
4✔
105
  }
106

107
  (void)taosThreadMutexDestroy(&queue->mutex);
940✔
108
  taosMemoryFree(queue);
940✔
109

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

113
bool taosQueueEmpty(STaosQueue *queue) {
750✔
114
  if (queue == NULL) return true;
750✔
115

116
  bool empty = false;
750✔
117
  (void)taosThreadMutexLock(&queue->mutex);
750✔
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 /*&& queue->memOfItems == 0*/) {
750✔
119
    empty = true;
744✔
120
  }
121
  (void)taosThreadMutexUnlock(&queue->mutex);
750✔
122

123
  return empty;
750✔
124
}
125

126
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
15,024✔
127
  if (queue == NULL) return;
15,024✔
128

129
  (void)taosThreadMutexLock(&queue->mutex);
15,024✔
130
  queue->numOfItems -= items;
15,030✔
131
  (void)taosThreadMutexUnlock(&queue->mutex);
15,030✔
132
}
133

134
int32_t taosQueueItemSize(STaosQueue *queue) {
×
135
  if (queue == NULL) return 0;
×
136

137
  (void)taosThreadMutexLock(&queue->mutex);
×
138
  int32_t numOfItems = queue->numOfItems;
×
139
  (void)taosThreadMutexUnlock(&queue->mutex);
×
140

141
  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
×
142
  return numOfItems;
×
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) {
16,648✔
153
  int64_t alloced = -1;
16,648✔
154

155
  if (itype == RPC_QITEM) {
16,648✔
156
    alloced = atomic_add_fetch_64(&tsQueueMemoryUsed, size + dataSize);
2,400✔
157
    if (alloced > tsQueueMemoryAllowed) {
2,400✔
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) {
14,248✔
164
    alloced = atomic_add_fetch_64(&tsApplyMemoryUsed, size + dataSize);
60✔
165
    if (alloced > tsApplyMemoryAllowed) {
60✔
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;
16,648✔
174
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
16,648✔
175
  if (pNode == NULL) {
16,655✔
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;
16,655✔
185
  pNode->size = size;
16,655✔
186
  pNode->itype = itype;
16,655✔
187
  pNode->timestamp = taosGetTimestampUs();
16,653✔
188
  uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
16,653✔
189
  *item = pNode->item;
16,655✔
190
  return 0;
16,655✔
191
}
192

193
void taosFreeQitem(void *pItem) {
16,649✔
194
  if (pItem == NULL) return;
16,649✔
195

196
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
16,649✔
197
  int64_t     alloced = -1;
16,649✔
198
  if (pNode->itype == RPC_QITEM) {
16,649✔
199
    alloced = atomic_sub_fetch_64(&tsQueueMemoryUsed, pNode->size + pNode->dataSize);
2,400✔
200
  } else if (pNode->itype == APPLY_QITEM) {
14,249✔
201
    alloced = atomic_sub_fetch_64(&tsApplyMemoryUsed, pNode->size + pNode->dataSize);
60✔
202
  }
203
  uTrace("item:%p, node:%p is freed, alloc:%" PRId64, pItem, pNode, alloced);
16,649✔
204

205
  taosMemoryFree(pNode);
16,649✔
206
}
207

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

214
  (void)taosThreadMutexLock(&queue->mutex);
16,606✔
215
  if (queue->memLimit > 0 && (queue->memOfItems + pNode->size + pNode->dataSize) > queue->memLimit) {
16,609✔
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) {
16,609✔
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) {
16,609✔
231
    queue->tail->next = pNode;
1,932✔
232
    queue->tail = pNode;
1,932✔
233
  } else {
234
    queue->head = pNode;
14,677✔
235
    queue->tail = pNode;
14,677✔
236
  }
237
  queue->numOfItems++;
16,609✔
238
  queue->memOfItems += (pNode->size + pNode->dataSize);
16,609✔
239
  if (queue->qset) {
16,609✔
240
    (void)atomic_add_fetch_32(&queue->qset->numOfItems, 1);
15,346✔
241
  }
242

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

245
  (void)taosThreadMutexUnlock(&queue->mutex);
16,609✔
246

247
  if (queue->qset) {
16,609✔
248
    if (tsem_post(&queue->qset->sem) != 0) {
15,346✔
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);
15,342✔
252
    }
253
  } else {
254
    uDebug("empty qset");
1,263✔
255
  }
256
  return code;
16,602✔
257
}
258

259
void taosReadQitem(STaosQueue *queue, void **ppItem) {
15,150✔
260
  STaosQnode *pNode = NULL;
15,150✔
261

262
  (void)taosThreadMutexLock(&queue->mutex);
15,150✔
263

264
  if (queue->head) {
15,150✔
265
    pNode = queue->head;
1,259✔
266
    *ppItem = pNode->item;
1,259✔
267
    queue->head = pNode->next;
1,259✔
268
    if (queue->head == NULL) {
1,259✔
269
      queue->tail = NULL;
661✔
270
    }
271
    queue->numOfItems--;
1,259✔
272
    queue->memOfItems -= (pNode->size + pNode->dataSize);
1,259✔
273
    if (queue->qset) {
1,259✔
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,
1,259✔
277
           queue->memOfItems);
278
  }
279

280
  (void)taosThreadMutexUnlock(&queue->mutex);
15,150✔
281
}
15,150✔
282

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

291
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
282✔
292

293
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
×
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));
×
302
    qall->current = queue->head;
×
303
    qall->start = queue->head;
×
304
    qall->numOfItems = queue->numOfItems;
×
305
    qall->memOfItems = queue->memOfItems;
×
306

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

310
    numOfItems = qall->numOfItems;
×
311

312
    queue->head = NULL;
×
313
    queue->tail = NULL;
×
314
    queue->numOfItems = 0;
×
315
    queue->memOfItems = 0;
×
316
    uTrace("read %d items from queue:%p, items:%d mem:%" PRId64, numOfItems, queue, queue->numOfItems,
×
317
           queue->memOfItems);
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.
326
  if (empty) {
×
327
    qall->current = NULL;
×
328
    qall->start = NULL;
×
329
    qall->numOfItems = 0;
×
330
  }
331
  return numOfItems;
×
332
}
333

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

338
  pNode = qall->current;
181✔
339
  if (pNode) qall->current = pNode->next;
181✔
340

341
  if (pNode) {
181✔
342
    *ppItem = pNode->item;
181✔
343
    num = 1;
181✔
344

345
    qall->unAccessedNumOfItems -= 1;
181✔
346
    qall->unAccessMemOfItems -= pNode->dataSize;
181✔
347

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

353
  return num;
181✔
354
}
355

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

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

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

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

375
  // remove all the queues from qset
376
  (void)taosThreadMutexLock(&qset->mutex);
844✔
377
  while (qset->head) {
1,490✔
378
    STaosQueue *queue = qset->head;
646✔
379
    qset->head = qset->head->next;
646✔
380

381
    queue->qset = NULL;
646✔
382
    queue->next = NULL;
646✔
383
  }
384
  (void)taosThreadMutexUnlock(&qset->mutex);
844✔
385

386
  (void)taosThreadMutexDestroy(&qset->mutex);
844✔
387
  if (tsem_destroy(&qset->sem) != 0) {
844✔
388
    uError("failed to destroy semaphore for qset:%p", qset);
×
389
  }
390
  taosMemoryFree(qset);
844✔
391
  uDebug("qset:%p, is closed", qset);
844✔
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) {
7,004✔
398
  uDebug("qset:%p, it will exit", qset);
7,004✔
399
  if (tsem_post(&qset->sem) != 0) {
7,004✔
400
    uError("failed to post semaphore for qset:%p", qset);
×
401
  }
402
}
7,004✔
403

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

407
  (void)taosThreadMutexLock(&qset->mutex);
958✔
408

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

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

419
  (void)taosThreadMutexUnlock(&qset->mutex);
958✔
420

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

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

428
  (void)taosThreadMutexLock(&qset->mutex);
312✔
429

430
  if (qset->head) {
312✔
431
    if (qset->head == queue) {
312✔
432
      qset->head = qset->head->next;
282✔
433
      tqueue = queue;
282✔
434
    } else {
435
      STaosQueue *prev = qset->head;
30✔
436
      tqueue = qset->head->next;
30✔
437
      while (tqueue) {
30✔
438
        if (tqueue == queue) {
30✔
439
          prev->next = tqueue->next;
30✔
440
          break;
30✔
441
        } else {
442
          prev = tqueue;
×
443
          tqueue = tqueue->next;
×
444
        }
445
      }
446
    }
447

448
    if (tqueue) {
312✔
449
      if (qset->current == queue) qset->current = tqueue->next;
312✔
450
      qset->numOfQueues--;
312✔
451

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

460
  (void)taosThreadMutexUnlock(&qset->mutex);
312✔
461

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

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

469
  uDebug("start to waitfromQset %p, sem:%p, idx:%d", qset, &qset->sem, qinfo->workerId);
21,831✔
470
  if (tsem_wait(&qset->sem) != 0) {
21,831✔
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);
21,826✔
474

475
  (void)taosThreadMutexLock(&qset->mutex);
21,826✔
476

477
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
28,417✔
478
    if (qset->current == NULL) qset->current = qset->head;
21,695✔
479
    STaosQueue *queue = qset->current;
21,695✔
480
    if (queue) qset->current = queue->next;
21,695✔
481
    if (queue == NULL) break;
21,695✔
482
    if (queue->head == NULL) continue;
21,695✔
483

484
    (void)taosThreadMutexLock(&queue->mutex);
15,165✔
485

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

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

504
    (void)taosThreadMutexUnlock(&queue->mutex);
15,165✔
505
    if (pNode) break;
15,165✔
506
  }
507

508
  (void)taosThreadMutexUnlock(&qset->mutex);
21,887✔
509

510
  return code;
21,881✔
511
}
512

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

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

522
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
703✔
523
    if (qset->current == NULL) qset->current = qset->head;
421✔
524
    queue = qset->current;
421✔
525
    if (queue) qset->current = queue->next;
421✔
526
    if (queue == NULL) break;
421✔
527
    if (queue->head == NULL) continue;
421✔
528

529
    (void)taosThreadMutexLock(&queue->mutex);
181✔
530

531
    if (queue->head) {
181✔
532
      qall->current = queue->head;
181✔
533
      qall->start = queue->head;
181✔
534
      qall->numOfItems = queue->numOfItems;
181✔
535
      qall->memOfItems = queue->memOfItems;
181✔
536
      qall->unAccessedNumOfItems = queue->numOfItems;
181✔
537
      qall->unAccessMemOfItems = queue->memOfItems;
181✔
538

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

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

551
      (void)atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
181✔
552

553
      STaosQnode *pNode = qall->start->next;
181✔
554
      // skip the first node, tsem_wait has been called once at the beginning of this function
555
      // queue->numOfItems is not entirely reliable and may be larger than the actual value due to concurrency issues.
556
      while (pNode) {
181✔
557
        if (tsem_wait(&qset->sem) != 0) {
×
558
          uError("failed to wait semaphore for qset:%p", qset);
×
559
        }
560
        pNode = pNode->next;
×
561
      }
562
    }
563

564
    (void)taosThreadMutexUnlock(&queue->mutex);
181✔
565

566
    if (code != 0) break;
181✔
567
  }
568

569
  (void)taosThreadMutexUnlock(&qset->mutex);
463✔
570
  return code;
463✔
571
}
572

573
int32_t taosQallItemSize(STaosQall *qall) { return qall->numOfItems; }
×
574
int64_t taosQallMemSize(STaosQall *qall) { return qall->memOfItems; }
×
575

576
int64_t taosQallUnAccessedItemSize(STaosQall *qall) { return qall->unAccessedNumOfItems; }
×
577
int64_t taosQallUnAccessedMemSize(STaosQall *qall) { return qall->unAccessMemOfItems; }
×
578

579
void    taosResetQitems(STaosQall *qall) { qall->current = qall->start; }
×
580
int32_t taosGetQueueNumber(STaosQset *qset) { return qset->numOfQueues; }
×
581

582
void taosQueueSetThreadId(STaosQueue *pQueue, int64_t threadId) { pQueue->threadId = threadId; }
324✔
583

584
int64_t taosQueueGetThreadId(STaosQueue *pQueue) { return pQueue->threadId; }
600✔
585

586
#if 0
587

588
void taosResetQsetThread(STaosQset *qset, void *pItem) {
589
  if (pItem == NULL) return;
590
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
591

592
  (void)taosThreadMutexLock(&qset->mutex);
593
  for (int32_t i = 0; i < pNode->queue->numOfItems; ++i) {
594
    tsem_post(&qset->sem);
595
  }
596
  (void)taosThreadMutexUnlock(&qset->mutex);
597
}
598

599
#endif
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