tqueue.c 10.8 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "os.h"
17

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
18
#include "taoserror.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
19
#include "tqueue.h"
20 21 22
#include "ulog.h"

typedef struct STaosQnode STaosQnode;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
23

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
24
typedef struct STaosQnode {
25 26
  STaosQnode *next;
  char        item[];
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
27 28
} STaosQnode;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
29
typedef struct STaosQueue {
30 31 32 33 34 35 36 37 38 39
  int32_t         itemSize;
  int32_t         numOfItems;
  STaosQnode     *head;
  STaosQnode     *tail;
  STaosQueue     *next;     // for queue set
  STaosQset      *qset;     // for queue set
  void           *ahandle;  // for queue set
  FProcessItem    itemFp;
  FProcessItems   itemsFp;
  pthread_mutex_t mutex;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
40 41
} STaosQueue;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
42
typedef struct STaosQset {
S
Shengliang Guan 已提交
43 44 45 46 47 48
  STaosQueue     *head;
  STaosQueue     *current;
  pthread_mutex_t mutex;
  int32_t         numOfQueues;
  int32_t         numOfItems;
  tsem_t          sem;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
49 50
} STaosQset;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
51
typedef struct STaosQall {
S
Shengliang Guan 已提交
52 53 54 55 56 57
  STaosQnode *current;
  STaosQnode *start;
  int32_t     itemSize;
  int32_t     numOfItems;
} STaosQall;

58 59
STaosQueue *taosOpenQueue() {
  STaosQueue *queue = calloc(sizeof(STaosQueue), 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
60
  if (queue == NULL) {
S
Shengliang Guan 已提交
61
    terrno = TSDB_CODE_OUT_OF_MEMORY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
62
    return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
63 64
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
65
  pthread_mutex_init(&queue->mutex, NULL);
66

S
TD-1843  
Shengliang Guan 已提交
67
  uTrace("queue:%p is opened", queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
68 69
  return queue;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
70

71 72
void taosSetQueueFp(STaosQueue *queue, FProcessItem itemFp, FProcessItems itemsFp) {
  if (queue == NULL) return;
S
Shengliang Guan 已提交
73 74
  queue->itemFp = itemFp;
  queue->itemsFp = itemsFp;
S
Shengliang Guan 已提交
75 76
}

77 78
void taosCloseQueue(STaosQueue *queue) {
  if (queue == NULL) return;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
79
  STaosQnode *pTemp;
80 81 82
  STaosQset  *qset;

  pthread_mutex_lock(&queue->mutex);
S
Shengliang Guan 已提交
83
  STaosQnode *pNode = queue->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
84
  queue->head = NULL;
85 86
  qset = queue->qset;
  pthread_mutex_unlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
87

S
Shengliang Guan 已提交
88
  if (queue->qset) taosRemoveFromQset(qset, queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
89

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
90 91 92
  while (pNode) {
    pTemp = pNode;
    pNode = pNode->next;
S
Shengliang Guan 已提交
93
    free(pTemp);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
94 95
  }

96
  pthread_mutex_destroy(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
97
  free(queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
98 99

  uTrace("queue:%p is closed", queue);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
100
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
101

102 103
bool taosQueueEmpty(STaosQueue *queue) {
  if (queue == NULL) return true;
S
Shengliang Guan 已提交
104 105 106 107 108 109

  bool empty = false;
  pthread_mutex_lock(&queue->mutex);
  if (queue->head == NULL && queue->tail == NULL) {
    empty = true;
  }
110
  pthread_mutex_unlock(&queue->mutex);
S
Shengliang Guan 已提交
111 112 113 114

  return empty;
}

115 116 117 118 119 120 121
int32_t taosQueueSize(STaosQueue *queue) {
  pthread_mutex_lock(&queue->mutex);
  int32_t numOfItems = queue->numOfItems;
  pthread_mutex_unlock(&queue->mutex);
  return numOfItems;
}

122
void *taosAllocateQitem(int32_t size) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
123
  STaosQnode *pNode = (STaosQnode *)calloc(sizeof(STaosQnode) + size, 1);
S
Shengliang Guan 已提交
124

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
125
  if (pNode == NULL) return NULL;
126
  uTrace("item:%p, node:%p is allocated", pNode->item, pNode);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
127 128
  return (void *)pNode->item;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
129

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
130 131 132 133 134
void taosFreeQitem(void *param) {
  if (param == NULL) return;

  char *temp = (char *)param;
  temp -= sizeof(STaosQnode);
135
  uTrace("item:%p, node:%p is freed", param, temp);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
136 137
  free(temp);
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
138

139 140
int32_t taosWriteQitem(STaosQueue *queue, void *pItem) {
  STaosQnode *pNode = (STaosQnode *)(((char *)pItem) - sizeof(STaosQnode));
141
  pNode->next = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
142

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
143
  pthread_mutex_lock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
144

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
145 146 147 148 149
  if (queue->tail) {
    queue->tail->next = pNode;
    queue->tail = pNode;
  } else {
    queue->head = pNode;
S
Shengliang Guan 已提交
150
    queue->tail = pNode;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
151 152 153 154
  }

  queue->numOfItems++;
  if (queue->qset) atomic_add_fetch_32(&queue->qset->numOfItems, 1);
155
  uTrace("item:%p is put into queue:%p, items:%d", pItem, queue, queue->numOfItems);
156

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
157
  pthread_mutex_unlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
158

159 160
  if (queue->qset) tsem_post(&queue->qset->sem);

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
161
  return 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
162 163
}

164
int32_t taosReadQitem(STaosQueue *queue, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
165
  STaosQnode *pNode = NULL;
166
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
167

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
168
  pthread_mutex_lock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
169

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
170
  if (queue->head) {
S
Shengliang Guan 已提交
171
    pNode = queue->head;
172
    *ppItem = pNode->item;
S
Shengliang Guan 已提交
173 174 175 176 177
    queue->head = pNode->next;
    if (queue->head == NULL) queue->tail = NULL;
    queue->numOfItems--;
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, 1);
    code = 1;
178
    uDebug("item:%p is read out from queue:%p, items:%d", *ppItem, queue, queue->numOfItems);
S
Shengliang Guan 已提交
179
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
180

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
181
  pthread_mutex_unlock(&queue->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
182

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
183 184
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
185

186
STaosQall *taosAllocateQall() { return calloc(sizeof(STaosQall), 1); }
187

188
void taosFreeQall(STaosQall *qall) { free(qall); }
189

190 191 192
int32_t taosReadAllQitems(STaosQueue *queue, STaosQall *qall) {
  int32_t code = 0;
  bool    empty;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
193 194 195

  pthread_mutex_lock(&queue->mutex);

J
Jun Li 已提交
196 197
  empty = queue->head == NULL;
  if (!empty) {
198 199 200 201 202 203 204 205 206 207 208
    memset(qall, 0, sizeof(STaosQall));
    qall->current = queue->head;
    qall->start = queue->head;
    qall->numOfItems = queue->numOfItems;
    qall->itemSize = queue->itemSize;
    code = qall->numOfItems;

    queue->head = NULL;
    queue->tail = NULL;
    queue->numOfItems = 0;
    if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, qall->numOfItems);
J
Jun Li 已提交
209
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
210

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
211
  pthread_mutex_unlock(&queue->mutex);
J
Jun Li 已提交
212 213 214 215 216 217 218 219

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

222
int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
223
  STaosQnode *pNode;
224
  int32_t     num = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
225

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
229
  if (pNode) {
230
    *ppItem = pNode->item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
231
    num = 1;
232
    uTrace("item:%p is fetched", *ppItem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
233 234
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
235
  return num;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
236 237
}

238
void taosResetQitems(STaosQall *qall) { qall->current = qall->start; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
239

240
STaosQset *taosOpenQset() {
S
Shengliang Guan 已提交
241
  STaosQset *qset = (STaosQset *)calloc(sizeof(STaosQset), 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
242
  if (qset == NULL) {
S
Shengliang Guan 已提交
243
    terrno = TSDB_CODE_OUT_OF_MEMORY;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
244
    return NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
245 246
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
247
  pthread_mutex_init(&qset->mutex, NULL);
248
  tsem_init(&qset->sem, 0, 0);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
249

S
TD-1843  
Shengliang Guan 已提交
250
  uTrace("qset:%p is opened", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
251 252
  return qset;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
253

254 255
void taosCloseQset(STaosQset *qset) {
  if (qset == NULL) return;
256 257 258 259 260 261 262 263 264 265 266 267

  // remove all the queues from qset
  pthread_mutex_lock(&qset->mutex);
  while (qset->head) {
    STaosQueue *queue = qset->head;
    qset->head = qset->head->next;

    queue->qset = NULL;
    queue->next = NULL;
  }
  pthread_mutex_unlock(&qset->mutex);

268 269
  pthread_mutex_destroy(&qset->mutex);
  tsem_destroy(&qset->sem);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
270
  free(qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
271
  uTrace("qset:%p is closed", qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
272 273
}

274 275 276
// 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.
277
void taosQsetThreadResume(STaosQset *qset) {
S
TD-1670  
Shengliang Guan 已提交
278
  uDebug("qset:%p, it will exit", qset);
279 280 281
  tsem_post(&qset->sem);
}

282
int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle) {
S
Shengliang Guan 已提交
283
  if (queue->qset) return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
284 285 286 287

  pthread_mutex_lock(&qset->mutex);

  queue->next = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
288
  queue->ahandle = ahandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
289 290 291 292 293 294 295 296 297
  qset->head = queue;
  qset->numOfQueues++;

  pthread_mutex_lock(&queue->mutex);
  atomic_add_fetch_32(&qset->numOfItems, queue->numOfItems);
  queue->qset = qset;
  pthread_mutex_unlock(&queue->mutex);

  pthread_mutex_unlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
298

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
299
  uTrace("queue:%p is added into qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
300 301 302
  return 0;
}

303
void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) {
304
  STaosQueue *tqueue = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
305 306 307 308 309 310

  pthread_mutex_lock(&qset->mutex);

  if (qset->head) {
    if (qset->head == queue) {
      qset->head = qset->head->next;
311
      tqueue = queue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
312 313 314 315
    } else {
      STaosQueue *prev = qset->head;
      tqueue = qset->head->next;
      while (tqueue) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
316
        assert(tqueue->qset);
S
Shengliang Guan 已提交
317
        if (tqueue == queue) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
318
          prev->next = tqueue->next;
319
          break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
320 321 322 323 324 325
        } else {
          prev = tqueue;
          tqueue = tqueue->next;
        }
      }
    }
326 327 328 329 330 331 332 333

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

      pthread_mutex_lock(&queue->mutex);
      atomic_sub_fetch_32(&qset->numOfItems, queue->numOfItems);
      queue->qset = NULL;
334
      queue->next = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
335
      pthread_mutex_unlock(&queue->mutex);
336
    }
S
Shengliang Guan 已提交
337 338
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
339
  pthread_mutex_unlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
340 341

  uTrace("queue:%p is removed from qset:%p", queue, qset);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
342 343
}

344
int32_t taosGetQueueNumber(STaosQset *qset) { return qset->numOfQueues; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
345

346
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, void **ahandle, FProcessItem *itemFp) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
347
  STaosQnode *pNode = NULL;
348
  int32_t     code = 0;
S
Shengliang Guan 已提交
349

350 351
  tsem_wait(&qset->sem);

J
Jeff Tao 已提交
352
  pthread_mutex_lock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
353

354
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
S
Shengliang Guan 已提交
355
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
356
    STaosQueue *queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
357 358
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
359
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
360 361 362 363

    pthread_mutex_lock(&queue->mutex);

    if (queue->head) {
S
Shengliang Guan 已提交
364
      pNode = queue->head;
365
      *ppItem = pNode->item;
S
Shengliang Guan 已提交
366
      if (ahandle) *ahandle = queue->ahandle;
S
Shengliang Guan 已提交
367
      if (itemFp) *itemFp = queue->itemFp;
S
Shengliang Guan 已提交
368 369 370 371 372
      queue->head = pNode->next;
      if (queue->head == NULL) queue->tail = NULL;
      queue->numOfItems--;
      atomic_sub_fetch_32(&qset->numOfItems, 1);
      code = 1;
373
      uTrace("item:%p is read out from queue:%p, items:%d", *ppItem, queue, queue->numOfItems);
S
Shengliang Guan 已提交
374
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
375 376 377 378 379

    pthread_mutex_unlock(&queue->mutex);
    if (pNode) break;
  }

J
Jeff Tao 已提交
380 381
  pthread_mutex_unlock(&qset->mutex);

S
Shengliang Guan 已提交
382
  return code;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
383 384
}

385
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, void **ahandle, FProcessItems *itemsFp) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
386
  STaosQueue *queue;
387
  int32_t     code = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
388

389
  tsem_wait(&qset->sem);
J
Jeff Tao 已提交
390 391
  pthread_mutex_lock(&qset->mutex);

392 393
  for (int32_t i = 0; i < qset->numOfQueues; ++i) {
    if (qset->current == NULL) qset->current = qset->head;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
394
    queue = qset->current;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
395 396
    if (queue) qset->current = queue->next;
    if (queue == NULL) break;
J
jtao1735 已提交
397
    if (queue->head == NULL) continue;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
398 399 400 401

    pthread_mutex_lock(&queue->mutex);

    if (queue->head) {
402 403 404 405 406
      qall->current = queue->head;
      qall->start = queue->head;
      qall->numOfItems = queue->numOfItems;
      qall->itemSize = queue->itemSize;
      code = qall->numOfItems;
S
Shengliang Guan 已提交
407
      if (ahandle) *ahandle = queue->ahandle;
S
Shengliang Guan 已提交
408
      if (itemsFp) *itemsFp = queue->itemsFp;
S
Shengliang Guan 已提交
409

410 411 412 413
      queue->head = NULL;
      queue->tail = NULL;
      queue->numOfItems = 0;
      atomic_sub_fetch_32(&qset->numOfItems, qall->numOfItems);
414 415
      for (int32_t j = 1; j < qall->numOfItems; ++j) tsem_wait(&qset->sem);
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
416 417 418

    pthread_mutex_unlock(&queue->mutex);

419
    if (code != 0) break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
420
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
421

J
Jeff Tao 已提交
422
  pthread_mutex_unlock(&qset->mutex);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
423 424
  return code;
}
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
425

426
int32_t taosGetQueueItemsNumber(STaosQueue *queue) {
J
Jun Li 已提交
427 428
  if (!queue) return 0;

429
  int32_t num;
J
Jun Li 已提交
430 431 432 433
  pthread_mutex_lock(&queue->mutex);
  num = queue->numOfItems;
  pthread_mutex_unlock(&queue->mutex);
  return num;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
434 435
}

436
int32_t taosGetQsetItemsNumber(STaosQset *qset) {
J
Jun Li 已提交
437 438
  if (!qset) return 0;

439
  int32_t num = 0;
J
Jun Li 已提交
440 441 442 443
  pthread_mutex_lock(&qset->mutex);
  num = qset->numOfItems;
  pthread_mutex_unlock(&qset->mutex);
  return num;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
444
}