tworker.h 2.8 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 20
#include "tqueue.h"

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

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

typedef struct SQWorker {
  int32_t       id;      // worker ID
S
Shengliang Guan 已提交
30
  TdThread      thread;  // thread
S
Shengliang Guan 已提交
31
  SQWorkerPool *pool;
S
Shengliang Guan 已提交
32
} SQWorker;
33

S
Shengliang Guan 已提交
34
typedef struct SQWorkerPool {
S
Shengliang Guan 已提交
35 36 37 38 39 40
  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 已提交
41
  TdThreadMutex mutex;
S
Shengliang Guan 已提交
42
} SQWorkerPool;
S
TD-2393  
Shengliang Guan 已提交
43

S
Shengliang Guan 已提交
44
typedef struct SWWorker {
45
  int32_t       id;      // worker id
S
Shengliang Guan 已提交
46
  TdThread      thread;  // thread
S
Shengliang Guan 已提交
47 48
  STaosQall    *qall;
  STaosQset    *qset;  // queue set
S
Shengliang Guan 已提交
49 50
  SWWorkerPool *pool;
} SWWorker;
51

S
Shengliang Guan 已提交
52
typedef struct SWWorkerPool {
S
Shengliang Guan 已提交
53 54 55 56
  int32_t       max;     // max number of workers
  int32_t       nextId;  // from 0 to max-1, cyclic
  const char   *name;
  SWWorker     *workers;
wafwerar's avatar
wafwerar 已提交
57
  TdThreadMutex mutex;
S
Shengliang Guan 已提交
58
} SWWorkerPool;
59

S
Shengliang Guan 已提交
60 61
int32_t     tQWorkerInit(SQWorkerPool *pool);
void        tQWorkerCleanup(SQWorkerPool *pool);
S
Shengliang Guan 已提交
62
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp);
S
Shengliang Guan 已提交
63
void        tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue);
64

S
Shengliang Guan 已提交
65 66
int32_t     tWWorkerInit(SWWorkerPool *pool);
void        tWWorkerCleanup(SWWorkerPool *pool);
S
Shengliang Guan 已提交
67
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp);
S
Shengliang Guan 已提交
68
void        tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue);
S
TD-2393  
Shengliang Guan 已提交
69

S
Shengliang Guan 已提交
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
typedef struct {
  const char *name;
  int32_t     minNum;
  int32_t     maxNum;
  FItem       fp;
  void       *param;
} SQWorkerAllCfg;

typedef struct {
  const char  *name;
  STaosQueue  *queue;
  SQWorkerPool pool;
} SQWorkerAll;

typedef struct {
  const char *name;
  int32_t     maxNum;
  FItems      fp;
  void       *param;
} SWWorkerAllCfg;

typedef struct {
  const char  *name;
  STaosQueue  *queue;
  SWWorkerPool pool;
} SWWorkerAll;

int32_t tQWorkerAllInit(SQWorkerAll *pWorker, const SQWorkerAllCfg *pCfg);
void    tQWorkerAllCleanup(SQWorkerAll *pWorker);
int32_t tWWorkerAllInit(SWWorkerAll *pWorker, const SWWorkerAllCfg *pCfg);
void    tWWorkerAllCleanup(SWWorkerAll *pWorker);

S
TD-2393  
Shengliang Guan 已提交
102 103 104 105
#ifdef __cplusplus
}
#endif

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