tqueue.c 12.4 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
17
#include "tqueue.h"
S
Shengliang Guan 已提交
18
#include "taoserror.h"
S
log  
Shengliang Guan 已提交
19
#include "tlog.h"
20

21 22 23
int64_t tsRpcQueueMemoryAllowed = 0;
int64_t tsRpcQueueMemoryUsed = 0;

24
STaosQueue *taosOpenQueue() {
wafwerar's avatar
wafwerar 已提交
25
  STaosQueue *queue = taosMemoryCalloc(1, sizeof(STaosQueue));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
26
  if (queue == NULL) {
S
Shengliang Guan 已提交
27
    terrno = TSDB_CODE_OUT_OF_MEMORY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
28
    return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
29 30
  }

wafwerar's avatar
wafwerar 已提交
31
  if (taosThreadMutexInit(&queue->mutex, NULL) != 0) {
S
Shengliang Guan 已提交
32 33 34
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
35

S
Shengliang Guan 已提交
36
  uDebug("queue:%p is opened", queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
37 38
  return queue;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
39

S
Shengliang Guan 已提交
40
void taosSetQueueFp(STaosQueue *queue, FItem itemFp, FItems itemsFp) {
41
  if (queue == NULL) return;
S
Shengliang Guan 已提交
42 43
  queue->itemFp = itemFp;
  queue->itemsFp = itemsFp;
S
Shengliang Guan 已提交
44 45
}

46 47
void taosCloseQueue(STaosQueue *queue) {
  if (queue == NULL) return;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
48
  STaosQnode *pTemp;
L
Liu Jicong 已提交
49
  STaosQset  *qset;
50

wafwerar's avatar
wafwerar 已提交
51
  taosThreadMutexLock(&queue->mutex);
S
Shengliang Guan 已提交
52
  STaosQnode *pNode = queue->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
53
  queue->head = NULL;
54
  qset = queue->qset;
wafwerar's avatar
wafwerar 已提交
55
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
56

S
Shengliang Guan 已提交
57 58 59
  if (queue->qset) {
    taosRemoveFromQset(qset, queue);
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
60

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
61 62 63
  while (pNode) {
    pTemp = pNode;
    pNode = pNode->next;
wafwerar's avatar
wafwerar 已提交
64
    taosMemoryFree(pTemp);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
65 66
  }

wafwerar's avatar
wafwerar 已提交
67
  taosThreadMutexDestroy(&queue->mutex);
wafwerar's avatar
wafwerar 已提交
68
  taosMemoryFree(queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
69

S
Shengliang Guan 已提交
70
  uDebug("queue:%p is closed", queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
71
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
72

73 74
bool taosQueueEmpty(STaosQueue *queue) {
  if (queue == NULL) return true;
S
Shengliang Guan 已提交
75 76

  bool empty = false;
wafwerar's avatar
wafwerar 已提交
77
  taosThreadMutexLock(&queue->mutex);
78
  if (queue->head == NULL && queue->tail == NULL && queue->numOfItems == 0 && queue->memOfItems == 0) {
S
Shengliang Guan 已提交
79 80
    empty = true;
  }
wafwerar's avatar
wafwerar 已提交
81
  taosThreadMutexUnlock(&queue->mutex);
S
Shengliang Guan 已提交
82 83 84 85

  return empty;
}

86 87 88 89 90 91 92 93
void taosUpdateItemSize(STaosQueue *queue, int32_t items) {
  if (queue == NULL) return;

  taosThreadMutexLock(&queue->mutex);
  queue->numOfItems -= items;
  taosThreadMutexUnlock(&queue->mutex);
}

94
int32_t taosQueueItemSize(STaosQueue *queue) {
95 96
  if (queue == NULL) return 0;

wafwerar's avatar
wafwerar 已提交
97
  taosThreadMutexLock(&queue->mutex);
98
  int32_t numOfItems = queue->numOfItems;
wafwerar's avatar
wafwerar 已提交
99
  taosThreadMutexUnlock(&queue->mutex);
S
Shengliang Guan 已提交
100 101

  uTrace("queue:%p, numOfItems:%d memOfItems:%" PRId64, queue, queue->numOfItems, queue->memOfItems);
102 103 104
  return numOfItems;
}

105
int64_t taosQueueMemorySize(STaosQueue *queue) {
106
  taosThreadMutexLock(&queue->mutex);
107
  int64_t memOfItems = queue->memOfItems;
108 109 110 111
  taosThreadMutexUnlock(&queue->mutex);
  return memOfItems;
}

S
Shengliang Guan 已提交
112
void *taosAllocateQitem(int32_t size, EQItype itype, int64_t dataSize) {
wafwerar's avatar
wafwerar 已提交
113
  STaosQnode *pNode = taosMemoryCalloc(1, sizeof(STaosQnode) + size);
S
Shengliang Guan 已提交
114 115 116 117
  if (pNode == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
Shengliang Guan 已提交
118

S
Shengliang Guan 已提交
119
  pNode->dataSize = dataSize;
S
Shengliang Guan 已提交
120 121 122 123
  pNode->size = size;
  pNode->itype = itype;
  pNode->timestamp = taosGetTimestampUs();

124
  if (itype == RPC_QITEM) {
S
Shengliang Guan 已提交
125
    int64_t alloced = atomic_add_fetch_64(&tsRpcQueueMemoryUsed, size + dataSize);
dengyihao's avatar
dengyihao 已提交
126
    if (alloced > tsRpcQueueMemoryAllowed) {
S
Shengliang Guan 已提交
127 128 129
      uError("failed to alloc qitem, size:%" PRId64 " alloc:%" PRId64 " allowed:%" PRId64, size + dataSize, alloced,
             tsRpcQueueMemoryUsed);
      atomic_sub_fetch_64(&tsRpcQueueMemoryUsed, size + dataSize);
130 131 132 133 134 135 136 137 138
      taosMemoryFree(pNode);
      terrno = TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE;
      return NULL;
    }
    uTrace("item:%p, node:%p is allocated, alloc:%" PRId64, pNode->item, pNode, alloced);
  } else {
    uTrace("item:%p, node:%p is allocated", pNode->item, pNode);
  }

S
Shengliang Guan 已提交
139
  return pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
140
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
141

S
Shengliang Guan 已提交
142 143
void taosFreeQitem(void *pItem) {
  if (pItem == NULL) return;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
144

145
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
S
Shengliang Guan 已提交
146 147
  if (pNode->itype == RPC_QITEM) {
    int64_t alloced = atomic_sub_fetch_64(&tsRpcQueueMemoryUsed, pNode->size + pNode->dataSize);
148 149 150 151 152 153
    uTrace("item:%p, node:%p is freed, alloc:%" PRId64, pItem, pNode, alloced);
  } else {
    uTrace("item:%p, node:%p is freed", pItem, pNode);
  }

  taosMemoryFree(pNode);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
154
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
155

S
Shengliang Guan 已提交
156
void taosWriteQitem(STaosQueue *queue, void *pItem) {
157
  STaosQnode *pNode = (STaosQnode *)(((char *)pItem) - sizeof(STaosQnode));
158
  pNode->next = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
159

wafwerar's avatar
wafwerar 已提交
160
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
161

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
162 163 164 165 166
  if (queue->tail) {
    queue->tail->next = pNode;
    queue->tail = pNode;
  } else {
    queue->head = pNode;
S
Shengliang Guan 已提交
167
    queue->tail = pNode;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
168 169 170
  }

  queue->numOfItems++;
171
  queue->memOfItems += pNode->size;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
172
  if (queue->qset) atomic_add_fetch_32(&queue->qset->numOfItems, 1);
173
  uTrace("item:%p is put into queue:%p, items:%d mem:%" PRId64, pItem, queue, queue->numOfItems, queue->memOfItems);
174

wafwerar's avatar
wafwerar 已提交
175
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
176

177
  if (queue->qset) tsem_post(&queue->qset->sem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
178 179
}

180
int32_t taosReadQitem(STaosQueue *queue, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
181
  STaosQnode *pNode = NULL;
182
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
183

wafwerar's avatar
wafwerar 已提交
184
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
185

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
186
  if (queue->head) {
S
Shengliang Guan 已提交
187
    pNode = queue->head;
188
    *ppItem = pNode->item;
S
Shengliang Guan 已提交
189 190 191
    queue->head = pNode->next;
    if (queue->head == NULL) queue->tail = NULL;
    queue->numOfItems--;
192
    queue->memOfItems -= pNode->size;
S
Shengliang Guan 已提交
193 194
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, 1);
    code = 1;
195 196
    uTrace("item:%p is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems,
           queue->memOfItems);
S
Shengliang Guan 已提交
197
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
198

wafwerar's avatar
wafwerar 已提交
199
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
200

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
201 202
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
203

204 205 206 207 208 209 210
STaosQall *taosAllocateQall() {
  STaosQall *qall = taosMemoryCalloc(1, sizeof(STaosQall));
  if (qall != NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
  }
  return qall;
}
211

wafwerar's avatar
wafwerar 已提交
212
void taosFreeQall(STaosQall *qall) { taosMemoryFree(qall); }
213

214
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
215
  int32_t numOfItems = 0;
216
  bool    empty;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
217

wafwerar's avatar
wafwerar 已提交
218
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
219

J
Jun Li 已提交
220 221
  empty = queue->head == NULL;
  if (!empty) {
222 223 224 225
    memset(qall, 0, sizeof(STaosQall));
    qall->current = queue->head;
    qall->start = queue->head;
    qall->numOfItems = queue->numOfItems;
226
    numOfItems = qall->numOfItems;
227 228 229 230

    queue->head = NULL;
    queue->tail = NULL;
    queue->numOfItems = 0;
231
    queue->memOfItems = 0;
232 233
    uTrace("read %d items from queue:%p, items:%d mem:%" PRId64, numOfItems, queue, queue->numOfItems,
           queue->memOfItems);
234
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, qall->numOfItems);
J
Jun Li 已提交
235
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
236

wafwerar's avatar
wafwerar 已提交
237
  taosThreadMutexUnlock(&queue->mutex);
J
Jun Li 已提交
238 239 240 241 242 243 244

  // if source queue is empty, we set destination qall to empty too.
  if (empty) {
    qall->current = NULL;
    qall->start = NULL;
    qall->numOfItems = 0;
  }
245
  return numOfItems;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
246
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
247

248
int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
249
  STaosQnode *pNode;
250
  int32_t     num = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
251

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
252
  pNode = qall->current;
S
Shengliang Guan 已提交
253 254
  if (pNode) qall->current = pNode->next;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
255
  if (pNode) {
256
    *ppItem = pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
257
    num = 1;
258
    uTrace("item:%p is fetched", *ppItem);
L
Liu Jicong 已提交
259 260
  } else {
    *ppItem = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
261 262
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
263
  return num;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
264 265
}

266
STaosQset *taosOpenQset() {
wafwerar's avatar
wafwerar 已提交
267
  STaosQset *qset = taosMemoryCalloc(sizeof(STaosQset), 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
268
  if (qset == NULL) {
S
Shengliang Guan 已提交
269
    terrno = TSDB_CODE_OUT_OF_MEMORY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
270
    return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
271 272
  }

wafwerar's avatar
wafwerar 已提交
273
  taosThreadMutexInit(&qset->mutex, NULL);
274
  tsem_init(&qset->sem, 0, 0);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
275

S
Shengliang Guan 已提交
276
  uDebug("qset:%p is opened", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
277 278
  return qset;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
279

280 281
void taosCloseQset(STaosQset *qset) {
  if (qset == NULL) return;
282 283

  // remove all the queues from qset
wafwerar's avatar
wafwerar 已提交
284
  taosThreadMutexLock(&qset->mutex);
285 286 287 288 289 290 291
  while (qset->head) {
    STaosQueue *queue = qset->head;
    qset->head = qset->head->next;

    queue->qset = NULL;
    queue->next = NULL;
  }
wafwerar's avatar
wafwerar 已提交
292
  taosThreadMutexUnlock(&qset->mutex);
293

wafwerar's avatar
wafwerar 已提交
294
  taosThreadMutexDestroy(&qset->mutex);
295
  tsem_destroy(&qset->sem);
wafwerar's avatar
wafwerar 已提交
296
  taosMemoryFree(qset);
S
Shengliang Guan 已提交
297
  uDebug("qset:%p is closed", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
298 299
}

300 301 302
// tsem_post 'qset->sem', so that reader threads waiting for it
// resumes execution and return, should only be used to signal the
// thread to exit.
303
void taosQsetThreadResume(STaosQset *qset) {
S
TD-1670  
Shengliang Guan 已提交
304
  uDebug("qset:%p, it will exit", qset);
305 306 307
  tsem_post(&qset->sem);
}

308
int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle) {
S
Shengliang Guan 已提交
309
  if (queue->qset) return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
310

wafwerar's avatar
wafwerar 已提交
311
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
312 313

  queue->next = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
314
  queue->ahandle = ahandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
315 316 317
  qset->head = queue;
  qset->numOfQueues++;

wafwerar's avatar
wafwerar 已提交
318
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
319 320
  atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
  queue->qset = qset;
wafwerar's avatar
wafwerar 已提交
321
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
322

wafwerar's avatar
wafwerar 已提交
323
  taosThreadMutexUnlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
324

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
325
  uTrace("queue:%p is added into qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
326 327 328
  return 0;
}

329
void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) {
330
  STaosQueue *tqueue = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
331

wafwerar's avatar
wafwerar 已提交
332
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
333 334 335 336

  if (qset->head) {
    if (qset->head == queue) {
      qset->head = qset->head->next;
337
      tqueue = queue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
338 339 340 341
    } else {
      STaosQueue *prev = qset->head;
      tqueue = qset->head->next;
      while (tqueue) {
S
Shengliang Guan 已提交
342
        if (tqueue == queue) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
343
          prev->next = tqueue->next;
344
          break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
345 346 347 348 349 350
        } else {
          prev = tqueue;
          tqueue = tqueue->next;
        }
      }
    }
351 352 353 354 355

    if (tqueue) {
      if (qset->current == queue) qset->current = tqueue->next;
      qset->numOfQueues--;

wafwerar's avatar
wafwerar 已提交
356
      taosThreadMutexLock(&queue->mutex);
357 358
      atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems);
      queue->qset = NULL;
359
      queue->next = NULL;
wafwerar's avatar
wafwerar 已提交
360
      taosThreadMutexUnlock(&queue->mutex);
361
    }
S
Shengliang Guan 已提交
362 363
  }

wafwerar's avatar
wafwerar 已提交
364
  taosThreadMutexUnlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
365

S
Shengliang Guan 已提交
366
  uDebug("queue:%p is removed from qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
367 368
}

369
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, SQueueInfo *qinfo) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
370
  STaosQnode *pNode = NULL;
371
  int32_t     code = 0;
S
Shengliang Guan 已提交
372

373 374
  tsem_wait(&qset->sem);

wafwerar's avatar
wafwerar 已提交
375
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
376

377
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
S
Shengliang Guan 已提交
378
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
379
    STaosQueue *queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
380 381
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
382
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
383

wafwerar's avatar
wafwerar 已提交
384
    taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
385 386

    if (queue->head) {
S
Shengliang Guan 已提交
387
      pNode = queue->head;
388
      *ppItem = pNode->item;
389 390 391 392
      qinfo->ahandle = queue->ahandle;
      qinfo->fp = queue->itemFp;
      qinfo->queue = queue;
      qinfo->timestamp = pNode->timestamp;
S
Shengliang Guan 已提交
393

S
Shengliang Guan 已提交
394 395
      queue->head = pNode->next;
      if (queue->head == NULL) queue->tail = NULL;
396
      // queue->numOfItems--;
397
      queue->memOfItems -= pNode->size;
S
Shengliang Guan 已提交
398 399
      atomic_sub_fetch_32(&qset->numOfItems, 1);
      code = 1;
400
      uTrace("item:%p is read out from queue:%p, items:%d mem:%" PRId64, *ppItem, queue, queue->numOfItems - 1,
401
             queue->memOfItems);
S
Shengliang Guan 已提交
402
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
403

wafwerar's avatar
wafwerar 已提交
404
    taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
405 406 407
    if (pNode) break;
  }

wafwerar's avatar
wafwerar 已提交
408
  taosThreadMutexUnlock(&qset->mutex);
J
Jeff Tao 已提交
409

S
Shengliang Guan 已提交
410
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
411 412
}

413
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, SQueueInfo *qinfo) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
414
  STaosQueue *queue;
415
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
416

417
  tsem_wait(&qset->sem);
wafwerar's avatar
wafwerar 已提交
418
  taosThreadMutexLock(&qset->mutex);
J
Jeff Tao 已提交
419

420 421
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
422
    queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
423 424
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
425
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
426

wafwerar's avatar
wafwerar 已提交
427
    taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
428 429

    if (queue->head) {
430 431 432 433
      qall->current = queue->head;
      qall->start = queue->head;
      qall->numOfItems = queue->numOfItems;
      code = qall->numOfItems;
434 435 436
      qinfo->ahandle = queue->ahandle;
      qinfo->fp = queue->itemsFp;
      qinfo->queue = queue;
S
Shengliang Guan 已提交
437

438 439
      queue->head = NULL;
      queue->tail = NULL;
440
      // queue->numOfItems = 0;
441
      queue->memOfItems = 0;
442
      uTrace("read %d items from queue:%p, items:0 mem:%" PRId64, code, queue, queue->memOfItems);
443

444
      atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
S
Shengliang Guan 已提交
445 446 447
      for (int32_t j = 1; j < qall->numOfItems; ++j) {
        tsem_wait(&qset->sem);
      }
448
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
449

wafwerar's avatar
wafwerar 已提交
450
    taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
451

452
    if (code != 0) break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
453
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
454

wafwerar's avatar
wafwerar 已提交
455
  taosThreadMutexUnlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
456 457
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
458

S
Shengliang Guan 已提交
459
int32_t taosQallItemSize(STaosQall *qall) { return qall->numOfItems; }
S
Shengliang Guan 已提交
460 461
void    taosResetQitems(STaosQall *qall) { qall->current = qall->start; }
int32_t taosGetQueueNumber(STaosQset *qset) { return qset->numOfQueues; }
S
Shengliang Guan 已提交
462 463 464

#if 0

S
Shengliang Guan 已提交
465 466 467 468
void taosResetQsetThread(STaosQset *qset, void *pItem) {
  if (pItem == NULL) return;
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));

wafwerar's avatar
wafwerar 已提交
469
  taosThreadMutexLock(&qset->mutex);
S
Shengliang Guan 已提交
470 471 472
  for (int32_t i = 0; i < pNode->queue->numOfItems; ++i) {
    tsem_post(&qset->sem);
  }
wafwerar's avatar
wafwerar 已提交
473
  taosThreadMutexUnlock(&qset->mutex);
S
Shengliang Guan 已提交
474
}
S
Shengliang Guan 已提交
475 476

#endif