diff --git a/src/os/darwin/src/tdarwin.c b/src/os/darwin/src/tdarwin.c index 7896592030f8e18e5d4be5efad0bf7c78100dfb8..a8dfdb1e3ff9360f7196ca455b81f873dfef7fc6 100644 --- a/src/os/darwin/src/tdarwin.c +++ b/src/os/darwin/src/tdarwin.c @@ -243,6 +243,11 @@ int taosInitTimer(void (*callback)(int), int ms) { return setitimer(ITIMER_REAL, &tv, NULL); } +void taosUninitTimer() { + struct itimerval tv = { 0 }; + return setitimer(ITIMER_REAL, &tv, NULL); +} + void taosGetSystemTimezone() { // get and set default timezone SGlobalConfig *cfg_timezone = tsGetConfigOption("timezone"); diff --git a/src/os/linux/src/tlinux.c b/src/os/linux/src/tlinux.c index c1bd0ceb5f5059f09bec9023dc1c767fbc871b9e..88d69816abbc161d07d1f96f33c6558a3a2ac42c 100644 --- a/src/os/linux/src/tlinux.c +++ b/src/os/linux/src/tlinux.c @@ -286,20 +286,23 @@ void *taosProcessAlarmSignal(void *tharg) { return NULL; } +static pthread_t timerThread; + int taosInitTimer(void (*callback)(int), int ms) { - pthread_t thread; pthread_attr_t tattr; pthread_attr_init(&tattr); - pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); - int code = pthread_create(&thread, &tattr, taosProcessAlarmSignal, callback); - pthread_detach(thread); + int code = pthread_create(&timerThread, &tattr, taosProcessAlarmSignal, callback); pthread_attr_destroy(&tattr); if (code != 0) { tmrError("failed to create timer thread"); 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) { diff --git a/src/os/windows/src/twintimer.c b/src/os/windows/src/twintimer.c index 68899bea51e936678abfbe6004e8fdb2713ce11a..2bb8478f09dc61c395d425953e2c2251862df557 100644 --- a/src/os/windows/src/twintimer.c +++ b/src/os/windows/src/twintimer.c @@ -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) { - MMRESULT timerId; DWORD_PTR param = *((int64_t *) & callback); timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); @@ -41,6 +41,10 @@ int taosInitTimer(win_timer_f callback, int ms) { return 0; } +void taosUninitTimer() { + timeKillEvent(timerId); +} + void taosMsleep(int mseconds) { Sleep(mseconds); } diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index 81cf177e73696723d147aefb4352df721240a1cf..60b2868ccfb6180ab6aa810d3ffb1ae05bc86cf1 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -139,6 +139,7 @@ int32_t taosFileRename(char *fullPath, char *suffix, char delimiter, char **dstP void getTmpfilePath(const char *fileNamePattern, char *dstPath); 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); diff --git a/src/util/src/ttimer.c b/src/util/src/ttimer.c index 04cad7565514e7dc4eea5385d4f8c19eb875556d..25862fbec3c969439a3a3c4186c5726869740315 100644 --- a/src/util/src/ttimer.c +++ b/src/util/src/ttimer.c @@ -84,8 +84,6 @@ static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* unusedTmrCtrl = NULL; static void* tmrQhandle; static int numOfTmrCtrl = 0; -//static void* tmrContext = NULL; -static int athread = 0; int taosTmrThreads = 1; @@ -519,7 +517,7 @@ static void taosTmrModuleInit(void) { } 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); } @@ -562,19 +560,29 @@ void taosTmrCleanUp(void* handle) { pthread_mutex_unlock(&tmrCtrlMutex); if (numOfTmrCtrl <=0) { -// pthread_cancel(athread); + taosUninitTimer(); + taosCleanUpScheduler(tmrQhandle); + 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); + 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(tmrCtrls); - taosCleanUpScheduler(tmrQhandle); - tmrModuleInit = PTHREAD_ONCE_INIT; tmrTrace("timer module is cleaned up"); }