tqueue.c 12.1 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;
}

112
void *taosAllocateQitem(int32_t size, EQItype itype) {
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 120 121 122
  pNode->size = size;
  pNode->itype = itype;
  pNode->timestamp = taosGetTimestampUs();

123 124
  if (itype == RPC_QITEM) {
    int64_t alloced = atomic_add_fetch_64(&tsRpcQueueMemoryUsed, size);
dengyihao's avatar
dengyihao 已提交
125
    if (alloced > tsRpcQueueMemoryAllowed) {
126 127 128 129 130 131 132 133 134
      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 已提交
135
  return pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
136
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
137

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

141 142 143 144 145 146 147 148 149
  STaosQnode *pNode = (STaosQnode *)((char *)pItem - sizeof(STaosQnode));
  if (pNode->itype > 0) {
    int64_t alloced = atomic_sub_fetch_64(&tsRpcQueueMemoryUsed, pNode->size);
    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) 已提交
150
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
151

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

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

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

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

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

173
  if (queue->qset) tsem_post(&queue->qset->sem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
174 175
}

176
int32_t taosReadQitem(STaosQueue *queue, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
177
  STaosQnode *pNode = NULL;
178
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
179

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

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
197 198
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
199

200 201 202 203 204 205 206
STaosQall *taosAllocateQall() {
  STaosQall *qall = taosMemoryCalloc(1, sizeof(STaosQall));
  if (qall != NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
  }
  return qall;
}
207

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

210
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
211
  int32_t numOfItems = 0;
212
  bool    empty;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
213

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

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

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

wafwerar's avatar
wafwerar 已提交
233
  taosThreadMutexUnlock(&queue->mutex);
J
Jun Li 已提交
234 235 236 237 238 239 240

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

244
int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
245
  STaosQnode *pNode;
246
  int32_t     num = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
247

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
259
  return num;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
260 261
}

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

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

S
Shengliang Guan 已提交
272
  uDebug("qset:%p is opened", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
273 274
  return qset;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
275

276 277
void taosCloseQset(STaosQset *qset) {
  if (qset == NULL) return;
278 279

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

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

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

296 297 298
// 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.
299
void taosQsetThreadResume(STaosQset *qset) {
S
TD-1670  
Shengliang Guan 已提交
300
  uDebug("qset:%p, it will exit", qset);
301 302 303
  tsem_post(&qset->sem);
}

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

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

  queue->next = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
310
  queue->ahandle = ahandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
311 312 313
  qset->head = queue;
  qset->numOfQueues++;

wafwerar's avatar
wafwerar 已提交
314
  taosThreadMutexLock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
315 316
  atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
  queue->qset = qset;
wafwerar's avatar
wafwerar 已提交
317
  taosThreadMutexUnlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
318

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
321
  uTrace("queue:%p is added into qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
322 323 324
  return 0;
}

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

wafwerar's avatar
wafwerar 已提交
328
  taosThreadMutexLock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
329 330 331 332

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

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

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

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

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

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

370 371
  tsem_wait(&qset->sem);

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
407
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
408 409
}

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

414
  tsem_wait(&qset->sem);
wafwerar's avatar
wafwerar 已提交
415
  taosThreadMutexLock(&qset->mutex);
J
Jeff Tao 已提交
416

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

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

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

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

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

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

449
    if (code != 0) break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
450
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
451

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

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

#if 0

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

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

#endif