tqueue.h 4.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
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
typedef struct STaosQnode STaosQnode;

64
struct STaosQnode {
65 66 67
  STaosQnode *next;
  STaosQueue *queue;
  int64_t     timestamp;
S
Shengliang Guan 已提交
68
  int64_t     dataSize;
69 70 71 72
  int32_t     size;
  int8_t      itype;
  int8_t      reserved[3];
  char        item[];
73
};
74

75
struct STaosQueue {
76 77 78 79 80 81 82 83 84 85 86
  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;
dengyihao's avatar
dengyihao 已提交
87 88
  int64_t       memLimit;
  int64_t       itemLimit;
89
};
90

91
struct STaosQset {
92 93 94 95 96 97
  STaosQueue   *head;
  STaosQueue   *current;
  TdThreadMutex mutex;
  tsem_t        sem;
  int32_t       numOfQueues;
  int32_t       numOfItems;
98
};
99

100
struct STaosQall {
101 102 103
  STaosQnode *current;
  STaosQnode *start;
  int32_t     numOfItems;
104
};
105

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

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 已提交
125
int32_t    taosQallItemSize(STaosQall *qall);
126 127 128 129 130 131 132 133

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);

134 135
int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, SQueueInfo *qinfo);
int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, SQueueInfo *qinfo);
S
Shengliang Guan 已提交
136
void    taosResetQsetThread(STaosQset *qset, void *pItem);
137 138

extern int64_t tsRpcQueueMemoryAllowed;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
139 140 141 142 143

#ifdef __cplusplus
}
#endif

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