syncEnv.c 3.3 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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/>.
 */

#include "syncEnv.h"
#include <assert.h>

SSyncEnv *gSyncEnv = NULL;

M
Minghao Li 已提交
21
// local function -----------------
M
Minghao Li 已提交
22 23 24 25 26
static SSyncEnv *doSyncEnvStart();
static int32_t   doSyncEnvStop(SSyncEnv *pSyncEnv);
static int32_t   doSyncEnvStartTimer(SSyncEnv *pSyncEnv);
static int32_t   doSyncEnvStopTimer(SSyncEnv *pSyncEnv);
static void      syncEnvTick(void *param, void *tmrId);
M
Minghao Li 已提交
27 28
// --------------------------------

M
Minghao Li 已提交
29
int32_t syncEnvStart() {
M
Minghao Li 已提交
30
  int32_t ret = 0;
wafwerar's avatar
wafwerar 已提交
31
  taosSeedRand(taosGetTimestampSec());
M
Minghao Li 已提交
32
  gSyncEnv = doSyncEnvStart(gSyncEnv);
M
Minghao Li 已提交
33 34 35 36 37 38 39 40 41
  assert(gSyncEnv != NULL);
  return ret;
}

int32_t syncEnvStop() {
  int32_t ret = doSyncEnvStop(gSyncEnv);
  return ret;
}

M
Minghao Li 已提交
42 43 44
int32_t syncEnvStartTimer() {
  int32_t ret = doSyncEnvStartTimer(gSyncEnv);
  return ret;
M
Minghao Li 已提交
45 46
}

M
Minghao Li 已提交
47 48 49 50
int32_t syncEnvStopTimer() {
  int32_t ret = doSyncEnvStopTimer(gSyncEnv);
  return ret;
}
M
Minghao Li 已提交
51 52 53 54

// local function -----------------
static void syncEnvTick(void *param, void *tmrId) {
  SSyncEnv *pSyncEnv = (SSyncEnv *)param;
M
Minghao Li 已提交
55 56 57 58 59 60 61 62 63 64 65
  if (atomic_load_64(&pSyncEnv->envTickTimerLogicClockUser) <= atomic_load_64(&pSyncEnv->envTickTimerLogicClock)) {
    ++(pSyncEnv->envTickTimerCounter);
    sTrace(
        "syncEnvTick ... envTickTimerLogicClockUser:%lu, envTickTimerLogicClock:%lu, envTickTimerCounter:%lu, "
        "envTickTimerMS:%d",
        pSyncEnv->envTickTimerLogicClockUser, pSyncEnv->envTickTimerLogicClock, pSyncEnv->envTickTimerCounter,
        pSyncEnv->envTickTimerMS);

    // do something, tick ...
    taosTmrReset(syncEnvTick, pSyncEnv->envTickTimerMS, pSyncEnv, pSyncEnv->pTimerManager, &pSyncEnv->pEnvTickTimer);
  }
M
Minghao Li 已提交
66 67
}

M
Minghao Li 已提交
68 69 70 71 72 73 74 75 76 77
static SSyncEnv *doSyncEnvStart() {
  SSyncEnv *pSyncEnv = (SSyncEnv *)malloc(sizeof(SSyncEnv));
  assert(pSyncEnv != NULL);
  memset(pSyncEnv, 0, sizeof(pSyncEnv));

  pSyncEnv->envTickTimerCounter = 0;
  pSyncEnv->envTickTimerMS = ENV_TICK_TIMER_MS;
  pSyncEnv->FpEnvTickTimer = syncEnvTick;
  atomic_store_64(&pSyncEnv->envTickTimerLogicClock, 0);
  atomic_store_64(&pSyncEnv->envTickTimerLogicClockUser, 0);
M
Minghao Li 已提交
78 79 80

  // start tmr thread
  pSyncEnv->pTimerManager = taosTmrInit(1000, 50, 10000, "SYNC-ENV");
M
Minghao Li 已提交
81
  return pSyncEnv;
M
Minghao Li 已提交
82 83 84 85 86 87 88
}

static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) {
  taosTmrCleanUp(pSyncEnv->pTimerManager);
  return 0;
}

M
Minghao Li 已提交
89 90 91 92 93 94
static int32_t doSyncEnvStartTimer(SSyncEnv *pSyncEnv) {
  int32_t ret = 0;
  pSyncEnv->pEnvTickTimer =
      taosTmrStart(pSyncEnv->FpEnvTickTimer, pSyncEnv->envTickTimerMS, pSyncEnv, pSyncEnv->pTimerManager);
  atomic_store_64(&pSyncEnv->envTickTimerLogicClock, pSyncEnv->envTickTimerLogicClockUser);
  return ret;
M
Minghao Li 已提交
95
}
M
Minghao Li 已提交
96

M
Minghao Li 已提交
97 98 99 100 101 102
static int32_t doSyncEnvStopTimer(SSyncEnv *pSyncEnv) {
  atomic_add_fetch_64(&pSyncEnv->envTickTimerLogicClockUser, 1);
  taosTmrStop(pSyncEnv->pEnvTickTimer);
  pSyncEnv->pEnvTickTimer = NULL;
  return 0;
}