提交 c5c197f5 编写于 作者: J Jeff Tao

remove the memory leak for timer module when it cleans up

上级 dcbf5972
...@@ -227,6 +227,11 @@ int taosOpenUDServerSocket(char *ip, uint16_t port) { ...@@ -227,6 +227,11 @@ int taosOpenUDServerSocket(char *ip, uint16_t port) {
return sockFd; return sockFd;
} }
static void taosDeleteTimer(void *tharg) {
timer_t *pTimer = tharg;
timer_delete(*pTimer);
}
void *taosProcessAlarmSignal(void *tharg) { void *taosProcessAlarmSignal(void *tharg) {
// Block the signal // Block the signal
sigset_t sigset; sigset_t sigset;
...@@ -235,7 +240,7 @@ void *taosProcessAlarmSignal(void *tharg) { ...@@ -235,7 +240,7 @@ void *taosProcessAlarmSignal(void *tharg) {
sigprocmask(SIG_BLOCK, &sigset, NULL); sigprocmask(SIG_BLOCK, &sigset, NULL);
void (*callback)(int) = tharg; void (*callback)(int) = tharg;
timer_t timerId; static timer_t timerId;
struct sigevent sevent = {0}; struct sigevent sevent = {0};
#ifdef _ALPINE #ifdef _ALPINE
...@@ -252,6 +257,8 @@ void *taosProcessAlarmSignal(void *tharg) { ...@@ -252,6 +257,8 @@ void *taosProcessAlarmSignal(void *tharg) {
tmrError("Failed to create timer"); tmrError("Failed to create timer");
} }
pthread_cleanup_push(taosDeleteTimer, &timerId);
struct itimerspec ts; struct itimerspec ts;
ts.it_value.tv_sec = 0; ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 1000000 * MSECONDS_PER_TICK; ts.it_value.tv_nsec = 1000000 * MSECONDS_PER_TICK;
...@@ -273,6 +280,8 @@ void *taosProcessAlarmSignal(void *tharg) { ...@@ -273,6 +280,8 @@ void *taosProcessAlarmSignal(void *tharg) {
callback(0); callback(0);
} }
pthread_cleanup_pop(1);
return NULL; return NULL;
} }
...@@ -282,13 +291,15 @@ int taosInitTimer(void (*callback)(int), int ms) { ...@@ -282,13 +291,15 @@ int taosInitTimer(void (*callback)(int), int ms) {
pthread_attr_t tattr; pthread_attr_t tattr;
pthread_attr_init(&tattr); pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&thread, &tattr, taosProcessAlarmSignal, callback) != 0) { int code = pthread_create(&thread, &tattr, taosProcessAlarmSignal, callback);
pthread_detach(thread);
pthread_attr_destroy(&tattr);
if (code != 0) {
tmrError("failed to create timer thread"); tmrError("failed to create timer thread");
return -1; return -1;
} }
pthread_attr_destroy(&tattr); return thread;
return 0;
} }
ssize_t tread(int fd, void *buf, size_t count) { ssize_t tread(int fd, void *buf, size_t count) {
......
...@@ -302,6 +302,7 @@ void rpcClose(void *param) { ...@@ -302,6 +302,7 @@ void rpcClose(void *param) {
tfree(pRpc->connList); tfree(pRpc->connList);
pthread_mutex_destroy(&pRpc->mutex); pthread_mutex_destroy(&pRpc->mutex);
tTrace("%s RPC is closed", pRpc->label);
tfree(pRpc); tfree(pRpc);
} }
......
...@@ -84,6 +84,8 @@ static tmr_ctrl_t* tmrCtrls; ...@@ -84,6 +84,8 @@ static tmr_ctrl_t* tmrCtrls;
static tmr_ctrl_t* unusedTmrCtrl = NULL; static tmr_ctrl_t* unusedTmrCtrl = NULL;
static void* tmrQhandle; static void* tmrQhandle;
static int numOfTmrCtrl = 0; static int numOfTmrCtrl = 0;
//static void* tmrContext = NULL;
static int athread = 0;
int taosTmrThreads = 1; int taosTmrThreads = 1;
...@@ -517,7 +519,7 @@ static void taosTmrModuleInit(void) { ...@@ -517,7 +519,7 @@ static void taosTmrModuleInit(void) {
} }
tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr"); tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr");
taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK); athread = taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK);
tmrTrace("timer module is initialized, number of threads: %d", taosTmrThreads); tmrTrace("timer module is initialized, number of threads: %d", taosTmrThreads);
} }
...@@ -558,4 +560,22 @@ void taosTmrCleanUp(void* handle) { ...@@ -558,4 +560,22 @@ void taosTmrCleanUp(void* handle) {
numOfTmrCtrl--; numOfTmrCtrl--;
unusedTmrCtrl = ctrl; unusedTmrCtrl = ctrl;
pthread_mutex_unlock(&tmrCtrlMutex); pthread_mutex_unlock(&tmrCtrlMutex);
if (numOfTmrCtrl <=0) {
pthread_cancel(athread);
for (int i = 0; i < tListLen(wheels); i++) {
time_wheel_t* wheel = wheels + i;
pthread_mutex_destroy(&wheel->mutex);
free(wheel->slots);
}
pthread_mutex_destroy(&tmrCtrlMutex);
free(timerMap.slots);
free(tmrCtrls);
taosCleanUpScheduler(tmrQhandle);
tmrModuleInit = PTHREAD_ONCE_INIT;
tmrTrace("timer module is cleaned up");
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册