tworker.h 3.4 KB
Newer Older
S
TD-2393  
Shengliang Guan 已提交
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 17
#ifndef _TD_UTIL_WORKER_H_
#define _TD_UTIL_WORKER_H_
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
#include "tqueue.h"
20
#include "tarray.h"
S
Shengliang Guan 已提交
21

S
TD-2393  
Shengliang Guan 已提交
22 23 24 25
#ifdef __cplusplus
extern "C" {
#endif

S
Shengliang Guan 已提交
26 27 28 29
typedef struct SQWorkerPool SQWorkerPool;
typedef struct SWWorkerPool SWWorkerPool;

typedef struct SQWorker {
30 31 32 33
  int32_t  id;      // worker id
  int64_t  pid;     // thread pid
  TdThread thread;  // thread id
  void    *pool;
S
Shengliang Guan 已提交
34
} SQWorker;
35

S
Shengliang Guan 已提交
36
typedef struct SQWorkerPool {
S
Shengliang Guan 已提交
37 38 39 40 41 42
  int32_t       max;  // max number of workers
  int32_t       min;  // min number of workers
  int32_t       num;  // current number of workers
  STaosQset    *qset;
  const char   *name;
  SQWorker     *workers;
wafwerar's avatar
wafwerar 已提交
43
  TdThreadMutex mutex;
S
Shengliang Guan 已提交
44
} SQWorkerPool;
S
TD-2393  
Shengliang Guan 已提交
45

46 47 48 49 50 51 52 53
typedef struct SAutoQWorkerPool {
  float         ratio;
  STaosQset    *qset;
  const char   *name;
  SArray       *workers;
  TdThreadMutex mutex;
} SAutoQWorkerPool;

S
Shengliang Guan 已提交
54
typedef struct SWWorker {
55
  int32_t       id;      // worker id
56 57
  int64_t       pid;     // thread pid
  TdThread      thread;  // thread id
S
Shengliang Guan 已提交
58
  STaosQall    *qall;
59
  STaosQset    *qset;
S
Shengliang Guan 已提交
60 61
  SWWorkerPool *pool;
} SWWorker;
62

S
Shengliang Guan 已提交
63
typedef struct SWWorkerPool {
S
Shengliang Guan 已提交
64 65
  int32_t       max;  // max number of workers
  int32_t       num;
S
Shengliang Guan 已提交
66 67 68
  int32_t       nextId;  // from 0 to max-1, cyclic
  const char   *name;
  SWWorker     *workers;
wafwerar's avatar
wafwerar 已提交
69
  TdThreadMutex mutex;
S
Shengliang Guan 已提交
70
} SWWorkerPool;
71

S
Shengliang Guan 已提交
72 73
int32_t     tQWorkerInit(SQWorkerPool *pool);
void        tQWorkerCleanup(SQWorkerPool *pool);
S
Shengliang Guan 已提交
74
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp);
S
Shengliang Guan 已提交
75
void        tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue);
76 77 78 79 80

int32_t     tAutoQWorkerInit(SAutoQWorkerPool *pool);
void        tAutoQWorkerCleanup(SAutoQWorkerPool *pool);
STaosQueue *tAutoQWorkerAllocQueue(SAutoQWorkerPool *pool, void *ahandle, FItem fp);
void        tAutoQWorkerFreeQueue(SAutoQWorkerPool *pool, STaosQueue *queue);
81

S
Shengliang Guan 已提交
82 83
int32_t     tWWorkerInit(SWWorkerPool *pool);
void        tWWorkerCleanup(SWWorkerPool *pool);
S
Shengliang Guan 已提交
84
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp);
S
Shengliang Guan 已提交
85
void        tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue);
S
TD-2393  
Shengliang Guan 已提交
86

S
Shengliang Guan 已提交
87 88
typedef struct {
  const char *name;
S
shm  
Shengliang Guan 已提交
89 90
  int32_t     min;
  int32_t     max;
S
Shengliang Guan 已提交
91 92
  FItem       fp;
  void       *param;
S
Shengliang Guan 已提交
93
} SSingleWorkerCfg;
S
Shengliang Guan 已提交
94 95 96 97 98

typedef struct {
  const char  *name;
  STaosQueue  *queue;
  SQWorkerPool pool;
S
Shengliang Guan 已提交
99
} SSingleWorker;
S
Shengliang Guan 已提交
100 101 102

typedef struct {
  const char *name;
S
shm  
Shengliang Guan 已提交
103
  int32_t     max;
S
Shengliang Guan 已提交
104 105
  FItems      fp;
  void       *param;
S
Shengliang Guan 已提交
106
} SMultiWorkerCfg;
S
Shengliang Guan 已提交
107 108 109 110 111

typedef struct {
  const char  *name;
  STaosQueue  *queue;
  SWWorkerPool pool;
S
Shengliang Guan 已提交
112
} SMultiWorker;
S
Shengliang Guan 已提交
113

S
Shengliang Guan 已提交
114 115 116 117
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg);
void    tSingleWorkerCleanup(SSingleWorker *pWorker);
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg);
void    tMultiWorkerCleanup(SMultiWorker *pWorker);
S
Shengliang Guan 已提交
118

S
TD-2393  
Shengliang Guan 已提交
119 120 121 122
#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
123
#endif /*_TD_UTIL_WORKER_H_*/