未验证 提交 77f1cf16 编写于 作者: S slguan 提交者: GitHub

Merge pull request #1633 from taosdata/hotfix/timer-memory-leak

fix memory leaks in timer
...@@ -243,6 +243,11 @@ int taosInitTimer(void (*callback)(int), int ms) { ...@@ -243,6 +243,11 @@ int taosInitTimer(void (*callback)(int), int ms) {
return setitimer(ITIMER_REAL, &tv, NULL); return setitimer(ITIMER_REAL, &tv, NULL);
} }
void taosUninitTimer() {
struct itimerval tv = { 0 };
return setitimer(ITIMER_REAL, &tv, NULL);
}
void taosGetSystemTimezone() { void taosGetSystemTimezone() {
// get and set default timezone // get and set default timezone
SGlobalConfig *cfg_timezone = tsGetConfigOption("timezone"); SGlobalConfig *cfg_timezone = tsGetConfigOption("timezone");
......
...@@ -286,20 +286,23 @@ void *taosProcessAlarmSignal(void *tharg) { ...@@ -286,20 +286,23 @@ void *taosProcessAlarmSignal(void *tharg) {
return NULL; return NULL;
} }
static pthread_t timerThread;
int taosInitTimer(void (*callback)(int), int ms) { int taosInitTimer(void (*callback)(int), int ms) {
pthread_t thread;
pthread_attr_t tattr; pthread_attr_t tattr;
pthread_attr_init(&tattr); pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); int code = pthread_create(&timerThread, &tattr, taosProcessAlarmSignal, callback);
int code = pthread_create(&thread, &tattr, taosProcessAlarmSignal, callback);
pthread_detach(thread);
pthread_attr_destroy(&tattr); pthread_attr_destroy(&tattr);
if (code != 0) { if (code != 0) {
tmrError("failed to create timer thread"); tmrError("failed to create timer thread");
return -1; return -1;
} }
return 0;
}
return thread; void taosUninitTimer() {
pthread_cancel(timerThread);
pthread_join(timerThread, NULL);
} }
ssize_t tread(int fd, void *buf, size_t count) { ssize_t tread(int fd, void *buf, size_t count) {
......
...@@ -30,8 +30,8 @@ void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR ...@@ -30,8 +30,8 @@ void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR
} }
} }
static MMRESULT timerId;
int taosInitTimer(win_timer_f callback, int ms) { int taosInitTimer(win_timer_f callback, int ms) {
MMRESULT timerId;
DWORD_PTR param = *((int64_t *) & callback); DWORD_PTR param = *((int64_t *) & callback);
timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC);
...@@ -41,6 +41,10 @@ int taosInitTimer(win_timer_f callback, int ms) { ...@@ -41,6 +41,10 @@ int taosInitTimer(win_timer_f callback, int ms) {
return 0; return 0;
} }
void taosUninitTimer() {
timeKillEvent(timerId);
}
void taosMsleep(int mseconds) { void taosMsleep(int mseconds) {
Sleep(mseconds); Sleep(mseconds);
} }
......
...@@ -139,6 +139,7 @@ int32_t taosFileRename(char *fullPath, char *suffix, char delimiter, char **dstP ...@@ -139,6 +139,7 @@ int32_t taosFileRename(char *fullPath, char *suffix, char delimiter, char **dstP
void getTmpfilePath(const char *fileNamePattern, char *dstPath); void getTmpfilePath(const char *fileNamePattern, char *dstPath);
int32_t taosInitTimer(void (*callback)(int), int32_t ms); int32_t taosInitTimer(void (*callback)(int), int32_t ms);
void taosUninitTimer();
bool taosMbsToUcs4(char *mbs, int32_t mbs_len, char *ucs4, int32_t ucs4_max_len); bool taosMbsToUcs4(char *mbs, int32_t mbs_len, char *ucs4, int32_t ucs4_max_len);
......
...@@ -84,8 +84,6 @@ static tmr_ctrl_t* tmrCtrls; ...@@ -84,8 +84,6 @@ 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;
...@@ -519,7 +517,7 @@ static void taosTmrModuleInit(void) { ...@@ -519,7 +517,7 @@ static void taosTmrModuleInit(void) {
} }
tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr"); tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr");
athread = taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK); 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);
} }
...@@ -562,7 +560,9 @@ void taosTmrCleanUp(void* handle) { ...@@ -562,7 +560,9 @@ void taosTmrCleanUp(void* handle) {
pthread_mutex_unlock(&tmrCtrlMutex); pthread_mutex_unlock(&tmrCtrlMutex);
if (numOfTmrCtrl <=0) { if (numOfTmrCtrl <=0) {
// pthread_cancel(athread); taosUninitTimer();
taosCleanUpScheduler(tmrQhandle);
for (int i = 0; i < tListLen(wheels); i++) { for (int i = 0; i < tListLen(wheels); i++) {
time_wheel_t* wheel = wheels + i; time_wheel_t* wheel = wheels + i;
...@@ -571,10 +571,18 @@ void taosTmrCleanUp(void* handle) { ...@@ -571,10 +571,18 @@ void taosTmrCleanUp(void* handle) {
} }
pthread_mutex_destroy(&tmrCtrlMutex); pthread_mutex_destroy(&tmrCtrlMutex);
for (size_t i = 0; i < timerMap.size; i++) {
timer_list_t* list = timerMap.slots + i;
tmr_obj_t* t = list->timers;
while (t != NULL) {
tmr_obj_t* next = t->mnext;
free(t);
t = next;
}
}
free(timerMap.slots); free(timerMap.slots);
free(tmrCtrls); free(tmrCtrls);
taosCleanUpScheduler(tmrQhandle);
tmrModuleInit = PTHREAD_ONCE_INIT;
tmrTrace("timer module is cleaned up"); tmrTrace("timer module is cleaned up");
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册