tprocess.c 14.4 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 "tprocess.h"
#include "taoserror.h"
#include "tlog.h"
S
shm  
Shengliang Guan 已提交
20
#include "tqueue.h"
S
shm  
Shengliang Guan 已提交
21 22

#define SHM_DEFAULT_SIZE (20 * 1024 * 1024)
S
shm  
Shengliang Guan 已提交
23
typedef void *(*ProcThreadFp)(void *param);
S
shm  
Shengliang Guan 已提交
24 25

typedef struct SProcQueue {
S
shm  
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34
  int32_t       head;
  int32_t       tail;
  int32_t       total;
  int32_t       avail;
  int32_t       items;
  char          name[8];
  TdThreadMutex mutex;
  tsem_t        sem;
  char          pBuffer[];
S
shm  
Shengliang Guan 已提交
35 36
} SProcQueue;

S
shm  
Shengliang Guan 已提交
37
typedef struct SProcObj {
S
shm  
Shengliang Guan 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  TdThread      thread;
  SProcQueue   *pChildQueue;
  SProcQueue   *pParentQueue;
  ProcConsumeFp childConsumeFp;
  ProcMallocFp  childMallocHeadFp;
  ProcFreeFp    childFreeHeadFp;
  ProcMallocFp  childMallocBodyFp;
  ProcFreeFp    childFreeBodyFp;
  ProcConsumeFp parentConsumeFp;
  ProcMallocFp  parentMallocHeadFp;
  ProcFreeFp    parentFreeHeadFp;
  ProcMallocFp  parentMallocBodyFp;
  ProcFreeFp    parentFreeBodyFp;
  void         *pParent;
  const char   *name;
  int32_t       pid;
  bool          isChild;
  bool          stopFlag;
S
shm  
Shengliang Guan 已提交
56 57
} SProcObj;

S
shm  
Shengliang Guan 已提交
58 59 60 61 62
static inline int32_t CEIL8(int32_t v) {
  const int32_t c = ceil((float)(v) / 8) * 8;
  return c < 8 ? 8 : c;
}

S
shm  
Shengliang Guan 已提交
63
static int32_t taosProcInitMutex(SProcQueue *pQueue) {
S
Shengliang Guan 已提交
64
  TdThreadMutexAttr mattr = {0};
S
shm  
Shengliang Guan 已提交
65

wafwerar's avatar
wafwerar 已提交
66
  if (taosThreadMutexAttrInit(&mattr) != 0) {
S
shm  
Shengliang Guan 已提交
67 68
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while init attr since %s", terrstr());
S
shm  
Shengliang Guan 已提交
69
    return -1;
S
shm  
Shengliang Guan 已提交
70 71
  }

wafwerar's avatar
wafwerar 已提交
72
  if (taosThreadMutexAttrSetPshared(&mattr, PTHREAD_PROCESS_SHARED) != 0) {
73
    taosThreadMutexAttrDestroy(&mattr);
S
shm  
Shengliang Guan 已提交
74 75
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while set shared since %s", terrstr());
S
shm  
Shengliang Guan 已提交
76
    return -1;
S
shm  
Shengliang Guan 已提交
77 78
  }

S
shm  
Shengliang Guan 已提交
79
  if (taosThreadMutexInit(&pQueue->mutex, &mattr) != 0) {
80
    taosThreadMutexDestroy(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
81 82
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex since %s", terrstr());
S
shm  
Shengliang Guan 已提交
83
    return -1;
S
shm  
Shengliang Guan 已提交
84 85
  }

wafwerar's avatar
wafwerar 已提交
86
  taosThreadMutexAttrDestroy(&mattr);
S
shm  
Shengliang Guan 已提交
87
  return 0;
S
shm  
Shengliang Guan 已提交
88 89
}

S
shm  
Shengliang Guan 已提交
90 91
static int32_t taosProcInitSem(SProcQueue *pQueue) {
  if (tsem_init(&pQueue->sem, 1, 0) != 0) {
S
shm  
Shengliang Guan 已提交
92
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
93
    uError("failed to init sem");
S
shm  
Shengliang Guan 已提交
94 95 96
    return -1;
  }

S
shm  
Shengliang Guan 已提交
97
  return 0;
S
shm  
Shengliang Guan 已提交
98 99
}

S
shm  
Shengliang Guan 已提交
100 101 102 103 104
static SProcQueue *taosProcInitQueue(const char *name, bool isChild, char *ptr, int32_t size) {
  int32_t bufSize = size - CEIL8(sizeof(SProcQueue));
  if (bufSize <= 1024) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
S
shm  
Shengliang Guan 已提交
105 106
  }

S
shm  
Shengliang Guan 已提交
107
  SProcQueue *pQueue = (SProcQueue *)(ptr);
S
shm  
Shengliang Guan 已提交
108

S
shm  
Shengliang Guan 已提交
109 110 111 112
  if (!isChild) {
    if (taosProcInitMutex(pQueue) != 0) {
      return NULL;
    }
S
shm  
Shengliang Guan 已提交
113

S
shm  
Shengliang Guan 已提交
114 115 116
    if (taosProcInitSem(pQueue) != 0) {
      return NULL;
    }
S
shm  
Shengliang Guan 已提交
117

S
shm  
Shengliang Guan 已提交
118 119 120 121 122 123
    tstrncpy(pQueue->name, name, sizeof(pQueue->name));
    pQueue->head = 0;
    pQueue->tail = 0;
    pQueue->total = bufSize;
    pQueue->avail = bufSize;
    pQueue->items = 0;
S
shm  
Shengliang Guan 已提交
124 125
  }

S
shm  
Shengliang Guan 已提交
126 127 128 129 130 131 132 133
  return pQueue;
}

#if 0
static void taosProcDestroyMutex(SProcQueue *pQueue) {
  if (pQueue->mutex != NULL) {
    taosThreadMutexDestroy(pQueue->mutex);
    pQueue->mutex = NULL;
S
shm  
Shengliang Guan 已提交
134
  }
S
shm  
Shengliang Guan 已提交
135
}
S
shm  
Shengliang Guan 已提交
136

S
shm  
Shengliang Guan 已提交
137 138 139 140
static void taosProcDestroySem(SProcQueue *pQueue) {
  if (pQueue->sem != NULL) {
    tsem_destroy(pQueue->sem);
    pQueue->sem = NULL;
S
shm  
Shengliang Guan 已提交
141
  }
S
shm  
Shengliang Guan 已提交
142
}
S
shm  
Shengliang Guan 已提交
143
#endif
S
shm  
Shengliang Guan 已提交
144

S
shm  
Shengliang Guan 已提交
145
static void taosProcCleanupQueue(SProcQueue *pQueue) {
S
shm  
Shengliang Guan 已提交
146
#if 0  
S
shm  
Shengliang Guan 已提交
147
  if (pQueue != NULL) {
S
shm  
Shengliang Guan 已提交
148 149
    taosProcDestroyMutex(pQueue);
    taosProcDestroySem(pQueue);
S
shm  
Shengliang Guan 已提交
150
  }
S
shm  
Shengliang Guan 已提交
151
#endif
S
shm  
Shengliang Guan 已提交
152
}
S
shm  
Shengliang Guan 已提交
153

S
shm  
Shengliang Guan 已提交
154
static int32_t taosProcQueuePush(SProcQueue *pQueue, const char *pHead, int16_t rawHeadLen, const char *pBody,
S
Shengliang Guan 已提交
155
                                 int32_t rawBodyLen, ProcFuncType ftype) {
S
shm  
Shengliang Guan 已提交
156 157 158
  const int32_t headLen = CEIL8(rawHeadLen);
  const int32_t bodyLen = CEIL8(rawBodyLen);
  const int32_t fullLen = headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
159

S
shm  
Shengliang Guan 已提交
160
  taosThreadMutexLock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
161
  if (fullLen > pQueue->avail) {
S
shm  
Shengliang Guan 已提交
162
    taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
163 164 165 166
    terrno = TSDB_CODE_OUT_OF_SHM_MEM;
    return -1;
  }

S
Shengliang Guan 已提交
167
  const int32_t pos = pQueue->tail;
S
shm  
Shengliang Guan 已提交
168
  if (pQueue->tail < pQueue->total) {
S
Shengliang Guan 已提交
169 170 171
    *(int16_t *)(pQueue->pBuffer + pQueue->tail) = headLen;
    *(int8_t *)(pQueue->pBuffer + pQueue->tail + 2) = (int8_t)ftype;
    *(int32_t *)(pQueue->pBuffer + pQueue->tail + 4) = bodyLen;
S
shm  
Shengliang Guan 已提交
172
  } else {
S
Shengliang Guan 已提交
173
    *(int16_t *)(pQueue->pBuffer) = headLen;
S
Shengliang Guan 已提交
174
    *(int8_t *)(pQueue->pBuffer + 2) = (int8_t)ftype;
S
shm  
Shengliang Guan 已提交
175 176 177
    *(int32_t *)(pQueue->pBuffer + 4) = bodyLen;
  }

S
shm  
Shengliang Guan 已提交
178
  if (pQueue->tail < pQueue->head) {
S
shm  
Shengliang Guan 已提交
179 180 181
    memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, rawHeadLen);
    memcpy(pQueue->pBuffer + pQueue->tail + 8 + headLen, pBody, rawBodyLen);
    pQueue->tail = pQueue->tail + 8 + headLen + bodyLen;
S
shm  
Shengliang Guan 已提交
182 183
  } else {
    int32_t remain = pQueue->total - pQueue->tail;
S
shm  
Shengliang Guan 已提交
184 185 186 187 188 189 190 191 192
    if (remain == 0) {
      memcpy(pQueue->pBuffer + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + 8 + headLen, pBody, rawBodyLen);
      pQueue->tail = 8 + headLen + bodyLen;
    } else if (remain == 8) {
      memcpy(pQueue->pBuffer, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + headLen, pBody, rawBodyLen);
      pQueue->tail = headLen + bodyLen;
    } else if (remain < 8 + headLen) {
S
Shengliang Guan 已提交
193
      memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, remain - 8);
S
shm  
Shengliang Guan 已提交
194 195 196
      memcpy(pQueue->pBuffer, pHead + remain - 8, rawHeadLen - (remain - 8));
      memcpy(pQueue->pBuffer + headLen - (remain - 8), pBody, rawBodyLen);
      pQueue->tail = headLen - (remain - 8) + bodyLen;
S
Shengliang Guan 已提交
197 198 199
    } else if (remain < 8 + headLen + bodyLen) {
      memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + pQueue->tail + 8 + headLen, pBody, remain - 8 - headLen);
S
shm  
Shengliang Guan 已提交
200 201
      memcpy(pQueue->pBuffer, pBody + remain - 8 - headLen, rawBodyLen - (remain - 8 - headLen));
      pQueue->tail = bodyLen - (remain - 8 - headLen);
S
shm  
Shengliang Guan 已提交
202
    } else {
S
Shengliang Guan 已提交
203 204 205
      memcpy(pQueue->pBuffer + pQueue->tail + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + pQueue->tail + headLen + 8, pBody, rawBodyLen);
      pQueue->tail = pQueue->tail + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
206 207 208 209 210
    }
  }

  pQueue->avail -= fullLen;
  pQueue->items++;
S
shm  
Shengliang Guan 已提交
211
  taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
212
  tsem_post(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
213

S
Shengliang Guan 已提交
214 215
  uTrace("proc:%s, push msg at pos:%d ftype:%d remain:%d, head:%d %p body:%d %p", pQueue->name, pos, ftype,
         pQueue->items, headLen, pHead, bodyLen, pBody);
S
shm  
Shengliang Guan 已提交
216 217 218
  return 0;
}

S
shm  
Shengliang Guan 已提交
219 220 221
static int32_t taosProcQueuePop(SProcQueue *pQueue, void **ppHead, int16_t *pHeadLen, void **ppBody, int32_t *pBodyLen,
                                ProcFuncType *pFuncType, ProcMallocFp mallocHeadFp, ProcFreeFp freeHeadFp,
                                ProcMallocFp mallocBodyFp, ProcFreeFp freeBodyFp) {
S
shm  
Shengliang Guan 已提交
222 223
  tsem_wait(&pQueue->sem);

S
shm  
Shengliang Guan 已提交
224
  taosThreadMutexLock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
225
  if (pQueue->total - pQueue->avail <= 0) {
S
shm  
Shengliang Guan 已提交
226
    taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
227
    terrno = TSDB_CODE_OUT_OF_SHM_MEM;
S
shm  
Shengliang Guan 已提交
228
    return 0;
S
shm  
Shengliang Guan 已提交
229 230
  }

S
Shengliang Guan 已提交
231 232
  int16_t headLen = 0;
  int8_t  ftype = 0;
S
shm  
Shengliang Guan 已提交
233 234
  int32_t bodyLen = 0;
  if (pQueue->head < pQueue->total) {
S
Shengliang Guan 已提交
235 236
    headLen = *(int16_t *)(pQueue->pBuffer + pQueue->head);
    ftype = *(int8_t *)(pQueue->pBuffer + pQueue->head + 2);
S
shm  
Shengliang Guan 已提交
237 238
    bodyLen = *(int32_t *)(pQueue->pBuffer + pQueue->head + 4);
  } else {
S
Shengliang Guan 已提交
239
    headLen = *(int16_t *)(pQueue->pBuffer);
240
    ftype = *(int8_t *)(pQueue->pBuffer + 2);
S
shm  
Shengliang Guan 已提交
241 242
    bodyLen = *(int32_t *)(pQueue->pBuffer + 4);
  }
S
shm  
Shengliang Guan 已提交
243

S
shm  
Shengliang Guan 已提交
244 245
  void *pHead = (*mallocHeadFp)(headLen);
  void *pBody = (*mallocBodyFp)(bodyLen);
S
shm  
Shengliang Guan 已提交
246
  if (pHead == NULL || pBody == NULL) {
S
shm  
Shengliang Guan 已提交
247
    taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
248
    tsem_post(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
249 250
    (*freeHeadFp)(pHead);
    (*freeBodyFp)(pBody);
S
shm  
Shengliang Guan 已提交
251 252 253 254
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
255
  const int32_t pos = pQueue->head;
S
shm  
Shengliang Guan 已提交
256
  if (pQueue->head < pQueue->tail) {
S
shm  
Shengliang Guan 已提交
257 258 259
    memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, headLen);
    memcpy(pBody, pQueue->pBuffer + pQueue->head + 8 + headLen, bodyLen);
    pQueue->head = pQueue->head + 8 + headLen + bodyLen;
S
shm  
Shengliang Guan 已提交
260 261
  } else {
    int32_t remain = pQueue->total - pQueue->head;
S
shm  
Shengliang Guan 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274
    if (remain == 0) {
      memcpy(pHead, pQueue->pBuffer + 8, headLen);
      memcpy(pBody, pQueue->pBuffer + 8 + headLen, bodyLen);
      pQueue->head = 8 + headLen + bodyLen;
    } else if (remain == 8) {
      memcpy(pHead, pQueue->pBuffer, headLen);
      memcpy(pBody, pQueue->pBuffer + headLen, bodyLen);
      pQueue->head = headLen + bodyLen;
    } else if (remain < 8 + headLen) {
      memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, remain - 8);
      memcpy(pHead + remain - 8, pQueue->pBuffer, headLen - (remain - 8));
      memcpy(pBody, pQueue->pBuffer + headLen - (remain - 8), bodyLen);
      pQueue->head = headLen - (remain - 8) + bodyLen;
S
Shengliang Guan 已提交
275
    } else if (remain < 8 + headLen + bodyLen) {
S
shm  
Shengliang Guan 已提交
276 277 278 279
      memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, headLen);
      memcpy(pBody, pQueue->pBuffer + pQueue->head + 8 + headLen, remain - 8 - headLen);
      memcpy(pBody + remain - 8 - headLen, pQueue->pBuffer, bodyLen - (remain - 8 - headLen));
      pQueue->head = bodyLen - (remain - 8 - headLen);
S
shm  
Shengliang Guan 已提交
280
    } else {
S
shm  
Shengliang Guan 已提交
281 282 283
      memcpy(pHead, pQueue->pBuffer + pQueue->head + 8, headLen);
      memcpy(pBody, pQueue->pBuffer + pQueue->head + headLen + 8, bodyLen);
      pQueue->head = pQueue->head + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
284 285 286
    }
  }

S
shm  
Shengliang Guan 已提交
287
  pQueue->avail = pQueue->avail + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
288
  pQueue->items--;
S
shm  
Shengliang Guan 已提交
289
  taosThreadMutexUnlock(&pQueue->mutex);
S
shm  
Shengliang Guan 已提交
290

S
shm  
Shengliang Guan 已提交
291 292 293 294
  *ppHead = pHead;
  *ppBody = pBody;
  *pHeadLen = headLen;
  *pBodyLen = bodyLen;
S
Shengliang Guan 已提交
295
  *pFuncType = (ProcFuncType)ftype;
S
shm  
Shengliang Guan 已提交
296

S
Shengliang Guan 已提交
297 298
  uTrace("proc:%s, pop msg at pos:%d ftype:%d remain:%d, head:%d %p body:%d %p", pQueue->name, pos, ftype,
         pQueue->items, headLen, pHead, bodyLen, pBody);
S
shm  
Shengliang Guan 已提交
299
  return 1;
S
shm  
Shengliang Guan 已提交
300 301 302
}

SProcObj *taosProcInit(const SProcCfg *pCfg) {
wafwerar's avatar
wafwerar 已提交
303
  SProcObj *pProc = taosMemoryCalloc(1, sizeof(SProcObj));
S
shm  
Shengliang Guan 已提交
304 305 306 307 308
  if (pProc == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
309 310 311 312 313 314 315 316
  int32_t cstart = 0;
  int32_t csize = CEIL8(pCfg->shm.size / 2);
  int32_t pstart = csize;
  int32_t psize = CEIL8(pCfg->shm.size - pstart);
  if (pstart + psize > pCfg->shm.size) {
    psize -= 8;
  }

S
shm  
Shengliang Guan 已提交
317
  pProc->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
318 319
  pProc->pChildQueue = taosProcInitQueue(pCfg->name, pCfg->isChild, (char *)pCfg->shm.ptr + cstart, csize);
  pProc->pParentQueue = taosProcInitQueue(pCfg->name, pCfg->isChild, (char *)pCfg->shm.ptr + pstart, psize);
S
shm  
Shengliang Guan 已提交
320
  if (pProc->pChildQueue == NULL || pProc->pParentQueue == NULL) {
S
shm  
Shengliang Guan 已提交
321
    taosProcCleanupQueue(pProc->pChildQueue);
wafwerar's avatar
wafwerar 已提交
322
    taosMemoryFree(pProc);
S
shm  
Shengliang Guan 已提交
323 324 325
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339
  pProc->name = pCfg->name;
  pProc->pParent = pCfg->pParent;
  pProc->childMallocHeadFp = pCfg->childMallocHeadFp;
  pProc->childFreeHeadFp = pCfg->childFreeHeadFp;
  pProc->childMallocBodyFp = pCfg->childMallocBodyFp;
  pProc->childFreeBodyFp = pCfg->childFreeBodyFp;
  pProc->childConsumeFp = pCfg->childConsumeFp;
  pProc->parentMallocHeadFp = pCfg->parentMallocHeadFp;
  pProc->parentFreeHeadFp = pCfg->parentFreeHeadFp;
  pProc->parentMallocBodyFp = pCfg->parentMallocBodyFp;
  pProc->parentFreeBodyFp = pCfg->parentFreeBodyFp;
  pProc->parentConsumeFp = pCfg->parentConsumeFp;
  pProc->isChild = pCfg->isChild;

S
shm  
Shengliang Guan 已提交
340
  uDebug("proc:%s, is initialized, isChild:%d child queue:%p parent queue:%p", pProc->name, pProc->isChild,
S
shm  
Shengliang Guan 已提交
341
         pProc->pChildQueue, pProc->pParentQueue);
S
shm  
Shengliang Guan 已提交
342

S
shm  
Shengliang Guan 已提交
343 344 345
  return pProc;
}

S
shm  
Shengliang Guan 已提交
346
static void taosProcThreadLoop(SProcObj *pProc) {
S
shm  
Shengliang Guan 已提交
347
  void         *pHead, *pBody;
S
Shengliang Guan 已提交
348 349 350
  int16_t       headLen;
  ProcFuncType  ftype;
  int32_t       bodyLen;
S
shm  
Shengliang Guan 已提交
351 352 353 354 355 356
  SProcQueue   *pQueue;
  ProcConsumeFp consumeFp;
  ProcMallocFp  mallocHeadFp;
  ProcFreeFp    freeHeadFp;
  ProcMallocFp  mallocBodyFp;
  ProcFreeFp    freeBodyFp;
S
shm  
Shengliang Guan 已提交
357

S
shm  
Shengliang Guan 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  if (pProc->isChild) {
    pQueue = pProc->pChildQueue;
    consumeFp = pProc->childConsumeFp;
    mallocHeadFp = pProc->childMallocHeadFp;
    freeHeadFp = pProc->childFreeHeadFp;
    mallocBodyFp = pProc->childMallocBodyFp;
    freeBodyFp = pProc->childFreeBodyFp;
  } else {
    pQueue = pProc->pParentQueue;
    consumeFp = pProc->parentConsumeFp;
    mallocHeadFp = pProc->parentMallocHeadFp;
    freeHeadFp = pProc->parentFreeHeadFp;
    mallocBodyFp = pProc->parentMallocBodyFp;
    freeBodyFp = pProc->parentFreeBodyFp;
  }

S
shm  
Shengliang Guan 已提交
374
  uDebug("proc:%s, start to get msg from queue:%p", pProc->name, pQueue);
S
shm  
Shengliang Guan 已提交
375

S
shm  
Shengliang Guan 已提交
376
  while (1) {
S
shm  
Shengliang Guan 已提交
377 378
    int32_t numOfMsgs = taosProcQueuePop(pQueue, &pHead, &headLen, &pBody, &bodyLen, &ftype, mallocHeadFp, freeHeadFp,
                                         mallocBodyFp, freeBodyFp);
S
shm  
Shengliang Guan 已提交
379
    if (numOfMsgs == 0) {
S
shm  
Shengliang Guan 已提交
380
      uDebug("proc:%s, get no msg from queue:%p and exit the proc thread", pProc->name, pQueue);
S
shm  
Shengliang Guan 已提交
381
      break;
S
shm  
Shengliang Guan 已提交
382
    } else if (numOfMsgs < 0) {
S
shm  
Shengliang Guan 已提交
383
      uTrace("proc:%s, get no msg from queue:%p since %s", pProc->name, pQueue, terrstr());
S
shm  
Shengliang Guan 已提交
384 385 386
      taosMsleep(1);
      continue;
    } else {
S
shm  
Shengliang Guan 已提交
387
      (*consumeFp)(pProc->pParent, pHead, headLen, pBody, bodyLen, ftype);
S
shm  
Shengliang Guan 已提交
388 389 390 391
    }
  }
}

S
shm  
Shengliang Guan 已提交
392
int32_t taosProcRun(SProcObj *pProc) {
S
Shengliang Guan 已提交
393 394 395
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
S
shm  
Shengliang Guan 已提交
396

S
shm  
Shengliang Guan 已提交
397 398 399 400
  if (taosThreadCreate(&pProc->thread, &thAttr, (ProcThreadFp)taosProcThreadLoop, pProc) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to create thread since %s", terrstr());
    return -1;
S
shm  
Shengliang Guan 已提交
401 402
  }

S
shm  
Shengliang Guan 已提交
403
  uDebug("proc:%s, start to consume queue:%p, thread:%" PRId64, pProc->name, pProc->pChildQueue, pProc->thread);
S
shm  
Shengliang Guan 已提交
404 405 406
  return 0;
}

S
shm  
Shengliang Guan 已提交
407 408
static void taosProcStop(SProcObj *pProc) {
  if (!taosCheckPthreadValid(pProc->thread)) return;
S
shm  
Shengliang Guan 已提交
409

S
shm  
Shengliang Guan 已提交
410
  uDebug("proc:%s, start to join thread:%" PRId64, pProc->name, pProc->thread);
S
shm  
Shengliang Guan 已提交
411 412 413
  SProcQueue *pQueue;
  if (pProc->isChild) {
    pQueue = pProc->pChildQueue;
S
shm  
Shengliang Guan 已提交
414 415
  } else {
    pQueue = pProc->pParentQueue;
S
shm  
Shengliang Guan 已提交
416 417 418 419
  }
  tsem_post(&pQueue->sem);
  taosThreadJoin(pProc->thread, NULL);
}
S
shm  
Shengliang Guan 已提交
420

S
shm  
Shengliang Guan 已提交
421 422
void taosProcCleanup(SProcObj *pProc) {
  if (pProc != NULL) {
S
shm  
Shengliang Guan 已提交
423
    uDebug("proc:%s, start to clean up", pProc->name);
S
shm  
Shengliang Guan 已提交
424
    taosProcStop(pProc);
S
shm  
Shengliang Guan 已提交
425 426
    taosProcCleanupQueue(pProc->pChildQueue);
    taosProcCleanupQueue(pProc->pParentQueue);
S
shm  
Shengliang Guan 已提交
427
    uDebug("proc:%s, is cleaned up", pProc->name);
wafwerar's avatar
wafwerar 已提交
428
    taosMemoryFree(pProc);
S
shm  
Shengliang Guan 已提交
429 430 431
  }
}

S
shm  
Shengliang Guan 已提交
432
int32_t taosProcPutToChildQ(SProcObj *pProc, const void *pHead, int16_t headLen, const void *pBody, int32_t bodyLen,
S
Shengliang Guan 已提交
433 434
                            ProcFuncType ftype) {
  return taosProcQueuePush(pProc->pChildQueue, pHead, headLen, pBody, bodyLen, ftype);
S
shm  
Shengliang Guan 已提交
435
}
S
shm  
Shengliang Guan 已提交
436

S
shm  
Shengliang Guan 已提交
437
int32_t taosProcPutToParentQ(SProcObj *pProc, const void *pHead, int16_t headLen, const void *pBody, int32_t bodyLen,
S
Shengliang Guan 已提交
438 439
                             ProcFuncType ftype) {
  return taosProcQueuePush(pProc->pParentQueue, pHead, headLen, pBody, bodyLen, ftype);
S
shm  
Shengliang Guan 已提交
440
}