fix: init once
This commit is contained in:
parent
3629958b43
commit
0ad9c4cebd
|
@ -104,7 +104,7 @@ typedef struct {
|
|||
tmr_h timer;
|
||||
} STqMgmt;
|
||||
|
||||
static STqMgmt tqMgmt;
|
||||
static STqMgmt tqMgmt = {0};
|
||||
|
||||
// init once
|
||||
int tqInit();
|
||||
|
|
|
@ -16,23 +16,35 @@
|
|||
#include "tq.h"
|
||||
|
||||
int32_t tqInit() {
|
||||
int8_t old = atomic_val_compare_exchange_8(&tqMgmt.inited, 0, 1);
|
||||
int8_t old;
|
||||
while (1) {
|
||||
old = atomic_val_compare_exchange_8(&tqMgmt.inited, 0, 2);
|
||||
if (old != 2) break;
|
||||
}
|
||||
|
||||
if (old == 0) {
|
||||
tqMgmt.timer = taosTmrInit(10000, 100, 10000, "TQ");
|
||||
if (tqMgmt.timer == NULL) {
|
||||
atomic_store_8(&tqMgmt.inited, 0);
|
||||
return -1;
|
||||
}
|
||||
atomic_store_8(&tqMgmt.inited, 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tqCleanUp() {
|
||||
int8_t old = atomic_val_compare_exchange_8(&tqMgmt.inited, 1, 2);
|
||||
if (old != 1) return;
|
||||
int8_t old;
|
||||
while (1) {
|
||||
old = atomic_val_compare_exchange_8(&tqMgmt.inited, 1, 2);
|
||||
if (old != 2) break;
|
||||
}
|
||||
|
||||
if (old == 1) {
|
||||
taosTmrCleanUp(tqMgmt.timer);
|
||||
atomic_store_8(&tqMgmt.inited, 0);
|
||||
}
|
||||
}
|
||||
|
||||
STQ* tqOpen(const char* path, SVnode* pVnode, SWal* pWal) {
|
||||
STQ* pTq = taosMemoryMalloc(sizeof(STQ));
|
||||
|
|
|
@ -36,9 +36,13 @@ static void walFreeObj(void *pWal);
|
|||
int64_t walGetSeq() { return (int64_t)atomic_load_32(&tsWal.seq); }
|
||||
|
||||
int32_t walInit() {
|
||||
int8_t old = atomic_val_compare_exchange_8(&tsWal.inited, 0, 1);
|
||||
if (old == 1) return 0;
|
||||
int8_t old;
|
||||
while (1) {
|
||||
old = atomic_val_compare_exchange_8(&tsWal.inited, 0, 2);
|
||||
if (old != 2) break;
|
||||
}
|
||||
|
||||
if (old == 0) {
|
||||
tsWal.refSetId = taosOpenRef(TSDB_MIN_VNODES, walFreeObj);
|
||||
|
||||
int32_t code = walCreateThread();
|
||||
|
@ -49,19 +53,26 @@ int32_t walInit() {
|
|||
}
|
||||
|
||||
wInfo("wal module is initialized, rsetId:%d", tsWal.refSetId);
|
||||
atomic_store_8(&tsWal.inited, 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void walCleanUp() {
|
||||
int8_t old = atomic_val_compare_exchange_8(&tsWal.inited, 1, 2);
|
||||
if (old != 1) {
|
||||
return;
|
||||
int8_t old;
|
||||
while (1) {
|
||||
old = atomic_val_compare_exchange_8(&tsWal.inited, 1, 2);
|
||||
if (old != 2) break;
|
||||
}
|
||||
|
||||
if (old == 1) {
|
||||
walStopThread();
|
||||
taosCloseRef(tsWal.refSetId);
|
||||
wInfo("wal module is cleaned up");
|
||||
atomic_store_8(&tsWal.inited, 0);
|
||||
}
|
||||
}
|
||||
|
||||
SWal *walOpen(const char *path, SWalCfg *pCfg) {
|
||||
SWal *pWal = taosMemoryMalloc(sizeof(SWal));
|
||||
|
|
Loading…
Reference in New Issue