tworker.c 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 16 17
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
S
Shengliang Guan 已提交
18
#include "ulog.h"
S
TD-2393  
Shengliang Guan 已提交
19 20 21
#include "tqueue.h"
#include "tworker.h"

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static void *taosWorkerThreadFp(void *wparam) {
  SWorker *    worker = wparam;
  SWorkerPool *pool = worker->pool;
  void *       msg = NULL;
  int32_t      qtype = 0;
  void *       ahandle = NULL;
  int32_t      code = 0;

  setThreadName(pool->name);

  while (1) {
    if (taosReadQitemFromQset(pool->qset, &qtype, (void **)&msg, &ahandle) == 0) {
      uDebug("pool:%s, worker:%d qset:%p, got no message and exiting", pool->name, worker->id, pool->qset);
      break;
    }

    code = (*pool->reqFp)(ahandle, msg);
    (*pool->rspFp)(ahandle, msg, qtype, code);
  }

  return NULL;
}

int32_t tWorkerInit(SWorkerPool *pool) {
  pool->qset = taosOpenQset();
  pool->workers = calloc(sizeof(SWorker), pool->max);
  pthread_mutex_init(&pool->mutex, NULL);
  for (int i = 0; i < pool->max; ++i) {
    SWorker *pWorker = pool->workers + i;
S
TD-2393  
Shengliang Guan 已提交
51
    pWorker->id = i;
52
    pWorker->pool = pool;
S
TD-2393  
Shengliang Guan 已提交
53 54
  }

55
  uInfo("worker:%s is initialized, min:%d max:%d", pool->name, pool->min, pool->max);
S
TD-2393  
Shengliang Guan 已提交
56 57 58
  return 0;
}

59 60 61
void tWorkerCleanup(SWorkerPool *pool) {
  for (int i = 0; i < pool->max; ++i) {
    SWorker *pWorker = pool->workers + i;
H
Haojun Liao 已提交
62
    if(taosCheckPthreadValid(pWorker->thread)) {
63
      taosQsetThreadResume(pool->qset);
S
TD-2393  
Shengliang Guan 已提交
64 65 66
    }
  }

67 68
  for (int i = 0; i < pool->max; ++i) {
    SWorker *pWorker = pool->workers + i;
H
Haojun Liao 已提交
69
    if (taosCheckPthreadValid(pWorker->thread)) {
S
TD-2393  
Shengliang Guan 已提交
70 71 72 73
      pthread_join(pWorker->thread, NULL);
    }
  }

74 75 76
  free(pool->workers);
  taosCloseQset(pool->qset);
  pthread_mutex_destroy(&pool->mutex);
S
TD-2393  
Shengliang Guan 已提交
77

78
  uInfo("worker:%s is closed", pool->name);
S
TD-2393  
Shengliang Guan 已提交
79 80
}

81 82
void *tWorkerAllocQueue(SWorkerPool *pool, void *ahandle) {
  pthread_mutex_lock(&pool->mutex);
S
TD-2393  
Shengliang Guan 已提交
83 84
  taos_queue pQueue = taosOpenQueue();
  if (pQueue == NULL) {
85
    pthread_mutex_unlock(&pool->mutex);
S
TD-2393  
Shengliang Guan 已提交
86 87 88
    return NULL;
  }

89
  taosAddIntoQset(pool->qset, pQueue, ahandle);
S
TD-2393  
Shengliang Guan 已提交
90 91

  // spawn a thread to process queue
92
  if (pool->num < pool->max) {
S
TD-2393  
Shengliang Guan 已提交
93
    do {
94
      SWorker *pWorker = pool->workers + pool->num;
S
TD-2393  
Shengliang Guan 已提交
95 96 97 98 99

      pthread_attr_t thAttr;
      pthread_attr_init(&thAttr);
      pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE);

100 101
      if (pthread_create(&pWorker->thread, &thAttr, taosWorkerThreadFp, pWorker) != 0) {
        uError("workers:%s:%d failed to create thread to process since %s", pool->name, pWorker->id, strerror(errno));
S
TD-2393  
Shengliang Guan 已提交
102 103 104
      }

      pthread_attr_destroy(&thAttr);
105 106 107
      pool->num++;
      uDebug("workers:%s:%d is launched, total:%d", pool->name, pWorker->id, pool->num);
    } while (pool->num < pool->min);
S
TD-2393  
Shengliang Guan 已提交
108 109
  }

110 111
  pthread_mutex_unlock(&pool->mutex);
  uDebug("workers:%s, queue:%p is allocated, ahandle:%p", pool->name, pQueue, ahandle);
S
TD-2393  
Shengliang Guan 已提交
112 113 114 115

  return pQueue;
}

116
void tWorkerFreeQueue(SWorkerPool *pool, void *pQueue) {
S
TD-2393  
Shengliang Guan 已提交
117
  taosCloseQueue(pQueue);
118
  uDebug("workers:%s, queue:%p is freed", pool->name, pQueue);
S
TD-2393  
Shengliang Guan 已提交
119
}