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, int32_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
      uError("failed to alloc qitem, size:%d alloc:%" PRId64 " allowed:%" PRId64, size + dataSize, alloced, tsRpcQueueMemoryUsed);
128 129 130 131 132 133 134 135 136
      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 已提交
137
  return pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
138
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
139

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

278 279
void taosCloseQset(STaosQset *qset) {
  if (qset == NULL) return;
280 281

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

372 373
  tsem_wait(&qset->sem);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#if 0

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

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

#endif