tprocess.c 14.8 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

S
shm  
Shengliang Guan 已提交
22 23 24 25
// todo
#include <sys/shm.h>
#include <sys/wait.h>

S
shm  
Shengliang Guan 已提交
26
#define SHM_DEFAULT_SIZE (20 * 1024 * 1024)
S
shm  
Shengliang Guan 已提交
27
#define CEIL8(n)         (ceil((float)(n) / 8) * 8)
S
shm  
Shengliang Guan 已提交
28
typedef void *(*ProcThreadFp)(void *param);
S
shm  
Shengliang Guan 已提交
29 30

typedef struct SProcQueue {
S
shm  
Shengliang Guan 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
  int32_t          head;
  int32_t          tail;
  int32_t          total;
  int32_t          avail;
  int32_t          items;
  char            *pBuffer;
  ProcMallocFp     mallocHeadFp;
  ProcFreeFp       freeHeadFp;
  ProcMallocFp     mallocBodyFp;
  ProcFreeFp       freeBodyFp;
  ProcConsumeFp    consumeFp;
  void            *pParent;
  tsem_t           sem;
S
Shengliang Guan 已提交
44
  TdThreadMutex   *mutex;
S
shm  
Shengliang Guan 已提交
45 46 47
  int32_t          mutexShmid;
  int32_t          bufferShmid;
  const char      *name;
S
shm  
Shengliang Guan 已提交
48 49
} SProcQueue;

S
shm  
Shengliang Guan 已提交
50
typedef struct SProcObj {
S
Shengliang Guan 已提交
51
  TdThread    childThread;
S
shm  
Shengliang Guan 已提交
52
  SProcQueue *pChildQueue;
S
Shengliang Guan 已提交
53
  TdThread    parentThread;
S
shm  
Shengliang Guan 已提交
54
  SProcQueue *pParentQueue;
S
shm  
Shengliang Guan 已提交
55
  const char *name;
S
shm  
Shengliang Guan 已提交
56 57 58 59 60
  int32_t     pid;
  bool        isChild;
  bool        stopFlag;
} SProcObj;

S
Shengliang Guan 已提交
61 62 63 64 65
static int32_t taosProcInitMutex(TdThreadMutex **ppMutex, int32_t *pShmid) {
  TdThreadMutex    *pMutex = NULL;
  TdThreadMutexAttr mattr = {0};
  int32_t           shmid = -1;
  int32_t           code = -1;
S
shm  
Shengliang Guan 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78

  if (pthread_mutexattr_init(&mattr) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while init attr since %s", terrstr());
    goto _OVER;
  }

  if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while set shared since %s", terrstr());
    goto _OVER;
  }

S
shm  
Shengliang Guan 已提交
79
  shmid = shmget(IPC_PRIVATE, sizeof(TdThreadMutex), IPC_CREAT | 0600);
S
shm  
Shengliang Guan 已提交
80 81 82 83 84 85
  if (shmid <= 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while shmget since %s", terrstr());
    goto _OVER;
  }

S
Shengliang Guan 已提交
86
  pMutex = (TdThreadMutex *)shmat(shmid, NULL, 0);
S
shm  
Shengliang Guan 已提交
87 88 89 90 91 92
  if (pMutex == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex while shmat since %s", terrstr());
    goto _OVER;
  }

S
Shengliang Guan 已提交
93
  if (taosThreadMutexInit(pMutex, &mattr) != 0) {
S
shm  
Shengliang Guan 已提交
94 95 96 97 98 99 100 101 102
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init mutex since %s", terrstr());
    goto _OVER;
  }

  code = 0;

_OVER:
  if (code != 0) {
S
shm  
Shengliang Guan 已提交
103 104 105 106 107 108 109
    if (pMutex != NULL) {
      taosThreadMutexDestroy(pMutex);
      shmdt(pMutex);
    }
    if (shmid >= 0) {
      shmctl(shmid, IPC_RMID, NULL);
    }
S
shm  
Shengliang Guan 已提交
110 111 112 113 114 115 116 117 118
  } else {
    *ppMutex = pMutex;
    *pShmid = shmid;
  }

  pthread_mutexattr_destroy(&mattr);
  return code;
}

S
shm  
Shengliang Guan 已提交
119
static void taosProcDestroyMutex(TdThreadMutex *pMutex, int32_t shmid) {
S
shm  
Shengliang Guan 已提交
120
  if (pMutex != NULL) {
S
Shengliang Guan 已提交
121
    taosThreadMutexDestroy(pMutex);
S
shm  
Shengliang Guan 已提交
122
  }
S
shm  
Shengliang Guan 已提交
123 124
  if (shmid >= 0) {
    shmctl(shmid, IPC_RMID, NULL);
S
shm  
Shengliang Guan 已提交
125 126 127 128 129 130 131 132 133 134 135
  }
}

static int32_t taosProcInitBuffer(void **ppBuffer, int32_t size) {
  int32_t shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
  if (shmid <= 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init buffer while shmget since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
136
  void *shmptr = shmat(shmid, NULL, 0);
S
shm  
Shengliang Guan 已提交
137 138 139 140 141 142 143 144 145 146 147
  if (shmptr == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to init buffer while shmat since %s", terrstr());
    shmctl(shmid, IPC_RMID, NULL);
    return -1;
  }

  *ppBuffer = shmptr;
  return shmid;
}

S
shm  
Shengliang Guan 已提交
148 149 150 151
static void taosProcDestroyBuffer(void *pBuffer, int32_t shmid) {
  if (shmid > 0) {
    shmdt(pBuffer);
    shmctl(shmid, IPC_RMID, NULL);
S
shm  
Shengliang Guan 已提交
152 153 154
  }
}

S
shm  
Shengliang Guan 已提交
155
static SProcQueue *taosProcQueueInit(int32_t size) {
S
shm  
Shengliang Guan 已提交
156 157
  if (size <= 0) size = SHM_DEFAULT_SIZE;

S
shm  
Shengliang Guan 已提交
158 159
  int32_t bufSize = CEIL8(size);
  int32_t headSize = CEIL8(sizeof(SProcQueue));
S
shm  
Shengliang Guan 已提交
160

S
shm  
Shengliang Guan 已提交
161 162
  SProcQueue *pQueue = NULL;
  int32_t     shmId = taosProcInitBuffer((void **)&pQueue, bufSize + headSize);
S
shm  
Shengliang Guan 已提交
163
  if (shmId < 0) {
S
shm  
Shengliang Guan 已提交
164 165 166
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
shm  
Shengliang Guan 已提交
167 168 169
  pQueue->bufferShmid = shmId;

  if (taosProcInitMutex(&pQueue->mutex, &pQueue->mutexShmid) != 0) {
S
shm  
Shengliang Guan 已提交
170
    taosProcDestroyBuffer(pQueue, pQueue->bufferShmid);
S
shm  
Shengliang Guan 已提交
171 172 173
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
174
  if (tsem_init(&pQueue->sem, 1, 0) != 0) {
S
shm  
Shengliang Guan 已提交
175 176
    taosProcDestroyMutex(pQueue->mutex, pQueue->mutexShmid);
    taosProcDestroyBuffer(pQueue, pQueue->bufferShmid);
S
shm  
Shengliang Guan 已提交
177 178 179 180
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
181 182
  if (taosProcInitMutex(&pQueue->mutex, &pQueue->mutexShmid) != 0) {
    tsem_destroy(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
183 184
    taosProcDestroyMutex(pQueue->mutex, pQueue->mutexShmid);
    taosProcDestroyBuffer(pQueue, pQueue->bufferShmid);
S
shm  
Shengliang Guan 已提交
185 186 187
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
188 189 190 191 192 193
  pQueue->head = 0;
  pQueue->tail = 0;
  pQueue->total = bufSize;
  pQueue->avail = bufSize;
  pQueue->items = 0;
  pQueue->pBuffer = (char *)pQueue + headSize;
S
shm  
Shengliang Guan 已提交
194 195 196
  return pQueue;
}

S
shm  
Shengliang Guan 已提交
197
static void taosProcCleanupQueue(SProcQueue *pQueue) {
S
shm  
Shengliang Guan 已提交
198
  if (pQueue != NULL) {
S
shm  
Shengliang Guan 已提交
199
    uDebug("proc:%s, queue:%p clean up", pQueue->name, pQueue);
S
shm  
Shengliang Guan 已提交
200
    tsem_destroy(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
201 202
    taosProcDestroyMutex(pQueue->mutex, pQueue->mutexShmid);
    taosProcDestroyBuffer(pQueue, pQueue->bufferShmid);
S
shm  
Shengliang Guan 已提交
203
  }
S
shm  
Shengliang Guan 已提交
204 205
}

S
shm  
Shengliang Guan 已提交
206 207 208 209
static int32_t taosProcQueuePush(SProcQueue *pQueue, char *pHead, int32_t rawHeadLen, char *pBody, int32_t rawBodyLen) {
  const int32_t headLen = CEIL8(rawHeadLen);
  const int32_t bodyLen = CEIL8(rawBodyLen);
  const int32_t fullLen = headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
210

S
shm  
Shengliang Guan 已提交
211 212 213 214 215
  if (headLen <= 0 || bodyLen <= 0) {
    terrno = TSDB_CODE_INVALID_PARA;
    return -1;
  }

S
Shengliang Guan 已提交
216
  taosThreadMutexLock(pQueue->mutex);
S
shm  
Shengliang Guan 已提交
217
  if (fullLen > pQueue->avail) {
S
Shengliang Guan 已提交
218
    taosThreadMutexUnlock(pQueue->mutex);
S
shm  
Shengliang Guan 已提交
219 220 221 222
    terrno = TSDB_CODE_OUT_OF_SHM_MEM;
    return -1;
  }

S
shm  
Shengliang Guan 已提交
223 224 225 226 227 228 229 230
  if (pQueue->tail < pQueue->total) {
    *(int32_t *)(pQueue->pBuffer + pQueue->head) = headLen;
    *(int32_t *)(pQueue->pBuffer + pQueue->head + 4) = bodyLen;
  } else {
    *(int32_t *)(pQueue->pBuffer) = headLen;
    *(int32_t *)(pQueue->pBuffer + 4) = bodyLen;
  }

S
shm  
Shengliang Guan 已提交
231
  if (pQueue->tail < pQueue->head) {
S
shm  
Shengliang Guan 已提交
232 233 234
    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 已提交
235 236
  } else {
    int32_t remain = pQueue->total - pQueue->tail;
S
shm  
Shengliang Guan 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    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) {
      memcpy(pQueue->pBuffer + pQueue->head + 8, pHead, remain - 8);
      memcpy(pQueue->pBuffer, pHead + remain - 8, rawHeadLen - (remain - 8));
      memcpy(pQueue->pBuffer + headLen - (remain - 8), pBody, rawBodyLen);
      pQueue->tail = headLen - (remain - 8) + bodyLen;
    } else if (remain < 8 + bodyLen) {
      memcpy(pQueue->pBuffer + pQueue->head + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + pQueue->head + 8 + headLen, pBody, remain - 8 - headLen);
      memcpy(pQueue->pBuffer, pBody + remain - 8 - headLen, rawBodyLen - (remain - 8 - headLen));
      pQueue->tail = bodyLen - (remain - 8 - headLen);
S
shm  
Shengliang Guan 已提交
255
    } else {
S
shm  
Shengliang Guan 已提交
256 257 258
      memcpy(pQueue->pBuffer + pQueue->head + 8, pHead, rawHeadLen);
      memcpy(pQueue->pBuffer + pQueue->head + headLen + 8, pBody, rawBodyLen);
      pQueue->tail = pQueue->head + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
259 260 261 262 263
    }
  }

  pQueue->avail -= fullLen;
  pQueue->items++;
S
Shengliang Guan 已提交
264
  taosThreadMutexUnlock(pQueue->mutex);
S
shm  
Shengliang Guan 已提交
265
  tsem_post(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
266

S
shm  
Shengliang Guan 已提交
267
  uTrace("proc:%s, push msg:%p:%d cont:%p:%d to queue:%p", pQueue->name, pHead, headLen, pBody, bodyLen, pQueue);
S
shm  
Shengliang Guan 已提交
268 269 270
  return 0;
}

S
shm  
Shengliang Guan 已提交
271 272
static int32_t taosProcQueuePop(SProcQueue *pQueue, void **ppHead, int32_t *pHeadLen, void **ppBody,
                                int32_t *pBodyLen) {
S
shm  
Shengliang Guan 已提交
273 274
  tsem_wait(&pQueue->sem);

S
Shengliang Guan 已提交
275
  taosThreadMutexLock(pQueue->mutex);
S
shm  
Shengliang Guan 已提交
276
  if (pQueue->total - pQueue->avail <= 0) {
S
Shengliang Guan 已提交
277
    taosThreadMutexUnlock(pQueue->mutex);
S
shm  
Shengliang Guan 已提交
278 279 280 281 282
    tsem_post(&pQueue->sem);
    terrno = TSDB_CODE_OUT_OF_SHM_MEM;
    return -1;
  }

S
shm  
Shengliang Guan 已提交
283 284 285 286 287 288 289 290 291
  int32_t headLen = 0;
  int32_t bodyLen = 0;
  if (pQueue->head < pQueue->total) {
    headLen = *(int32_t *)(pQueue->pBuffer + pQueue->head);
    bodyLen = *(int32_t *)(pQueue->pBuffer + pQueue->head + 4);
  } else {
    headLen = *(int32_t *)(pQueue->pBuffer);
    bodyLen = *(int32_t *)(pQueue->pBuffer + 4);
  }
S
shm  
Shengliang Guan 已提交
292

S
shm  
Shengliang Guan 已提交
293 294
  void *pHead = (*pQueue->mallocHeadFp)(headLen);
  void *pBody = (*pQueue->mallocBodyFp)(bodyLen);
S
shm  
Shengliang Guan 已提交
295
  if (pHead == NULL || pBody == NULL) {
S
Shengliang Guan 已提交
296
    taosThreadMutexUnlock(pQueue->mutex);
S
shm  
Shengliang Guan 已提交
297
    tsem_post(&pQueue->sem);
S
shm  
Shengliang Guan 已提交
298 299
    (*pQueue->freeHeadFp)(pHead);
    (*pQueue->freeBodyFp)(pBody);
S
shm  
Shengliang Guan 已提交
300 301 302 303 304
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  if (pQueue->head < pQueue->tail) {
S
shm  
Shengliang Guan 已提交
305 306 307
    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 已提交
308 309
  } else {
    int32_t remain = pQueue->total - pQueue->head;
S
shm  
Shengliang Guan 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    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;
    } else if (remain < 8 + bodyLen) {
      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 已提交
328
    } else {
S
shm  
Shengliang Guan 已提交
329 330 331
      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 已提交
332 333 334
    }
  }

S
shm  
Shengliang Guan 已提交
335
  pQueue->avail = pQueue->avail + headLen + bodyLen + 8;
S
shm  
Shengliang Guan 已提交
336
  pQueue->items--;
S
Shengliang Guan 已提交
337
  taosThreadMutexUnlock(pQueue->mutex);
S
shm  
Shengliang Guan 已提交
338

S
shm  
Shengliang Guan 已提交
339 340 341 342
  *ppHead = pHead;
  *ppBody = pBody;
  *pHeadLen = headLen;
  *pBodyLen = bodyLen;
S
shm  
Shengliang Guan 已提交
343 344

  uTrace("proc:%s, get msg:%p:%d cont:%p:%d from queue:%p", pQueue->name, pHead, headLen, pBody, bodyLen, pQueue);
S
shm  
Shengliang Guan 已提交
345 346 347 348 349 350 351 352 353 354
  return 0;
}

SProcObj *taosProcInit(const SProcCfg *pCfg) {
  SProcObj *pProc = calloc(1, sizeof(SProcObj));
  if (pProc == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
355
  pProc->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
356 357
  pProc->pChildQueue = taosProcQueueInit(pCfg->childQueueSize);
  pProc->pParentQueue = taosProcQueueInit(pCfg->parentQueueSize);
S
shm  
Shengliang Guan 已提交
358
  if (pProc->pChildQueue == NULL || pProc->pParentQueue == NULL) {
S
shm  
Shengliang Guan 已提交
359
    taosProcCleanupQueue(pProc->pChildQueue);
S
shm  
Shengliang Guan 已提交
360 361 362 363
    free(pProc);
    return NULL;
  }

S
shm  
Shengliang Guan 已提交
364
  pProc->pChildQueue->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
365 366 367 368 369 370
  pProc->pChildQueue->pParent = pCfg->pParent;
  pProc->pChildQueue->mallocHeadFp = pCfg->childMallocHeadFp;
  pProc->pChildQueue->freeHeadFp = pCfg->childFreeHeadFp;
  pProc->pChildQueue->mallocBodyFp = pCfg->childMallocBodyFp;
  pProc->pChildQueue->freeBodyFp = pCfg->childFreeBodyFp;
  pProc->pChildQueue->consumeFp = pCfg->childConsumeFp;
S
shm  
Shengliang Guan 已提交
371
  pProc->pParentQueue->name = pCfg->name;
S
shm  
Shengliang Guan 已提交
372 373 374 375 376 377 378
  pProc->pParentQueue->pParent = pCfg->pParent;
  pProc->pParentQueue->mallocHeadFp = pCfg->parentdMallocHeadFp;
  pProc->pParentQueue->freeHeadFp = pCfg->parentFreeHeadFp;
  pProc->pParentQueue->mallocBodyFp = pCfg->parentMallocBodyFp;
  pProc->pParentQueue->freeBodyFp = pCfg->parentFreeBodyFp;
  pProc->pParentQueue->consumeFp = pCfg->parentConsumeFp;

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

S
shm  
Shengliang Guan 已提交
381 382 383 384 385 386
  pProc->pid = fork();
  if (pProc->pid == 0) {
    pProc->isChild = 1;
  } else {
    pProc->isChild = 0;
    uInfo("this is parent process, child pid:%d", pProc->pid);
S
shm  
Shengliang Guan 已提交
387
  }
S
shm  
Shengliang Guan 已提交
388

S
shm  
Shengliang Guan 已提交
389 390 391
  return pProc;
}

S
shm  
Shengliang Guan 已提交
392 393 394 395 396
static void taosProcThreadLoop(SProcQueue *pQueue) {
  ProcConsumeFp consumeFp = pQueue->consumeFp;
  void         *pParent = pQueue->pParent;
  void         *pHead, *pBody;
  int32_t       headLen, bodyLen;
S
shm  
Shengliang Guan 已提交
397

S
shm  
Shengliang Guan 已提交
398 399
  uDebug("proc:%s, start to get message from queue:%p", pQueue->name, pQueue);

S
shm  
Shengliang Guan 已提交
400
  while (1) {
S
shm  
Shengliang Guan 已提交
401
    int32_t code = taosProcQueuePop(pQueue, &pHead, &headLen, &pBody, &bodyLen);
S
shm  
Shengliang Guan 已提交
402
    if (code < 0) {
S
shm  
Shengliang Guan 已提交
403
      uDebug("proc:%s, get no message from queue:%p and exiting", pQueue->name, pQueue);
S
shm  
Shengliang Guan 已提交
404
      break;
S
shm  
Shengliang Guan 已提交
405
    } else if (code == 0) {
S
shm  
Shengliang Guan 已提交
406
      uTrace("proc:%s, get no message from queue:%p since %s", pQueue->name, pQueue, terrstr());
S
shm  
Shengliang Guan 已提交
407 408 409
      taosMsleep(1);
      continue;
    } else {
S
shm  
Shengliang Guan 已提交
410
      (*consumeFp)(pParent, pHead, headLen, pBody, bodyLen);
S
shm  
Shengliang Guan 已提交
411 412 413 414
    }
  }
}

S
shm  
Shengliang Guan 已提交
415
int32_t taosProcRun(SProcObj *pProc) {
S
Shengliang Guan 已提交
416 417 418
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
S
shm  
Shengliang Guan 已提交
419

S
shm  
Shengliang Guan 已提交
420
  if (pProc->isChild) {
S
Shengliang Guan 已提交
421
    if (taosThreadCreate(&pProc->childThread, &thAttr, (ProcThreadFp)taosProcThreadLoop, pProc->pChildQueue) != 0) {
S
shm  
Shengliang Guan 已提交
422 423 424 425
      terrno = TAOS_SYSTEM_ERROR(errno);
      uError("failed to create thread since %s", terrstr());
      return -1;
    }
S
shm  
Shengliang Guan 已提交
426
    uDebug("proc:%s, child start to consume queue:%p", pProc->name, pProc->pChildQueue);
S
shm  
Shengliang Guan 已提交
427
  } else {
S
Shengliang Guan 已提交
428
    if (taosThreadCreate(&pProc->parentThread, &thAttr, (ProcThreadFp)taosProcThreadLoop, pProc->pParentQueue) != 0) {
S
shm  
Shengliang Guan 已提交
429 430 431 432
      terrno = TAOS_SYSTEM_ERROR(errno);
      uError("failed to create thread since %s", terrstr());
      return -1;
    }
S
shm  
Shengliang Guan 已提交
433
    uDebug("proc:%s, parent start to consume queue:%p", pProc->name, pProc->pParentQueue);
S
shm  
Shengliang Guan 已提交
434 435 436 437 438 439 440
  }

  return 0;
}

void taosProcStop(SProcObj *pProc) {
  pProc->stopFlag = true;
S
shm  
Shengliang Guan 已提交
441
  // todo join
S
shm  
Shengliang Guan 已提交
442 443
}

S
shm  
Shengliang Guan 已提交
444 445
bool taosProcIsChild(SProcObj *pProc) { return pProc->isChild; }

S
shm  
Shengliang Guan 已提交
446 447
int32_t taosProcChildId(SProcObj *pProc) { return pProc->pid; }

S
shm  
Shengliang Guan 已提交
448 449
void taosProcCleanup(SProcObj *pProc) {
  if (pProc != NULL) {
S
shm  
Shengliang Guan 已提交
450
    uDebug("proc:%s, clean up", pProc->name);
S
shm  
Shengliang Guan 已提交
451
    taosProcStop(pProc);
S
shm  
Shengliang Guan 已提交
452 453
    taosProcCleanupQueue(pProc->pChildQueue);
    taosProcCleanupQueue(pProc->pParentQueue);
S
shm  
Shengliang Guan 已提交
454 455 456 457 458 459 460
    free(pProc);
  }
}

int32_t taosProcPutToChildQueue(SProcObj *pProc, void *pHead, int32_t headLen, void *pBody, int32_t bodyLen) {
  return taosProcQueuePush(pProc->pChildQueue, pHead, headLen, pBody, bodyLen);
}
S
shm  
Shengliang Guan 已提交
461

S
shm  
Shengliang Guan 已提交
462 463
int32_t taosProcPutToParentQueue(SProcObj *pProc, void *pHead, int32_t headLen, void *pBody, int32_t bodyLen) {
  return taosProcQueuePush(pProc->pParentQueue, pHead, headLen, pBody, bodyLen);
S
shm  
Shengliang Guan 已提交
464
}