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

taosdata / TDengine / #4685

21 Aug 2025 12:03PM UTC coverage: 60.092% (+0.06%) from 60.031%
#4685

push

travis-ci

web-flow
jdbc release 3.7.3 (#32682)

* chore(jdbc): update jdbc version

* docs(jdbc): release 3.7.3

138041 of 292193 branches covered (47.24%)

Branch coverage included in aggregate %.

208485 of 284466 relevant lines covered (73.29%)

24747801.12 hits per line

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

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

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

76
  uDebug("queue:%p is opened", queue);
14,095,118✔
77
  return 0;
14,086,800✔
78
}
79

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

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

91
  (void)taosThreadMutexLock(&queue->mutex);
14,095,930✔
92
  STaosQnode *pNode = queue->head;
14,097,967✔
93
  queue->head = NULL;
14,097,967✔
94
  qset = queue->qset;
14,097,967✔
95
  (void)taosThreadMutexUnlock(&queue->mutex);
14,097,967✔
96

97
  if (queue->qset) {
14,099,424✔
98
    taosRemoveFromQset(qset, queue);
77,576✔
99
  }
100

101
  while (pNode) {
14,093,802✔
102
    pTemp = pNode;
149✔
103
    pNode = pNode->next;
149✔
104
    taosMemoryFree(pTemp);
149!
105
  }
106

107
  (void)taosThreadMutexDestroy(&queue->mutex);
14,093,653✔
108
  taosMemoryFree(queue);
14,089,104!
109

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

113
bool taosQueueEmpty(STaosQueue *queue) {
53,713,448✔
114
  if (queue == NULL) return true;
53,713,448!
115

116
  bool empty = false;
53,713,448✔
117
  (void)taosThreadMutexLock(&queue->mutex);
53,713,448✔
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 /*&& queue->memOfItems == 0*/) {
53,815,243!
119
    empty = true;
18,572,719✔
120
  }
121
  (void)taosThreadMutexUnlock(&queue->mutex);
53,815,243✔
122

123
  return empty;
53,835,458✔
124
}
125

126
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
104,258,034✔
127
  if (queue == NULL) return;
104,258,034!
128

129
  (void)taosThreadMutexLock(&queue->mutex);
104,258,034✔
130
  queue->numOfItems -= items;
104,298,103✔
131
  (void)taosThreadMutexUnlock(&queue->mutex);
104,298,103✔
132
}
133

134
int32_t taosQueueItemSize(STaosQueue *queue) {
70,277,639✔
135
  if (queue == NULL) return 0;
70,277,639!
136

137
  (void)taosThreadMutexLock(&queue->mutex);
70,277,639✔
138
  int32_t numOfItems = queue->numOfItems;
70,320,089✔
139
  (void)taosThreadMutexUnlock(&queue->mutex);
70,320,089✔
140

141
  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
70,335,616✔
142
  return numOfItems;
70,335,072✔
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) {
152,764,878✔
153
  int64_t alloced = -1;
152,764,878✔
154

155
  if (itype == RPC_QITEM) {
152,764,878✔
156
    alloced = atomic_add_fetch_64(&tsQueueMemoryUsed, size + dataSize);
93,024,834✔
157
    if (alloced > tsQueueMemoryAllowed) {
93,148,722!
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) {
59,740,044✔
164
    alloced = atomic_add_fetch_64(&tsApplyMemoryUsed, size + dataSize);
5,633,775✔
165
    if (alloced > tsApplyMemoryAllowed) {
5,633,775!
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;
152,888,766✔
174
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
152,888,766✔
175
  if (pNode == NULL) {
152,859,300!
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;
152,859,300✔
185
  pNode->size = size;
152,859,300✔
186
  pNode->itype = itype;
152,859,300✔
187
  pNode->timestamp = taosGetTimestampUs();
152,892,890✔
188
  uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
152,892,890✔
189
  *item = pNode->item;
152,941,235✔
190
  return 0;
152,941,235✔
191
}
192

193
void taosFreeQitem(void *pItem) {
152,868,282✔
194
  if (pItem == NULL) return;
152,868,282!
195

196
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
152,868,282✔
197
  int64_t     alloced = -1;
152,868,282✔
198
  if (pNode->itype == RPC_QITEM) {
152,868,282✔
199
    alloced = atomic_sub_fetch_64(&tsQueueMemoryUsed, pNode->size + pNode->dataSize);
93,145,855✔
200
  } else if (pNode->itype == APPLY_QITEM) {
59,722,427✔
201
    alloced = atomic_sub_fetch_64(&tsApplyMemoryUsed, pNode->size + pNode->dataSize);
5,633,774✔
202
  }
203
  uTrace("item:%p, node:%p is freed, alloc:%" PRId64, pItem, pNode, alloced);
152,887,269✔
204

205
  taosMemoryFree(pNode);
152,887,269!
206
}
207

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

214
  (void)taosThreadMutexLock(&queue->mutex);
152,849,078✔
215
  if (queue->memLimit > 0 && (queue->memOfItems + pNode->size + pNode->dataSize) > queue->memLimit) {
152,970,697!
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) {
152,970,697!
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) {
152,970,697✔
231
    queue->tail->next = pNode;
41,201,742✔
232
    queue->tail = pNode;
41,201,742✔
233
  } else {
234
    queue->head = pNode;
111,768,955✔
235
    queue->tail = pNode;
111,768,955✔
236
  }
237
  queue->numOfItems++;
152,970,697✔
238
  queue->memOfItems += (pNode->size + pNode->dataSize);
152,970,697✔
239
  if (queue->qset) {
152,970,697✔
240
    (void)atomic_add_fetch_32(&queue->qset->numOfItems, 1);
117,550,756✔
241
  }
242

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

245
  (void)taosThreadMutexUnlock(&queue->mutex);
153,041,526✔
246

247
  if (queue->qset) {
152,981,591✔
248
    if (tsem_post(&queue->qset->sem) != 0) {
117,613,804!
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);
117,578,757✔
252
    }
253
  } else {
254
    uDebug("empty qset");
35,367,787✔
255
  }
256
  return code;
152,936,118✔
257
}
258

259
void taosReadQitem(STaosQueue *queue, void **ppItem) {
37,255,791✔
260
  STaosQnode *pNode = NULL;
37,255,791✔
261

262
  (void)taosThreadMutexLock(&queue->mutex);
37,255,791✔
263

264
  if (queue->head) {
37,265,222✔
265
    pNode = queue->head;
35,267,229✔
266
    *ppItem = pNode->item;
35,267,229✔
267
    queue->head = pNode->next;
35,267,229✔
268
    if (queue->head == NULL) {
35,267,229✔
269
      queue->tail = NULL;
11,032,970✔
270
    }
271
    queue->numOfItems--;
35,267,229✔
272
    queue->memOfItems -= (pNode->size + pNode->dataSize);
35,267,229✔
273
    if (queue->qset) {
35,267,229!
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,
35,267,229✔
277
           queue->memOfItems);
278
  }
279

280
  (void)taosThreadMutexUnlock(&queue->mutex);
37,265,222✔
281
}
37,303,798✔
282

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

291
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
80,016!
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) {
71,922,394✔
335
  STaosQnode *pNode;
336
  int32_t     num = 0;
71,922,394✔
337

338
  pNode = qall->current;
71,922,394✔
339
  if (pNode) qall->current = pNode->next;
71,922,394!
340

341
  if (pNode) {
71,922,394!
342
    *ppItem = pNode->item;
71,941,782✔
343
    num = 1;
71,941,782✔
344

345
    qall->unAccessedNumOfItems -= 1;
71,941,782✔
346
    qall->unAccessMemOfItems -= pNode->dataSize;
71,941,782✔
347

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

353
  return num;
71,944,297✔
354
}
355

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

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

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

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

375
  // remove all the queues from qset
376
  (void)taosThreadMutexLock(&qset->mutex);
147,118✔
377
  while (qset->head) {
233,918✔
378
    STaosQueue *queue = qset->head;
86,800✔
379
    qset->head = qset->head->next;
86,800✔
380

381
    queue->qset = NULL;
86,800✔
382
    queue->next = NULL;
86,800✔
383
  }
384
  (void)taosThreadMutexUnlock(&qset->mutex);
147,118✔
385

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

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

407
  (void)taosThreadMutexLock(&qset->mutex);
164,372✔
408

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

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

419
  (void)taosThreadMutexUnlock(&qset->mutex);
164,373✔
420

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

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

428
  (void)taosThreadMutexLock(&qset->mutex);
77,576✔
429

430
  if (qset->head) {
77,576!
431
    if (qset->head == queue) {
77,576✔
432
      qset->head = qset->head->next;
70,195✔
433
      tqueue = queue;
70,195✔
434
    } else {
435
      STaosQueue *prev = qset->head;
7,381✔
436
      tqueue = qset->head->next;
7,381✔
437
      while (tqueue) {
28,933✔
438
        if (tqueue == queue) {
28,932✔
439
          prev->next = tqueue->next;
7,380✔
440
          break;
7,380✔
441
        } else {
442
          prev = tqueue;
21,552✔
443
          tqueue = tqueue->next;
21,552✔
444
        }
445
      }
446
    }
447

448
    if (tqueue) {
77,576✔
449
      if (qset->current == queue) qset->current = tqueue->next;
77,572✔
450
      qset->numOfQueues--;
77,572✔
451

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

460
  (void)taosThreadMutexUnlock(&qset->mutex);
77,579✔
461

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

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

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

475
  (void)taosThreadMutexLock(&qset->mutex);
46,391,073✔
476

477
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
319,150,370✔
478
    if (qset->current == NULL) qset->current = qset->head;
318,363,011✔
479
    STaosQueue *queue = qset->current;
318,363,011✔
480
    if (queue) qset->current = queue->next;
318,363,011!
481
    if (queue == NULL) break;
318,363,011!
482
    if (queue->head == NULL) continue;
318,363,011✔
483

484
    (void)taosThreadMutexLock(&queue->mutex);
45,645,521✔
485

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

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

504
    (void)taosThreadMutexUnlock(&queue->mutex);
45,650,136✔
505
    if (pNode) break;
45,649,960!
506
  }
507

508
  (void)taosThreadMutexUnlock(&qset->mutex);
46,437,406✔
509

510
  return code;
46,430,056✔
511
}
512

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

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

522
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
153,372,493✔
523
    if (qset->current == NULL) qset->current = qset->head;
153,250,974✔
524
    queue = qset->current;
153,250,974✔
525
    if (queue) qset->current = queue->next;
153,250,974!
526
    if (queue == NULL) break;
153,250,974!
527
    if (queue->head == NULL) continue;
153,250,974✔
528

529
    (void)taosThreadMutexLock(&queue->mutex);
59,061,705✔
530

531
    if (queue->head) {
59,106,729!
532
      qall->current = queue->head;
59,107,121✔
533
      qall->start = queue->head;
59,107,121✔
534
      qall->numOfItems = queue->numOfItems;
59,107,121✔
535
      qall->memOfItems = queue->memOfItems;
59,107,121✔
536
      qall->unAccessedNumOfItems = queue->numOfItems;
59,107,121✔
537
      qall->unAccessMemOfItems = queue->memOfItems;
59,107,121✔
538

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

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

551
      (void)atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
59,107,121✔
552

553
      STaosQnode *pNode = qall->start->next;
59,105,975✔
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) {
71,981,396✔
557
        if (tsem_wait(&qset->sem) != 0) {
12,875,309!
558
          uError("failed to wait semaphore for qset:%p", qset);
×
559
        }
560
        pNode = pNode->next;
12,875,421✔
561
      }
562
    }
563

564
    (void)taosThreadMutexUnlock(&queue->mutex);
59,105,695✔
565

566
    if (code != 0) break;
59,105,907!
567
  }
568

569
  (void)taosThreadMutexUnlock(&qset->mutex);
59,228,102✔
570
  return code;
59,185,159✔
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; }
85,971✔
583

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