提交 467dcc07 编写于 作者: weixin_48148422's avatar weixin_48148422

fix memory leaks in timer

上级 14acfd35
......@@ -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");
......
......@@ -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) {
......
......@@ -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);
}
......
......@@ -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);
......
......@@ -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");
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册