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

taosdata / TDengine / #4865

26 Nov 2025 05:46AM UTC coverage: 64.495% (-0.05%) from 64.548%
#4865

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

767 of 945 new or added lines in 33 files covered. (81.16%)

3088 existing lines in 119 files now uncovered.

158096 of 245129 relevant lines covered (64.5%)

111671620.5 hits per line

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

78.27
/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) {
225,519,918✔
65
  *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
225,519,918✔
66
  if (*queue == NULL) {
225,271,703✔
67
    return terrno;
×
68
  }
69

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

76
  uDebug("queue:%p is opened", queue);
225,351,749✔
77
  return 0;
225,509,276✔
78
}
79

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

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

91
  (void)taosThreadMutexLock(&queue->mutex);
224,266,747✔
92
  STaosQnode *pNode = queue->head;
224,288,753✔
93
  queue->head = NULL;
224,288,691✔
94
  qset = queue->qset;
224,283,914✔
95
  (void)taosThreadMutexUnlock(&queue->mutex);
224,267,905✔
96

97
  if (queue->qset) {
224,284,364✔
98
    taosRemoveFromQset(qset, queue);
23,196,641✔
99
  }
100

101
  while (pNode) {
224,347,887✔
102
    pTemp = pNode;
103,602✔
103
    pNode = pNode->next;
103,602✔
104
    taosMemoryFree(pTemp);
103,602✔
105
  }
106

107
  (void)taosThreadMutexDestroy(&queue->mutex);
224,244,285✔
108
  taosMemoryFree(queue);
224,251,179✔
109

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

113
bool taosQueueEmpty(STaosQueue *queue) {
748,127,136✔
114
  if (queue == NULL) return true;
748,127,136✔
115

116
  bool empty = false;
748,127,136✔
117
  (void)taosThreadMutexLock(&queue->mutex);
748,127,136✔
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 /*&& queue->memOfItems == 0*/) {
748,136,886✔
119
    empty = true;
299,285,874✔
120
  }
121
  (void)taosThreadMutexUnlock(&queue->mutex);
748,137,242✔
122

123
  return empty;
748,181,044✔
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) {
915,338,230✔
135
  if (queue == NULL) return 0;
915,338,230✔
136

137
  (void)taosThreadMutexLock(&queue->mutex);
915,338,230✔
138
  int32_t numOfItems = queue->numOfItems;
915,396,592✔
139
  (void)taosThreadMutexUnlock(&queue->mutex);
915,399,027✔
140

141
  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
915,396,008✔
142
  return numOfItems;
915,361,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) {
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);
1,970,571,585✔
157
    if (alloced > tsQueueMemoryAllowed) {
1,970,572,325✔
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);
89,841,461✔
165
    if (alloced > tsApplyMemoryAllowed) {
89,841,461✔
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);
1,970,578,712✔
200
  } else if (pNode->itype == APPLY_QITEM) {
2,147,483,647✔
201
    alloced = atomic_sub_fetch_64(&tsApplyMemoryUsed, pNode->size + pNode->dataSize);
89,841,059✔
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;
433,981,327✔
232
    queue->tail = pNode;
433,978,234✔
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");
451,454,665✔
255
  }
256
  return code;
2,147,483,647✔
257
}
258

259
void taosReadQitem(STaosQueue *queue, void **ppItem) {
789,092,426✔
260
  STaosQnode *pNode = NULL;
789,092,426✔
261

262
  (void)taosThreadMutexLock(&queue->mutex);
789,092,426✔
263

264
  if (queue->head) {
789,165,258✔
265
    pNode = queue->head;
451,293,183✔
266
    *ppItem = pNode->item;
451,293,698✔
267
    queue->head = pNode->next;
451,286,411✔
268
    if (queue->head == NULL) {
451,307,973✔
269
      queue->tail = NULL;
182,463,203✔
270
    }
271
    queue->numOfItems--;
451,286,707✔
272
    queue->memOfItems -= (pNode->size + pNode->dataSize);
451,293,282✔
273
    if (queue->qset) {
451,309,736✔
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,
451,285,652✔
277
           queue->memOfItems);
278
  }
279

280
  (void)taosThreadMutexUnlock(&queue->mutex);
789,142,873✔
281
}
789,162,926✔
282

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

291
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
20,235,209✔
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,421,241,953✔
335
  STaosQnode *pNode;
336
  int32_t     num = 0;
1,421,241,953✔
337

338
  pNode = qall->current;
1,421,241,953✔
339
  if (pNode) qall->current = pNode->next;
1,421,267,190✔
340

341
  if (pNode) {
1,421,272,820✔
342
    *ppItem = pNode->item;
1,421,272,820✔
343
    num = 1;
1,421,272,135✔
344

345
    qall->unAccessedNumOfItems -= 1;
1,421,272,135✔
346
    qall->unAccessMemOfItems -= pNode->dataSize;
1,421,246,293✔
347

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

353
  return num;
1,421,227,520✔
354
}
355

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

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

368
  uDebug("qset:%p, is opened", qset);
39,606,873✔
369
  return 0;
39,608,373✔
370
}
371

372
void taosCloseQset(STaosQset *qset) {
39,609,234✔
373
  if (qset == NULL) return;
39,609,234✔
374

375
  // remove all the queues from qset
376
  (void)taosThreadMutexLock(&qset->mutex);
39,608,949✔
377
  while (qset->head) {
64,555,276✔
378
    STaosQueue *queue = qset->head;
24,946,327✔
379
    qset->head = qset->head->next;
24,946,327✔
380

381
    queue->qset = NULL;
24,946,327✔
382
    queue->next = NULL;
24,946,327✔
383
  }
384
  (void)taosThreadMutexUnlock(&qset->mutex);
39,608,449✔
385

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

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

407
  (void)taosThreadMutexLock(&qset->mutex);
48,142,465✔
408

409
  queue->next = qset->head;
48,141,441✔
410
  queue->ahandle = ahandle;
48,142,968✔
411
  qset->head = queue;
48,141,513✔
412
  qset->numOfQueues++;
48,142,308✔
413

414
  (void)taosThreadMutexLock(&queue->mutex);
48,140,721✔
415
  (void)atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
48,142,968✔
416
  queue->qset = qset;
48,142,968✔
417
  (void)taosThreadMutexUnlock(&queue->mutex);
48,142,968✔
418

419
  (void)taosThreadMutexUnlock(&qset->mutex);
48,142,968✔
420

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

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

428
  (void)taosThreadMutexLock(&qset->mutex);
23,196,641✔
429

430
  if (qset->head) {
23,196,641✔
431
    if (qset->head == queue) {
23,196,641✔
432
      qset->head = qset->head->next;
19,680,142✔
433
      tqueue = queue;
19,680,142✔
434
    } else {
435
      STaosQueue *prev = qset->head;
3,516,499✔
436
      tqueue = qset->head->next;
3,516,499✔
437
      while (tqueue) {
11,294,430✔
438
        if (tqueue == queue) {
11,294,430✔
439
          prev->next = tqueue->next;
3,516,499✔
440
          break;
3,516,499✔
441
        } else {
442
          prev = tqueue;
7,777,931✔
443
          tqueue = tqueue->next;
7,777,931✔
444
        }
445
      }
446
    }
447

448
    if (tqueue) {
23,196,641✔
449
      if (qset->current == queue) qset->current = tqueue->next;
23,196,641✔
450
      qset->numOfQueues--;
23,196,641✔
451

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

460
  (void)taosThreadMutexUnlock(&qset->mutex);
23,196,641✔
461

462
  uDebug("queue:%p, is removed from qset:%p", queue, qset);
23,196,641✔
463
}
23,196,641✔
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,388,392,725✔
514
  STaosQueue *queue;
515
  int32_t     code = 0;
1,388,392,725✔
516

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

522
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
1,418,749,164✔
523
    if (qset->current == NULL) qset->current = qset->head;
1,398,503,908✔
524
    queue = qset->current;
1,398,501,420✔
525
    if (queue) qset->current = queue->next;
1,398,494,519✔
526
    if (queue == NULL) break;
1,398,471,173✔
527
    if (queue->head == NULL) continue;
1,398,471,173✔
528

529
    (void)taosThreadMutexLock(&queue->mutex);
1,368,161,650✔
530

531
    if (queue->head) {
1,368,204,360✔
532
      qall->current = queue->head;
1,368,188,527✔
533
      qall->start = queue->head;
1,368,193,532✔
534
      qall->numOfItems = queue->numOfItems;
1,368,127,073✔
535
      qall->memOfItems = queue->memOfItems;
1,368,135,444✔
536
      qall->unAccessedNumOfItems = queue->numOfItems;
1,368,150,601✔
537
      qall->unAccessMemOfItems = queue->memOfItems;
1,368,149,140✔
538

539
      code = qall->numOfItems;
1,368,157,264✔
540
      qinfo->ahandle = queue->ahandle;
1,368,153,361✔
541
      qinfo->fp = queue->itemsFp;
1,368,156,314✔
542
      qinfo->queue = queue;
1,368,130,742✔
543
      qinfo->timestamp = queue->head->timestamp;
1,368,139,344✔
544

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

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

553
      STaosQnode *pNode = qall->start->next;
1,368,174,250✔
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,421,333,073✔
557
        if (tsem_wait(&qset->sem) != 0) {
53,157,378✔
558
          uError("failed to wait semaphore for qset:%p", qset);
×
559
        }
560
        pNode = pNode->next;
53,157,600✔
561
      }
562
    }
563

564
    (void)taosThreadMutexUnlock(&queue->mutex);
1,368,190,658✔
565

566
    if (code != 0) break;
1,368,173,280✔
567
  }
568

569
  (void)taosThreadMutexUnlock(&qset->mutex);
1,388,400,896✔
570
  return code;
1,388,424,480✔
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; }
21,676,911✔
583

584
int64_t taosQueueGetThreadId(STaosQueue *pQueue) { return pQueue->threadId; }
42,357,246✔
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