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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

69.25
/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) {
254,302,498✔
65
  *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
254,302,498✔
66
  if (*queue == NULL) {
254,106,093!
67
    return terrno;
×
68
  }
69

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

76
  uDebug("queue:%p is opened", queue);
254,193,926✔
77
  return 0;
254,305,665✔
78
}
79

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

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

91
  (void)taosThreadMutexLock(&queue->mutex);
253,489,432✔
92
  STaosQnode *pNode = queue->head;
253,524,350✔
93
  queue->head = NULL;
253,522,729✔
94
  qset = queue->qset;
253,515,090✔
95
  (void)taosThreadMutexUnlock(&queue->mutex);
253,488,002✔
96

97
  if (queue->qset) {
253,525,376✔
98
    taosRemoveFromQset(qset, queue);
24,351,740✔
99
  }
100

101
  while (pNode) {
253,532,489✔
102
    pTemp = pNode;
47,859✔
103
    pNode = pNode->next;
47,859✔
104
    taosMemoryFree(pTemp);
47,859!
105
  }
106

107
  (void)taosThreadMutexDestroy(&queue->mutex);
253,484,630✔
108
  taosMemoryFree(queue);
253,485,582✔
109

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

113
bool taosQueueEmpty(STaosQueue *queue) {
757,875,200✔
114
  if (queue == NULL) return true;
757,875,200!
115

116
  bool empty = false;
757,875,200✔
117
  (void)taosThreadMutexLock(&queue->mutex);
757,875,200✔
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 /*&& queue->memOfItems == 0*/) {
757,951,439!
119
    empty = true;
326,483,752✔
120
  }
121
  (void)taosThreadMutexUnlock(&queue->mutex);
757,950,548✔
122

123
  return empty;
757,949,674✔
124
}
125

126
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
2,147,483,647✔
127
  if (queue == NULL) return;
2,147,483,647!
128

129
  (void)taosThreadMutexLock(&queue->mutex);
2,147,483,647✔
130
  queue->numOfItems -= items;
2,147,483,647✔
131
  (void)taosThreadMutexUnlock(&queue->mutex);
2,147,483,647✔
132
}
133

134
int32_t taosQueueItemSize(STaosQueue *queue) {
871,002,007✔
135
  if (queue == NULL) return 0;
871,002,007!
136

137
  (void)taosThreadMutexLock(&queue->mutex);
871,002,007✔
138
  int32_t numOfItems = queue->numOfItems;
871,056,769✔
139
  (void)taosThreadMutexUnlock(&queue->mutex);
871,058,169✔
140

141
  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
871,047,130✔
142
  return numOfItems;
871,024,812✔
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) {
2,147,483,647✔
153
  int64_t alloced = -1;
2,147,483,647✔
154

155
  if (itype == RPC_QITEM) {
2,147,483,647✔
156
    alloced = atomic_add_fetch_64(&tsQueueMemoryUsed, size + dataSize);
2,078,653,705✔
157
    if (alloced > tsQueueMemoryAllowed) {
2,078,675,830!
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) {
2,147,483,647✔
164
    alloced = atomic_add_fetch_64(&tsApplyMemoryUsed, size + dataSize);
49,166,415✔
165
    if (alloced > tsApplyMemoryAllowed) {
49,166,415!
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;
2,147,483,647✔
174
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
2,147,483,647✔
175
  if (pNode == NULL) {
2,147,483,647!
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;
2,147,483,647✔
185
  pNode->size = size;
2,147,483,647✔
186
  pNode->itype = itype;
2,147,483,647✔
187
  pNode->timestamp = taosGetTimestampUs();
2,147,483,647✔
188
  uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
2,147,483,647✔
189
  *item = pNode->item;
2,147,483,647✔
190
  return 0;
2,147,483,647✔
191
}
192

193
void taosFreeQitem(void *pItem) {
2,147,483,647✔
194
  if (pItem == NULL) return;
2,147,483,647!
195

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

205
  taosMemoryFree(pNode);
2,147,483,647✔
206
}
207

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

214
  (void)taosThreadMutexLock(&queue->mutex);
2,147,483,647✔
215
  if (queue->memLimit > 0 && (queue->memOfItems + pNode->size + pNode->dataSize) > queue->memLimit) {
2,147,483,647!
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) {
2,147,483,647!
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) {
2,147,483,647✔
231
    queue->tail->next = pNode;
407,099,113✔
232
    queue->tail = pNode;
407,097,908✔
233
  } else {
234
    queue->head = pNode;
2,147,483,647✔
235
    queue->tail = pNode;
2,147,483,647✔
236
  }
237
  queue->numOfItems++;
2,147,483,647✔
238
  queue->memOfItems += (pNode->size + pNode->dataSize);
2,147,483,647✔
239
  if (queue->qset) {
2,147,483,647✔
240
    (void)atomic_add_fetch_32(&queue->qset->numOfItems, 1);
2,147,483,647✔
241
  }
242

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

245
  (void)taosThreadMutexUnlock(&queue->mutex);
2,147,483,647✔
246

247
  if (queue->qset) {
2,147,483,647✔
248
    if (tsem_post(&queue->qset->sem) != 0) {
2,147,483,647!
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);
2,147,483,647✔
252
    }
253
  } else {
254
    uDebug("empty qset");
433,159,350✔
255
  }
256
  return code;
2,147,483,647✔
257
}
258

259
void taosReadQitem(STaosQueue *queue, void **ppItem) {
723,932,198✔
260
  STaosQnode *pNode = NULL;
723,932,198✔
261

262
  (void)taosThreadMutexLock(&queue->mutex);
723,932,198✔
263

264
  if (queue->head) {
723,955,523✔
265
    pNode = queue->head;
433,115,483✔
266
    *ppItem = pNode->item;
433,114,856✔
267
    queue->head = pNode->next;
433,116,612✔
268
    if (queue->head == NULL) {
433,123,738✔
269
      queue->tail = NULL;
201,642,870✔
270
    }
271
    queue->numOfItems--;
433,118,124✔
272
    queue->memOfItems -= (pNode->size + pNode->dataSize);
433,118,939✔
273
    if (queue->qset) {
433,117,212!
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,
433,113,148✔
277
           queue->memOfItems);
278
  }
279

280
  (void)taosThreadMutexUnlock(&queue->mutex);
723,925,730✔
281
}
723,964,364✔
282

283
int32_t taosAllocateQall(STaosQall **qall) {
19,367,897✔
284
  *qall = taosMemoryCalloc(1, sizeof(STaosQall));
19,367,897!
285
  if (*qall == NULL) {
19,367,266!
286
    return terrno;
×
287
  }
288
  return 0;
19,368,078✔
289
}
290

291
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
19,367,893!
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) {
1,465,748,955✔
335
  STaosQnode *pNode;
336
  int32_t     num = 0;
1,465,748,955✔
337

338
  pNode = qall->current;
1,465,748,955✔
339
  if (pNode) qall->current = pNode->next;
1,465,765,827✔
340

341
  if (pNode) {
1,465,782,658!
342
    *ppItem = pNode->item;
1,465,782,658✔
343
    num = 1;
1,465,775,443✔
344

345
    qall->unAccessedNumOfItems -= 1;
1,465,775,443✔
346
    qall->unAccessMemOfItems -= pNode->dataSize;
1,465,825,504✔
347

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

353
  return num;
1,465,812,197✔
354
}
355

356
int32_t taosOpenQset(STaosQset **qset) {
40,785,403✔
357
  *qset = taosMemoryCalloc(sizeof(STaosQset), 1);
40,785,403!
358
  if (*qset == NULL) {
40,786,057!
359
    return terrno;
×
360
  }
361

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

368
  uDebug("qset:%p, is opened", qset);
40,785,294✔
369
  return 0;
40,786,057✔
370
}
371

372
void taosCloseQset(STaosQset *qset) {
40,786,500✔
373
  if (qset == NULL) return;
40,786,500✔
374

375
  // remove all the queues from qset
376
  (void)taosThreadMutexLock(&qset->mutex);
40,786,057✔
377
  while (qset->head) {
64,891,175✔
378
    STaosQueue *queue = qset->head;
24,105,118✔
379
    qset->head = qset->head->next;
24,105,118✔
380

381
    queue->qset = NULL;
24,105,118✔
382
    queue->next = NULL;
24,105,118✔
383
  }
384
  (void)taosThreadMutexUnlock(&qset->mutex);
40,786,057✔
385

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

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

407
  (void)taosThreadMutexLock(&qset->mutex);
48,456,095✔
408

409
  queue->next = qset->head;
48,456,858✔
410
  queue->ahandle = ahandle;
48,456,095✔
411
  qset->head = queue;
48,456,858✔
412
  qset->numOfQueues++;
48,456,095✔
413

414
  (void)taosThreadMutexLock(&queue->mutex);
48,456,174✔
415
  (void)atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
48,456,858✔
416
  queue->qset = qset;
48,455,914✔
417
  (void)taosThreadMutexUnlock(&queue->mutex);
48,455,914✔
418

419
  (void)taosThreadMutexUnlock(&qset->mutex);
48,456,858✔
420

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

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

428
  (void)taosThreadMutexLock(&qset->mutex);
24,350,029✔
429

430
  if (qset->head) {
24,351,740!
431
    if (qset->head == queue) {
24,351,740✔
432
      qset->head = qset->head->next;
21,218,565✔
433
      tqueue = queue;
21,218,565✔
434
    } else {
435
      STaosQueue *prev = qset->head;
3,133,175✔
436
      tqueue = qset->head->next;
3,133,175✔
437
      while (tqueue) {
8,927,324!
438
        if (tqueue == queue) {
8,927,324✔
439
          prev->next = tqueue->next;
3,133,175✔
440
          break;
3,133,175✔
441
        } else {
442
          prev = tqueue;
5,794,149✔
443
          tqueue = tqueue->next;
5,794,149✔
444
        }
445
      }
446
    }
447

448
    if (tqueue) {
24,351,740✔
449
      if (qset->current == queue) qset->current = tqueue->next;
24,350,733✔
450
      qset->numOfQueues--;
24,350,733✔
451

452
      (void)taosThreadMutexLock(&queue->mutex);
24,350,928✔
453
      (void)atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems);
24,351,300✔
454
      queue->qset = NULL;
24,351,740✔
455
      queue->next = NULL;
24,350,851✔
456
      (void)taosThreadMutexUnlock(&queue->mutex);
24,350,851✔
457
    }
458
  }
459

460
  (void)taosThreadMutexUnlock(&qset->mutex);
24,352,747✔
461

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

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

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

475
  (void)taosThreadMutexLock(&qset->mutex);
2,147,483,647✔
476

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

484
    (void)taosThreadMutexLock(&queue->mutex);
2,147,483,647✔
485

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

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

504
    (void)taosThreadMutexUnlock(&queue->mutex);
2,147,483,647✔
505
    if (pNode) break;
2,147,483,647!
506
  }
507

508
  (void)taosThreadMutexUnlock(&qset->mutex);
2,147,483,647✔
509

510
  return code;
2,147,483,647✔
511
}
512

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

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

522
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
1,462,218,000✔
523
    if (qset->current == NULL) qset->current = qset->head;
1,442,837,888✔
524
    queue = qset->current;
1,442,822,539✔
525
    if (queue) qset->current = queue->next;
1,442,826,529✔
526
    if (queue == NULL) break;
1,442,801,131!
527
    if (queue->head == NULL) continue;
1,442,801,131✔
528

529
    (void)taosThreadMutexLock(&queue->mutex);
1,407,405,757✔
530

531
    if (queue->head) {
1,407,451,010✔
532
      qall->current = queue->head;
1,407,399,970✔
533
      qall->start = queue->head;
1,407,431,224✔
534
      qall->numOfItems = queue->numOfItems;
1,407,375,824✔
535
      qall->memOfItems = queue->memOfItems;
1,407,402,934✔
536
      qall->unAccessedNumOfItems = queue->numOfItems;
1,407,417,249✔
537
      qall->unAccessMemOfItems = queue->memOfItems;
1,407,413,094✔
538

539
      code = qall->numOfItems;
1,407,423,230✔
540
      qinfo->ahandle = queue->ahandle;
1,407,416,463✔
541
      qinfo->fp = queue->itemsFp;
1,407,425,368✔
542
      qinfo->queue = queue;
1,407,390,545✔
543
      qinfo->timestamp = queue->head->timestamp;
1,407,392,303✔
544

545
      queue->head = NULL;
1,407,442,615✔
546
      queue->tail = NULL;
1,407,440,405✔
547
      // queue->numOfItems = 0;
548
      queue->memOfItems = 0;
1,407,408,417✔
549
      uTrace("read %d items from queue:%p, items:0 mem:%" PRId64, code, queue, queue->memOfItems);
1,407,443,599✔
550

551
      (void)atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
1,407,443,599✔
552

553
      STaosQnode *pNode = qall->start->next;
1,407,442,882✔
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) {
1,465,860,763✔
557
        if (tsem_wait(&qset->sem) != 0) {
58,420,706!
558
          uError("failed to wait semaphore for qset:%p", qset);
×
559
        }
560
        pNode = pNode->next;
58,420,426✔
561
      }
562
    }
563

564
    (void)taosThreadMutexUnlock(&queue->mutex);
1,407,491,734✔
565

566
    if (code != 0) break;
1,407,434,512!
567
  }
568

569
  (void)taosThreadMutexUnlock(&qset->mutex);
1,426,790,326✔
570
  return code;
1,426,805,155✔
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; }
20,516,586✔
583

584
int64_t taosQueueGetThreadId(STaosQueue *pQueue) { return pQueue->threadId; }
39,954,570✔
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