diff --git a/include/os/osTimer.h b/include/os/osTimer.h index 4b5db895a2..9040113b23 100644 --- a/include/os/osTimer.h +++ b/include/os/osTimer.h @@ -20,6 +20,15 @@ extern "C" { #endif +// If the error is in a third-party library, place this header file under the third-party library header file. +#ifndef ALLOW_FORBID_FUNC + #define timer_create TIMER_CREATE_FUNC_TAOS_FORBID + #define timer_settime TIMER_SETTIME_FUNC_TAOS_FORBID + #define timer_delete TIMER_DELETE_FUNC_TAOS_FORBID + #define timeSetEvent TIMESETEVENT_SETTIME_FUNC_TAOS_FORBID + #define timeKillEvent TIMEKILLEVENT_SETTIME_FUNC_TAOS_FORBID +#endif + #define MSECONDS_PER_TICK 5 int32_t taosInitTimer(void (*callback)(int32_t), int32_t ms); diff --git a/source/os/src/osTimer.c b/source/os/src/osTimer.c index 6b60923189..c95ca72bd5 100644 --- a/source/os/src/osTimer.c +++ b/source/os/src/osTimer.c @@ -13,15 +13,11 @@ * along with this program. If not, see . */ +#define ALLOW_FORBID_FUNC #define _DEFAULT_SOURCE #include "os.h" #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - -/* - * windows implementation - */ - #include #include #include @@ -39,24 +35,9 @@ void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR } static MMRESULT timerId; -int taosInitTimer(win_timer_f callback, int ms) { - DWORD_PTR param = *((int64_t *)&callback); - - timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); - if (timerId == 0) { - return -1; - } - return 0; -} - -void taosUninitTimer() { timeKillEvent(timerId); } #elif defined(_TD_DARWIN_64) -/* - * darwin implementation - */ - #include #include #include @@ -88,53 +69,12 @@ static void* timer_routine(void* arg) { return NULL; } -int taosInitTimer(void (*callback)(int), int ms) { - int r = 0; - timer_kq = -1; - timer_stop = 0; - timer_ms = ms; - timer_callback = callback; - - timer_kq = kqueue(); - if (timer_kq == -1) { - fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - - r = pthread_create(&timer_thread, NULL, timer_routine, NULL); - if (r) { - fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - return 0; -} - -void taosUninitTimer() { - int r = 0; - timer_stop = 1; - r = pthread_join(timer_thread, NULL); - if (r) { - fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); - // since no caller of this func checks the return value for the moment - abort(); - } - close(timer_kq); - timer_kq = -1; -} - void taos_block_sigalrm(void) { // we don't know if there's any specific API for SIGALRM to deliver to specific thread // this implementation relies on kqueue rather than SIGALRM } #else - -/* - * linux implementation - */ - #include #include @@ -200,8 +140,39 @@ static void * taosProcessAlarmSignal(void *tharg) { return NULL; } +#endif int taosInitTimer(void (*callback)(int), int ms) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + DWORD_PTR param = *((int64_t *)&callback); + + timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); + if (timerId == 0) { + return -1; + } + return 0; +#elif defined(_TD_DARWIN_64) + int r = 0; + timer_kq = -1; + timer_stop = 0; + timer_ms = ms; + timer_callback = callback; + + timer_kq = kqueue(); + if (timer_kq == -1) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer kq\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + + r = pthread_create(&timer_thread, NULL, timer_routine, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to create timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + return 0; +#else stopTimer = false; pthread_attr_t tattr; pthread_attr_init(&tattr); @@ -215,13 +186,29 @@ int taosInitTimer(void (*callback)(int), int ms) { } return 0; +#endif } void taosUninitTimer() { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + timeKillEvent(timerId); +#elif defined(_TD_DARWIN_64) + int r = 0; + timer_stop = 1; + r = pthread_join(timer_thread, NULL); + if (r) { + fprintf(stderr, "==%s[%d]%s()==failed to join timer thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__); + // since no caller of this func checks the return value for the moment + abort(); + } + close(timer_kq); + timer_kq = -1; +#else stopTimer = true; // printf("join timer thread:0x%08" PRIx64, taosGetPthreadId(timerThread)); pthread_join(timerThread, NULL); +#endif } int64_t taosGetMonotonicMs() { @@ -239,5 +226,3 @@ const char *taosMonotonicInit() { return NULL; #endif } - -#endif