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

taosdata / TDengine / #4680

21 Aug 2025 12:03PM UTC coverage: 59.87% (-0.2%) from 60.031%
#4680

push

travis-ci

web-flow
jdbc release 3.7.3 (#32682)

* chore(jdbc): update jdbc version

* docs(jdbc): release 3.7.3

137338 of 292193 branches covered (47.0%)

Branch coverage included in aggregate %.

207909 of 284466 relevant lines covered (73.09%)

4731310.74 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) {
1,790,680✔
65
  *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
1,790,680!
66
  if (*queue == NULL) {
1,791,351!
67
    return terrno;
×
68
  }
69

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

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

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

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

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

97
  if (queue->qset) {
1,789,152✔
98
    taosRemoveFromQset(qset, queue);
72,592✔
99
  }
100

101
  while (pNode) {
1,789,256✔
102
    pTemp = pNode;
145✔
103
    pNode = pNode->next;
145✔
104
    taosMemoryFree(pTemp);
145!
105
  }
106

107
  (void)taosThreadMutexDestroy(&queue->mutex);
1,789,111✔
108
  taosMemoryFree(queue);
1,789,029!
109

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

113
bool taosQueueEmpty(STaosQueue *queue) {
5,601,849✔
114
  if (queue == NULL) return true;
5,601,849!
115

116
  bool empty = false;
5,601,849✔
117
  (void)taosThreadMutexLock(&queue->mutex);
5,601,849✔
118
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 /*&& queue->memOfItems == 0*/) {
5,602,200!
119
    empty = true;
2,126,495✔
120
  }
121
  (void)taosThreadMutexUnlock(&queue->mutex);
5,602,200✔
122

123
  return empty;
5,602,305✔
124
}
125

126
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
10,693,595✔
127
  if (queue == NULL) return;
10,693,595!
128

129
  (void)taosThreadMutexLock(&queue->mutex);
10,693,595✔
130
  queue->numOfItems -= items;
10,694,074✔
131
  (void)taosThreadMutexUnlock(&queue->mutex);
10,694,074✔
132
}
133

134
int32_t taosQueueItemSize(STaosQueue *queue) {
6,945,156✔
135
  if (queue == NULL) return 0;
6,945,156!
136

137
  (void)taosThreadMutexLock(&queue->mutex);
6,945,156✔
138
  int32_t numOfItems = queue->numOfItems;
6,945,212✔
139
  (void)taosThreadMutexUnlock(&queue->mutex);
6,945,212✔
140

141
  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
6,945,255✔
142
  return numOfItems;
6,945,224✔
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) {
14,586,782✔
153
  int64_t alloced = -1;
14,586,782✔
154

155
  if (itype == RPC_QITEM) {
14,586,782✔
156
    alloced = atomic_add_fetch_64(&tsQueueMemoryUsed, size + dataSize);
9,822,778✔
157
    if (alloced > tsQueueMemoryAllowed) {
9,823,258!
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) {
4,764,004✔
164
    alloced = atomic_add_fetch_64(&tsApplyMemoryUsed, size + dataSize);
451,288✔
165
    if (alloced > tsApplyMemoryAllowed) {
451,288!
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;
14,587,262✔
174
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
14,587,262✔
175
  if (pNode == NULL) {
14,587,522!
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;
14,587,522✔
185
  pNode->size = size;
14,587,522✔
186
  pNode->itype = itype;
14,587,522✔
187
  pNode->timestamp = taosGetTimestampUs();
14,587,849✔
188
  uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
14,587,849✔
189
  *item = pNode->item;
14,587,993✔
190
  return 0;
14,587,993✔
191
}
192

193
void taosFreeQitem(void *pItem) {
14,587,663✔
194
  if (pItem == NULL) return;
14,587,663!
195

196
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
14,587,663✔
197
  int64_t     alloced = -1;
14,587,663✔
198
  if (pNode->itype == RPC_QITEM) {
14,587,663✔
199
    alloced = atomic_sub_fetch_64(&tsQueueMemoryUsed, pNode->size + pNode->dataSize);
9,823,333✔
200
  } else if (pNode->itype == APPLY_QITEM) {
4,764,330✔
201
    alloced = atomic_sub_fetch_64(&tsApplyMemoryUsed, pNode->size + pNode->dataSize);
451,287✔
202
  }
203
  uTrace("item:%p, node:%p is freed, alloc:%" PRId64, pItem, pNode, alloced);
14,587,783✔
204

205
  taosMemoryFree(pNode);
14,587,783!
206
}
207

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

214
  (void)taosThreadMutexLock(&queue->mutex);
14,578,357✔
215
  if (queue->memLimit > 0 && (queue->memOfItems + pNode->size + pNode->dataSize) > queue->memLimit) {
14,579,189!
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) {
14,579,189!
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) {
14,579,189✔
231
    queue->tail->next = pNode;
2,608,036✔
232
    queue->tail = pNode;
2,608,036✔
233
  } else {
234
    queue->head = pNode;
11,971,153✔
235
    queue->tail = pNode;
11,971,153✔
236
  }
237
  queue->numOfItems++;
14,579,189✔
238
  queue->memOfItems += (pNode->size + pNode->dataSize);
14,579,189✔
239
  if (queue->qset) {
14,579,189✔
240
    (void)atomic_add_fetch_32(&queue->qset->numOfItems, 1);
11,104,368✔
241
  }
242

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

245
  (void)taosThreadMutexUnlock(&queue->mutex);
14,579,416✔
246

247
  if (queue->qset) {
14,579,074✔
248
    if (tsem_post(&queue->qset->sem) != 0) {
11,104,479!
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);
11,104,273✔
252
    }
253
  } else {
254
    uDebug("empty qset");
3,474,595✔
255
  }
256
  return code;
14,578,624✔
257
}
258

259
void taosReadQitem(STaosQueue *queue, void **ppItem) {
3,579,797✔
260
  STaosQnode *pNode = NULL;
3,579,797✔
261

262
  (void)taosThreadMutexLock(&queue->mutex);
3,579,797✔
263

264
  if (queue->head) {
3,579,853✔
265
    pNode = queue->head;
3,474,653✔
266
    *ppItem = pNode->item;
3,474,653✔
267
    queue->head = pNode->next;
3,474,653✔
268
    if (queue->head == NULL) {
3,474,653✔
269
      queue->tail = NULL;
1,352,722✔
270
    }
271
    queue->numOfItems--;
3,474,653✔
272
    queue->memOfItems -= (pNode->size + pNode->dataSize);
3,474,653✔
273
    if (queue->qset) {
3,474,653!
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,
3,474,653✔
277
           queue->memOfItems);
278
  }
279

280
  (void)taosThreadMutexUnlock(&queue->mutex);
3,579,853✔
281
}
3,579,856✔
282

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

291
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
74,449!
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) {
6,967,365✔
335
  STaosQnode *pNode;
336
  int32_t     num = 0;
6,967,365✔
337

338
  pNode = qall->current;
6,967,365✔
339
  if (pNode) qall->current = pNode->next;
6,967,365!
340

341
  if (pNode) {
6,967,365!
342
    *ppItem = pNode->item;
6,968,850✔
343
    num = 1;
6,968,850✔
344

345
    qall->unAccessedNumOfItems -= 1;
6,968,850✔
346
    qall->unAccessMemOfItems -= pNode->dataSize;
6,968,850✔
347

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

353
  return num;
6,968,957✔
354
}
355

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

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

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

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

375
  // remove all the queues from qset
376
  (void)taosThreadMutexLock(&qset->mutex);
137,932✔
377
  while (qset->head) {
217,724✔
378
    STaosQueue *queue = qset->head;
79,792✔
379
    qset->head = qset->head->next;
79,792✔
380

381
    queue->qset = NULL;
79,792✔
382
    queue->next = NULL;
79,792✔
383
  }
384
  (void)taosThreadMutexUnlock(&qset->mutex);
137,932✔
385

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

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

407
  (void)taosThreadMutexLock(&qset->mutex);
152,380✔
408

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

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

419
  (void)taosThreadMutexUnlock(&qset->mutex);
152,381✔
420

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

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

428
  (void)taosThreadMutexLock(&qset->mutex);
72,592✔
429

430
  if (qset->head) {
72,592!
431
    if (qset->head == queue) {
72,592✔
432
      qset->head = qset->head->next;
66,411✔
433
      tqueue = queue;
66,411✔
434
    } else {
435
      STaosQueue *prev = qset->head;
6,181✔
436
      tqueue = qset->head->next;
6,181✔
437
      while (tqueue) {
20,424✔
438
        if (tqueue == queue) {
20,423✔
439
          prev->next = tqueue->next;
6,180✔
440
          break;
6,180✔
441
        } else {
442
          prev = tqueue;
14,243✔
443
          tqueue = tqueue->next;
14,243✔
444
        }
445
      }
446
    }
447

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

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

460
  (void)taosThreadMutexUnlock(&qset->mutex);
72,594✔
461

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

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

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

475
  (void)taosThreadMutexLock(&qset->mutex);
4,753,277✔
476

477
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
7,629,746✔
478
    if (qset->current == NULL) qset->current = qset->head;
6,997,323✔
479
    STaosQueue *queue = qset->current;
6,997,323✔
480
    if (queue) qset->current = queue->next;
6,997,323!
481
    if (queue == NULL) break;
6,997,323!
482
    if (queue->head == NULL) continue;
6,997,323✔
483

484
    (void)taosThreadMutexLock(&queue->mutex);
4,134,846✔
485

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

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

504
    (void)taosThreadMutexUnlock(&queue->mutex);
4,134,925✔
505
    if (pNode) break;
4,134,816!
506
  }
507

508
  (void)taosThreadMutexUnlock(&qset->mutex);
4,767,290✔
509

510
  return code;
4,766,070✔
511
}
512

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

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

522
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
6,897,171✔
523
    if (qset->current == NULL) qset->current = qset->head;
6,823,205✔
524
    queue = qset->current;
6,823,205✔
525
    if (queue) qset->current = queue->next;
6,823,205!
526
    if (queue == NULL) break;
6,823,205!
527
    if (queue->head == NULL) continue;
6,823,205✔
528

529
    (void)taosThreadMutexLock(&queue->mutex);
6,682,669✔
530

531
    if (queue->head) {
6,682,806!
532
      qall->current = queue->head;
6,682,821✔
533
      qall->start = queue->head;
6,682,821✔
534
      qall->numOfItems = queue->numOfItems;
6,682,821✔
535
      qall->memOfItems = queue->memOfItems;
6,682,821✔
536
      qall->unAccessedNumOfItems = queue->numOfItems;
6,682,821✔
537
      qall->unAccessMemOfItems = queue->memOfItems;
6,682,821✔
538

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

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

551
      (void)atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
6,682,821✔
552

553
      STaosQnode *pNode = qall->start->next;
6,682,904✔
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) {
6,969,519✔
557
        if (tsem_wait(&qset->sem) != 0) {
286,614!
558
          uError("failed to wait semaphore for qset:%p", qset);
×
559
        }
560
        pNode = pNode->next;
286,615✔
561
      }
562
    }
563

564
    (void)taosThreadMutexUnlock(&queue->mutex);
6,682,890✔
565

566
    if (code != 0) break;
6,682,575!
567
  }
568

569
  (void)taosThreadMutexUnlock(&qset->mutex);
6,756,650✔
570
  return code;
6,756,495✔
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; }
78,736✔
583

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