tqueue.h 4.3 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
queue  
Shengliang Guan 已提交
16 17 18
#ifndef _TD_UTIL_QUEUE_H_
#define _TD_UTIL_QUEUE_H_

S
Shengliang Guan 已提交
19
#include "os.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
20 21 22 23 24

#ifdef __cplusplus
extern "C" {
#endif

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
25 26
/*

27 28 29
This set of API for queue is designed specially for vnode/mnode. The main purpose is to
consume all the items instead of one item from a queue by one single read. Also, it can
combine multiple queues into a queue set, a consumer thread can consume a queue set via
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
30 31 32
a single API instead of looping every queue by itself.

Notes:
33
1: taosOpenQueue/taosCloseQueue, taosOpenQset/taosCloseQset is NOT multi-thread safe
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
34 35 36 37
2: after taosCloseQueue/taosCloseQset is called, read/write operation APIs are not safe.
3: read/write operation APIs are multi-thread safe

To remove the limitation and make this set of queue APIs multi-thread safe, REF(tref.c)
38
shall be used to set up the protection.
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
39 40 41

*/

42 43 44
typedef struct STaosQueue STaosQueue;
typedef struct STaosQset  STaosQset;
typedef struct STaosQall  STaosQall;
S
Shengliang Guan 已提交
45 46
typedef struct {
  void   *ahandle;
47 48
  void   *fp;
  void   *queue;
S
Shengliang Guan 已提交
49 50
  int32_t workerId;
  int32_t threadNum;
D
dapan1121 已提交
51
  int64_t timestamp;
S
Shengliang Guan 已提交
52 53
} SQueueInfo;

54 55 56 57 58
typedef enum {
  DEF_QITEM = 0,
  RPC_QITEM = 1,
} EQItype;

S
Shengliang Guan 已提交
59 60
typedef void (*FItem)(SQueueInfo *pInfo, void *pItem);
typedef void (*FItems)(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfItems);
61

62 63 64 65 66 67
typedef struct STaosQnode STaosQnode;

typedef struct STaosQnode {
  STaosQnode *next;
  STaosQueue *queue;
  int64_t     timestamp;
S
Shengliang Guan 已提交
68
  int64_t     dataSize;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  int32_t     size;
  int8_t      itype;
  int8_t      reserved[3];
  char        item[];
} STaosQnode;

typedef struct STaosQueue {
  STaosQnode   *head;
  STaosQnode   *tail;
  STaosQueue   *next;     // for queue set
  STaosQset    *qset;     // for queue set
  void         *ahandle;  // for queue set
  FItem         itemFp;
  FItems        itemsFp;
  TdThreadMutex mutex;
  int64_t       memOfItems;
  int32_t       numOfItems;
  int64_t       threadId;
} STaosQueue;

typedef struct STaosQset {
  STaosQueue   *head;
  STaosQueue   *current;
  TdThreadMutex mutex;
  tsem_t        sem;
  int32_t       numOfQueues;
  int32_t       numOfItems;
} STaosQset;

typedef struct STaosQall {
  STaosQnode *current;
  STaosQnode *start;
  int32_t     numOfItems;
} STaosQall;

104 105
STaosQueue *taosOpenQueue();
void        taosCloseQueue(STaosQueue *queue);
S
Shengliang Guan 已提交
106
void        taosSetQueueFp(STaosQueue *queue, FItem itemFp, FItems itemsFp);
S
Shengliang Guan 已提交
107
void       *taosAllocateQitem(int32_t size, EQItype itype, int64_t dataSize);
108
void        taosFreeQitem(void *pItem);
S
Shengliang Guan 已提交
109
void        taosWriteQitem(STaosQueue *queue, void *pItem);
110 111
int32_t     taosReadQitem(STaosQueue *queue, void **ppItem);
bool        taosQueueEmpty(STaosQueue *queue);
112
void        taosUpdateItemSize(STaosQueue *queue, int32_t items);
113
int32_t     taosQueueItemSize(STaosQueue *queue);
114
int64_t     taosQueueMemorySize(STaosQueue *queue);
115 116 117 118 119 120

STaosQall *taosAllocateQall();
void       taosFreeQall(STaosQall *qall);
int32_t    taosReadAllQitems(STaosQueue *queue, STaosQall *qall);
int32_t    taosGetQitem(STaosQall *qall, void **ppItem);
void       taosResetQitems(STaosQall *qall);
C
Cary Xu 已提交
121
int32_t    taosQallItemSize(STaosQall *qall);
122 123 124 125 126 127 128 129

STaosQset *taosOpenQset();
void       taosCloseQset(STaosQset *qset);
void       taosQsetThreadResume(STaosQset *qset);
int32_t    taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle);
void       taosRemoveFromQset(STaosQset *qset, STaosQueue *queue);
int32_t    taosGetQueueNumber(STaosQset *qset);

130 131
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, SQueueInfo *qinfo);
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, SQueueInfo *qinfo);
S
Shengliang Guan 已提交
132
void    taosResetQsetThread(STaosQset *qset, void *pItem);
133 134

extern int64_t tsRpcQueueMemoryAllowed;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
135 136 137 138 139

#ifdef __cplusplus
}
#endif

S
queue  
Shengliang Guan 已提交
140
#endif /*_TD_UTIL_QUEUE_H_*/