tworker.h 2.7 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
TD-2393  
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

25 26
typedef int32_t (*ProcessStartFp)(void *ahandle, void *pMsg, int32_t qtype);
typedef void (*ProcessEndFp)(void *ahandle, void *pMsg, int32_t qtype, int32_t code);
27

28 29 30
typedef bool (*ProcessWriteStartFp)(void *ahandle, void *pMsg, int32_t qtype);
typedef void (*ProcessWriteSyncFp)(void *ahandle, int32_t code);
typedef void (*ProcessWriteEndFp)(void *ahandle, void *pMsg, int32_t qtype);
S
TD-2393  
Shengliang Guan 已提交
31

32
typedef struct SWorker {
33
  int32_t             id;      // worker ID
34
  pthread_t           thread;  // thread
35
  struct SWorkerPool *pool;
S
TD-2393  
Shengliang Guan 已提交
36 37 38
} SWorker;

typedef struct SWorkerPool {
39 40 41
  int32_t         max;  // max number of workers
  int32_t         min;  // min number of workers
  int32_t         num;  // current number of workers
42
  taos_qset       qset;
43
  const char *    name;
44 45
  ProcessStartFp  startFp;
  ProcessEndFp    endFp;
46
  SWorker *       workers;
S
TD-2393  
Shengliang Guan 已提交
47 48 49
  pthread_mutex_t mutex;
} SWorkerPool;

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
typedef struct SWriteWorker {
  int32_t                  id;      // worker id
  pthread_t                thread;  // thread
  taos_qall                qall;
  taos_qset                qset;  // queue set
  struct SWriteWorkerPool *pool;
} SWriteWorker;

typedef struct SWriteWorkerPool {
  int32_t             max;     // max number of workers
  int32_t             nextId;  // from 0 to max-1, cyclic
  const char *        name;
  ProcessWriteStartFp startFp;
  ProcessWriteSyncFp  syncFp;
  ProcessWriteEndFp   endFp;
  SWriteWorker *      workers;
  pthread_mutex_t     mutex;
} SWriteWorkerPool;

int32_t    tWorkerInit(SWorkerPool *pool);
void       tWorkerCleanup(SWorkerPool *pool);
taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle);
void       tWorkerFreeQueue(SWorkerPool *pool, taos_queue queue);

int32_t    tWriteWorkerInit(SWriteWorkerPool *pool);
void       tWriteWorkerCleanup(SWriteWorkerPool *pool);
taos_queue tWriteWorkerAllocQueue(SWriteWorkerPool *pool, void *ahandle);
void       tWriteWorkerFreeQueue(SWriteWorkerPool *pool, taos_queue queue);
S
TD-2393  
Shengliang Guan 已提交
78 79 80 81 82

#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
83
#endif /*_TD_UTIL_WORKER_H*/